-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstackUsingSQueue.java
More file actions
76 lines (61 loc) · 1.47 KB
/
stackUsingSQueue.java
File metadata and controls
76 lines (61 loc) · 1.47 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
//stack using single queue
public class stackUsingSQueue {
/*implementing queue using linked list */
Queue < Integer > q = new LinkedList < Integer > ();
/* Push operation of stack using queue*/
void push(int x) {
int size=q.size();
q.add(x);
for(int i=0;i<size;i++)
{
int temp=q.remove();
q.add(temp);
}
}
/*Removes the top element of the stack*/
int pop() {
//write your code herei
if(!q.isEmpty())
{
int x=q.remove();
return x;
}
return -1 ;
}
/*Returns the element at the top of the stack */
int top()
{
if(!q.isEmpty())
{
return q.peek();
}
return -1;
}
int size()
{
return q.size();
}
public static void main(String[] args) {
stackUsingSQueue obj = new stackUsingSQueue();
Scanner in = new Scanner(System.in);
/*Enter the number of elements you want to add in the stack */
int n = in .nextInt();
if(n==0)
{
System.out.println("Stack is empty");
}
else
{
/*Enter the elements of the stack */
for (int i = 0; i < n; i++) {
obj.push( in .nextInt());
}
for(int i=0;i<n;i++)
{
System.out.println("Top "+obj.top());
obj.pop();
}
}
}
}