-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsing_Two_Stacks.java
More file actions
79 lines (67 loc) · 2.39 KB
/
QueueUsing_Two_Stacks.java
File metadata and controls
79 lines (67 loc) · 2.39 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
77
78
79
/******************************************************************************
* Compilation: javac QueueWithTwoStacks.java
* Execution: java QueueWithTwoStacks < input.txt
* Dependencies: StdIn.java StdOut.java
* Data files: https://algs4.cs.princeton.edu/13stacks/tobe.txt
*
* A generic queue, implemented using two stacks.
*
* % java QueueWithTwoStacks < tobe.txt
* to be or not to be (2 left on queue)
*
******************************************************************************/
import java.util.NoSuchElementException;
import java.util.Stack;
import java.util.regex.Pattern;
import java.util.Scanner;
public class QueueUsing_Two_Stacks<Item> {
private Stack<Item> stack1; // back of queue
private Stack<Item> stack2; // front of queue
// create an empty queue
public QueueUsing_Two_Stacks() {
stack1 = new Stack<Item>();
stack2 = new Stack<Item>();
}
// move all items from stack1 to stack2
private void moveStack1ToStack2() {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
// is the queue empty?
public boolean isEmpty() {
return stack1.isEmpty() && stack2.isEmpty();
}
// return the number of items in the queue.
public int size() {
return stack1.size() + stack2.size();
}
// return the item least recently added to the queue.
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty()) moveStack1ToStack2();
return stack2.peek();
}
// add the item to the queue
public void enqueue(Item item) {
stack1.push(item);
}
// remove and return the item on the queue least recently added
public Item dequeue() {
if (isEmpty()) throw new NoSuchElementException("Queue underflow");
if (stack2.isEmpty()) moveStack1ToStack2();
return stack2.pop();
}
// a test client
public static void main(String[] args) {
QueueUsing_Two_Stacks<String> q = new QueueUsing_Two_Stacks<String>();
int i = 5;
while (i == 0) {
Scanner sc = new Scanner(System.in);
String item = sc.next();
if (!item.equals("-")) q.enqueue(item);
else if (!q.isEmpty()) System.out.print(q.dequeue() + " ");
--i;
}
System.out.println("(" + q.size() + " left on queue)");
}
}