From a0965834a90cc8b6099e04085dd1c6447e6d6f1b Mon Sep 17 00:00:00 2001 From: Kamalakannan S <128273085+Kamal1023@users.noreply.github.com> Date: Sun, 12 Oct 2025 10:01:18 +0530 Subject: [PATCH 1/2] Add StackUsingLinkedList.java for #6715 --- .../stacks/StackUsingLinkedList.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java new file mode 100644 index 000000000000..df7d8aa44454 --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -0,0 +1,69 @@ +/** + * Stack implementation using singly linked list. + * Supports push, pop, peek, isEmpty, and size operations in O(1) time. + * + * Issue: #6715 + * Author: @Kamal1023 + */ +public class StackUsingLinkedList { + private Node top; + private int size; + + // Inner Node class + private static class Node { + T data; + Node next; + Node(T data) { + this.data = data; + this.next = null; + } + } + + //Pushes an element onto the stack + public void push(T data) { + Node node = new Node<>(data); + node.next = top; + top = node; + size++; + } + + // Removes and returns the top element of the stack + public T pop() { + if (isEmpty()) throw new RuntimeException("Stack is empty"); + T data = top.data; + top = top.next; + size--; + return data; + } + + /** Returns the top element without removing it */ + public T peek() { + if (isEmpty()) throw new RuntimeException("Stack is empty"); + return top.data; + } + + /** Checks if the stack is empty */ + public boolean isEmpty() { + return top == null; + } + + //Returns the number of elements in the stack + public int size() { + return size; + } + + // Demo/test for StackUsingLinkedList + public static void main(String[] args) { + StackUsingLinkedList stack = new StackUsingLinkedList<>(); + stack.push(10); + stack.push(20); + stack.push(30); + + System.out.println("Top: " + stack.peek()); // 30 + System.out.println("Size: " + stack.size()); // 3 + + while (!stack.isEmpty()) { + System.out.println("Pop: " + stack.pop()); + } + } +} From e90f77f8d604f3b73a1f39ac7fde9cb41ede2457 Mon Sep 17 00:00:00 2001 From: Kamalakannan S <128273085+Kamal1023@users.noreply.github.com> Date: Sun, 12 Oct 2025 10:54:33 +0530 Subject: [PATCH 2/2] Update StackUsingTwoQueues.java --- .../stacks/StackUsingTwoQueues.java | 134 ++++++++---------- 1 file changed, 56 insertions(+), 78 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java b/src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java index 5b1ca5d1d5a5..7848f983e6d4 100644 --- a/src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java +++ b/src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java @@ -1,91 +1,69 @@ -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. + * Stack implementation using singly linked list. + * Supports push, pop, peek, isEmpty, and size operations in O(1) time. + * + * Issue: #6715 + * Author: @Kamal1023 */ -public class StackUsingTwoQueues { +public class StackUsingLinkedList { + private Node top; + private int size; - private Queue mainQueue; - private Queue tempQueue; - - /** - * Constructs an empty stack using two queues. - */ - public StackUsingTwoQueues() { - mainQueue = new LinkedList<>(); - tempQueue = new LinkedList<>(); + // Inner Node class + private static class Node { + T data; + Node next; + Node(T data) { + this.data = data; + this.next = null; } + } - /** - * 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); + /** Pushes an element onto the stack */ + public void push(T data) { + Node node = new Node<>(data); + node.next = top; + top = node; + size++; + } - // Move all elements from the mainQueue to tempQueue to maintain LIFO order - while (!mainQueue.isEmpty()) { - tempQueue.add(mainQueue.remove()); - } + /** Removes and returns the top element of the stack */ + public T pop() { + if (isEmpty()) throw new RuntimeException("Stack is empty"); + T data = top.data; + top = top.next; + size--; + return data; + } - // Swap the names of the two queues - Queue swap = mainQueue; - mainQueue = tempQueue; - tempQueue = swap; // tempQueue is now empty - } + /** Returns the top element without removing it */ + public T peek() { + if (isEmpty()) throw new RuntimeException("Stack is empty"); + return top.data; + } - /** - * 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(); - } + /** Checks if the stack is empty */ + public boolean isEmpty() { + return top == null; + } - /** - * 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 the number of elements in the stack */ + public int size() { + return size; + } - /** - * Returns true if the stack is empty. - * - * @return true if the stack is empty; false otherwise. - */ - public boolean isEmpty() { - return mainQueue.isEmpty(); - } + /** Demo/test for StackUsingLinkedList */ + public static void main(String[] args) { + StackUsingLinkedList stack = new StackUsingLinkedList<>(); + stack.push(10); + stack.push(20); + stack.push(30); + + System.out.println("Top: " + stack.peek()); // 30 + System.out.println("Size: " + stack.size()); // 3 - /** - * Returns the number of elements in the stack. - * - * @return The size of the stack. - */ - public int size() { - return mainQueue.size(); + while (!stack.isEmpty()) { + System.out.println("Pop: " + stack.pop()); } + } }