|
1 | | -package com.thealgorithms.stacks; |
2 | | - |
3 | | -import java.util.LinkedList; |
4 | | -import java.util.NoSuchElementException; |
5 | | -import java.util.Queue; |
6 | | - |
7 | 1 | /** |
8 | | - * A class that implements a stack using two queues. |
9 | | - * This approach ensures that the stack's LIFO (Last In, First Out) behavior |
10 | | - * is maintained by utilizing two queues for storage. |
11 | | - * The mainQueue is used to store the elements of the stack, while the tempQueue |
12 | | - * is used to temporarily store elements during the push operation. |
| 2 | + * Stack implementation using singly linked list. |
| 3 | + * Supports push, pop, peek, isEmpty, and size operations in O(1) time. |
| 4 | + * |
| 5 | + * Issue: #6715 |
| 6 | + * Author: @Kamal1023 |
13 | 7 | */ |
14 | | -public class StackUsingTwoQueues { |
| 8 | +public class StackUsingLinkedList<T> { |
| 9 | + private Node<T> top; |
| 10 | + private int size; |
15 | 11 |
|
16 | | - private Queue<Integer> mainQueue; |
17 | | - private Queue<Integer> tempQueue; |
18 | | - |
19 | | - /** |
20 | | - * Constructs an empty stack using two queues. |
21 | | - */ |
22 | | - public StackUsingTwoQueues() { |
23 | | - mainQueue = new LinkedList<>(); |
24 | | - tempQueue = new LinkedList<>(); |
| 12 | + // Inner Node class |
| 13 | + private static class Node<T> { |
| 14 | + T data; |
| 15 | + Node<T> next; |
| 16 | + Node(T data) { |
| 17 | + this.data = data; |
| 18 | + this.next = null; |
25 | 19 | } |
| 20 | + } |
26 | 21 |
|
27 | | - /** |
28 | | - * Pushes an element onto the top of the stack. |
29 | | - * The newly pushed element becomes the top of the stack. |
30 | | - * |
31 | | - * @param item The element to be pushed onto the stack. |
32 | | - */ |
33 | | - public void push(int item) { |
34 | | - tempQueue.add(item); |
| 22 | + /** Pushes an element onto the stack */ |
| 23 | + public void push(T data) { |
| 24 | + Node<T> node = new Node<>(data); |
| 25 | + node.next = top; |
| 26 | + top = node; |
| 27 | + size++; |
| 28 | + } |
35 | 29 |
|
36 | | - // Move all elements from the mainQueue to tempQueue to maintain LIFO order |
37 | | - while (!mainQueue.isEmpty()) { |
38 | | - tempQueue.add(mainQueue.remove()); |
39 | | - } |
| 30 | + /** Removes and returns the top element of the stack */ |
| 31 | + public T pop() { |
| 32 | + if (isEmpty()) throw new RuntimeException("Stack is empty"); |
| 33 | + T data = top.data; |
| 34 | + top = top.next; |
| 35 | + size--; |
| 36 | + return data; |
| 37 | + } |
40 | 38 |
|
41 | | - // Swap the names of the two queues |
42 | | - Queue<Integer> swap = mainQueue; |
43 | | - mainQueue = tempQueue; |
44 | | - tempQueue = swap; // tempQueue is now empty |
45 | | - } |
| 39 | + /** Returns the top element without removing it */ |
| 40 | + public T peek() { |
| 41 | + if (isEmpty()) throw new RuntimeException("Stack is empty"); |
| 42 | + return top.data; |
| 43 | + } |
46 | 44 |
|
47 | | - /** |
48 | | - * Removes and returns the element at the top of the stack. |
49 | | - * Throws an exception if the stack is empty. |
50 | | - * |
51 | | - * @return The element at the top of the stack. |
52 | | - * @throws NoSuchElementException if the stack is empty. |
53 | | - */ |
54 | | - public int pop() { |
55 | | - if (mainQueue.isEmpty()) { |
56 | | - throw new NoSuchElementException("Stack is empty"); |
57 | | - } |
58 | | - return mainQueue.remove(); |
59 | | - } |
| 45 | + /** Checks if the stack is empty */ |
| 46 | + public boolean isEmpty() { |
| 47 | + return top == null; |
| 48 | + } |
60 | 49 |
|
61 | | - /** |
62 | | - * Returns the element at the top of the stack without removing it. |
63 | | - * Returns null if the stack is empty. |
64 | | - * |
65 | | - * @return The element at the top of the stack, or null if the stack is empty. |
66 | | - */ |
67 | | - public Integer peek() { |
68 | | - if (mainQueue.isEmpty()) { |
69 | | - return null; |
70 | | - } |
71 | | - return mainQueue.peek(); |
72 | | - } |
| 50 | + /** Returns the number of elements in the stack */ |
| 51 | + public int size() { |
| 52 | + return size; |
| 53 | + } |
73 | 54 |
|
74 | | - /** |
75 | | - * Returns true if the stack is empty. |
76 | | - * |
77 | | - * @return true if the stack is empty; false otherwise. |
78 | | - */ |
79 | | - public boolean isEmpty() { |
80 | | - return mainQueue.isEmpty(); |
81 | | - } |
| 55 | + /** Demo/test for StackUsingLinkedList */ |
| 56 | + public static void main(String[] args) { |
| 57 | + StackUsingLinkedList<Integer> stack = new StackUsingLinkedList<>(); |
| 58 | + stack.push(10); |
| 59 | + stack.push(20); |
| 60 | + stack.push(30); |
| 61 | + |
| 62 | + System.out.println("Top: " + stack.peek()); // 30 |
| 63 | + System.out.println("Size: " + stack.size()); // 3 |
82 | 64 |
|
83 | | - /** |
84 | | - * Returns the number of elements in the stack. |
85 | | - * |
86 | | - * @return The size of the stack. |
87 | | - */ |
88 | | - public int size() { |
89 | | - return mainQueue.size(); |
| 65 | + while (!stack.isEmpty()) { |
| 66 | + System.out.println("Pop: " + stack.pop()); |
90 | 67 | } |
| 68 | + } |
91 | 69 | } |
0 commit comments