-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueWithStack.java
More file actions
62 lines (52 loc) · 1.43 KB
/
Copy pathQueueWithStack.java
File metadata and controls
62 lines (52 loc) · 1.43 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
/* Name: Rafael Neves Moraes
*
* Description: Queue with Stack algorithm.
*
* Written: 5/06/2019
*
* % javac QueueWithStack.java
* % java QueueWithStack
*
**************************************************************************** */
public class QueueWithStack<Item>
{
private Stack<Item> stackPush = null;
private Stack<Item> stackPop = null;
public QueueWithStack () {
stackPush = new Stack<Item>();
stackPop = new Stack<Item>();
}
public boolean isEmpty()
{
return stackPush.isEmpty() && stackPop.isEmpty();
}
public void enqueue(Item item)
{
stackPush.push(item);
}
public Item dequeue()
{
if (stackPop.isEmpty())
while (!stackPush.isEmpty())
stackPop.push(stackPush.pop());
return stackPop.pop();
}
public static void main (String args[]) {
QueueWithStack<Integer> queue = new QueueWithStack<Integer>();
queue.enqueue(3);
queue.enqueue(5);
queue.enqueue(10);
queue.enqueue(1);
queue.enqueue(4);
queue.enqueue(8);
queue.enqueue(20);
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
System.out.println("-------------------");
}
}