-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingTwoQueues.java
More file actions
52 lines (50 loc) · 1.63 KB
/
StackUsingTwoQueues.java
File metadata and controls
52 lines (50 loc) · 1.63 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
package Ds.Achievers;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class StackUsingTwoQueues {
static Queue<Integer> queue1=new LinkedList<Integer>();
static Queue<Integer> queue2=new LinkedList<Integer>();
void push(int element){
queue2.add(element);
while (!queue1.isEmpty()){
queue2.add(queue1.remove());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
void pop(){
if (queue1.isEmpty())
return;
System.out.println("Removed element is: "+queue1.remove());
}
public static void main(String[] args) {
StackUsingTwoQueues stack =new StackUsingTwoQueues();
Scanner sc=new Scanner(System.in);
System.out.println("***STACK***\n1. Push\n2. Pop\n-1 to exit");
int choice=sc.nextInt();
int element=0;
while (choice!=-1){
switch (choice){
case 1:
System.out.println("Enter element to insert(-1 to exit)");
element=sc.nextInt();
while (element!=-1){
stack.push(element);
System.out.println("Enter again");
element=sc.nextInt();
}
break;
case 2:
System.out.println("Deleting");
stack.pop();
break;
default:
System.out.println("Wrong input! enter again");
break;
}
choice=sc.nextInt();
}
}
}