-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacksusingqueue.java
59 lines (55 loc) · 961 Bytes
/
stacksusingqueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//implementing stacks using queues
import java.util.LinkedList;
import java.util.Queue;
class Squeue
{
Queue<Integer> q = new LinkedList<Integer>();
//inserting in the queue
void push(int item)
{
int size=q.size();
q.add(item);
for(int i=0;i<size;i++)
{
int x=q.remove();
q.add(item);
}
}
//removing the top element
int pop()
{
if(q.isEmpty())
{
System.out.print("No elements");
return -1;
}
int x=q.remove();
return x;
}
//returning the top element of the stack
int top()
{
if(q.isEmpty())
return -1;
return q.peek();
}
//checking whether element is present in the stack or not
boolean isempty()
{
return q.isEmpty();
}
public static void main(String args[])
{
Squeue s = new Squeue();
s.push(1);
s.push(2);
s.push(3);
System.out.println("Top element is "+ s.top());
s.pop();
s.push(4);
s.push(5);
s.push(7);
s.pop();
System.out.print("Top element is "+s.top());
}
}