Skip to content

Commit 086f1bd

Browse files
Add files via upload
1 parent 6cb007d commit 086f1bd

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

QUEUES/QUEUE USING LL.txt

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
public class Queue {
2+
3+
//Define the data members
4+
private Node front;
5+
private Node rear;
6+
private int size;
7+
8+
9+
public Queue() {
10+
//Implement the Constructor
11+
front=null;
12+
rear=null;
13+
size=0;
14+
}
15+
16+
17+
18+
/*----------------- Public Functions of Stack -----------------*/
19+
20+
21+
public int getSize() {
22+
//Implement the getSize() function
23+
return size;
24+
}
25+
26+
27+
public boolean isEmpty() {
28+
//Implement the isEmpty() function
29+
return size==0;
30+
}
31+
32+
33+
public void enqueue(int data) {
34+
//Implement the enqueue(element) function
35+
Node newNode=new Node(data);
36+
if (front==null)
37+
{
38+
front=newNode;
39+
rear=newNode;
40+
}
41+
else
42+
{
43+
rear.next=newNode;
44+
rear=newNode;
45+
}
46+
size=size+1;
47+
}
48+
49+
50+
public int dequeue() {
51+
//Implement the dequeue() function
52+
if (front!=null)
53+
{
54+
int temp=front.data;
55+
front=front.next;
56+
size=size-1;
57+
return temp;
58+
59+
}
60+
else
61+
{
62+
return -1;
63+
}
64+
}
65+
66+
67+
public int front() {
68+
//Implement the front() function
69+
if (front!=null)
70+
{
71+
return front.data;
72+
}
73+
else
74+
{
75+
return -1;
76+
}
77+
78+
}
79+
}

QUEUES/REVERSE QUEUE.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class Solution {
5+
6+
public static void reverseQueue(Queue<Integer> input) {
7+
//Your code goes here
8+
if (input.size()==0 || input.size()==1)
9+
{
10+
return;
11+
}
12+
13+
int temp=input.remove();
14+
reverseQueue(input);
15+
input.add(temp);
16+
}
17+
18+
}

QUEUES/STACK USING 2 QUEUES.txt

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.util.*;
2+
public class Stack {
3+
4+
//Define the data members
5+
private Queue<Integer> q1;
6+
private Queue<Integer> q2;
7+
private int size;
8+
9+
10+
public Stack() {
11+
//Implement the Constructor
12+
q1=new LinkedList<Integer>();
13+
q2=new LinkedList<Integer>();
14+
size=0;
15+
}
16+
17+
18+
19+
/*----------------- Public Functions of Stack -----------------*/
20+
21+
22+
public int getSize() {
23+
//Implement the getSize() function
24+
return size;
25+
}
26+
27+
public boolean isEmpty() {
28+
//Implement the isEmpty() function
29+
return size==0;
30+
}
31+
32+
public void push(int element) {
33+
//Implement the push(element) function
34+
q1.add(element);
35+
size=size+1;
36+
}
37+
38+
public int pop() {
39+
//Implement the pop() function
40+
if (q1.isEmpty())
41+
{
42+
return -1;
43+
}
44+
while(q1.size()!=1)
45+
{
46+
q2.add(q1.remove());
47+
}
48+
int top=q1.remove();
49+
50+
while(!q2.isEmpty())
51+
{
52+
q1.add(q2.remove());
53+
}
54+
size=size-1;
55+
return top;
56+
}
57+
58+
public int top() {
59+
//Implement the top() function
60+
if (q1.isEmpty())
61+
{
62+
return -1;
63+
}
64+
while(q1.size()!=1)
65+
{
66+
q2.add(q1.remove());
67+
}
68+
int top=q1.peek();
69+
q2.add(q1.remove());
70+
71+
Queue<Integer> q=q1;
72+
q1=q2;
73+
q2=q;
74+
return top;
75+
}
76+
}

0 commit comments

Comments
 (0)