-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathStackUsingTwoQueues.java
91 lines (80 loc) · 2.49 KB
/
StackUsingTwoQueues.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.thealgorithms.stacks;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;
/**
* A class that implements a stack using two queues.
* This approach ensures that the stack's LIFO (Last In, First Out) behavior
* is maintained by utilizing two queues for storage.
* The mainQueue is used to store the elements of the stack, while the tempQueue
* is used to temporarily store elements during the push operation.
*/
public class StackUsingTwoQueues {
private Queue<Integer> mainQueue;
private Queue<Integer> tempQueue;
/**
* Constructs an empty stack using two queues.
*/
public StackUsingTwoQueues() {
mainQueue = new LinkedList<>();
tempQueue = new LinkedList<>();
}
/**
* Pushes an element onto the top of the stack.
* The newly pushed element becomes the top of the stack.
*
* @param item The element to be pushed onto the stack.
*/
public void push(int item) {
tempQueue.add(item);
// Move all elements from the mainQueue to tempQueue to maintain LIFO order
while (!mainQueue.isEmpty()) {
tempQueue.add(mainQueue.remove());
}
// Swap the names of the two queues
Queue<Integer> swap = mainQueue;
mainQueue = tempQueue;
tempQueue = swap; // tempQueue is now empty
}
/**
* Removes and returns the element at the top of the stack.
* Throws an exception if the stack is empty.
*
* @return The element at the top of the stack.
* @throws NoSuchElementException if the stack is empty.
*/
public int pop() {
if (mainQueue.isEmpty()) {
throw new NoSuchElementException("Stack is empty");
}
return mainQueue.remove();
}
/**
* Returns the element at the top of the stack without removing it.
* Returns null if the stack is empty.
*
* @return The element at the top of the stack, or null if the stack is empty.
*/
public Integer peek() {
if (mainQueue.isEmpty()) {
return null;
}
return mainQueue.peek();
}
/**
* Returns true if the stack is empty.
*
* @return true if the stack is empty; false otherwise.
*/
public boolean isEmpty() {
return mainQueue.isEmpty();
}
/**
* Returns the number of elements in the stack.
*
* @return The size of the stack.
*/
public int size() {
return mainQueue.size();
}
}