From c3b0fd184e7382469a6f1f077a3c0cb7d6b941bd Mon Sep 17 00:00:00 2001 From: "thisispraphullverma@gmail.com" Date: Thu, 9 Oct 2025 18:55:12 +0530 Subject: [PATCH 1/3] Add Stack implementation using Linked List --- .../stacks/StackUsingLinkedList.java | 125 ++++++++++++++++++ 1 file changed, 125 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..eb02acf0707c --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -0,0 +1,125 @@ +package com.thealgorithms.stacks; + +/** + * Stack implementation using a singly linked list. + * + * @param the type of elements stored in the stack + * + * Operations: + * - push(T data): Insert an element onto the stack (O(1)) + * - pop(): Remove and return the top element (O(1)) + * - peek(): View the top element without removing it (O(1)) + * - isEmpty(): Check if the stack is empty (O(1)) + * - size(): Return the number of elements (O(1)) + */ +public class StackUsingLinkedList { + + /** Inner class representing a node in the linked list */ + private class Node { + T data; + Node next; + + Node(T data) { + this.data = data; + this.next = null; + } + } + + private Node top; // top of the stack + private int size; // number of elements in the stack + + /** Constructor: Initializes an empty stack */ + public StackUsingLinkedList() { + top = null; + size = 0; + } + + /** + * Pushes an element onto the top of the stack. + * @param data element to be pushed + */ + public void push(T data) { + Node newNode = new Node(data); + newNode.next = top; + top = newNode; + size++; + } + + /** + * Removes and returns the top element of the stack. + * @return the popped element + * @throws IllegalStateException if the stack is empty + */ + public T pop() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty. Cannot pop."); + } + T data = top.data; + top = top.next; + size--; + return data; + } + + /** + * Returns the top element without removing it. + * @return the top element + * @throws IllegalStateException if the stack is empty + */ + public T peek() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty. Cannot peek."); + } + return top.data; + } + + /** + * Checks whether the stack is empty. + * @return true if empty, false otherwise + */ + public boolean isEmpty() { + return top == null; + } + + /** + * Returns the number of elements in the stack. + * @return the size of the stack + */ + public int size() { + return size; + } + + /** + * Prints the stack elements from top to bottom. + */ + public void printStack() { + if (isEmpty()) { + System.out.println("Stack is empty."); + return; + } + Node current = top; + System.out.print("Stack (top -> bottom): "); + while (current != null) { + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + /** + * Example usage + */ + public static void main(String[] args) { + StackUsingLinkedList stack = new StackUsingLinkedList<>(); + + stack.push(10); + stack.push(20); + stack.push(30); + + stack.printStack(); // Output: 30 20 10 + + System.out.println("Top element: " + stack.peek()); // 30 + System.out.println("Popped: " + stack.pop()); // 30 + stack.printStack(); // 20 10 + System.out.println("Size: " + stack.size()); // 2 + } +} From 2cda368e76b06c2b6494209d8105c6a5c8e419f5 Mon Sep 17 00:00:00 2001 From: "thisispraphullverma@gmail.com" Date: Thu, 9 Oct 2025 19:27:58 +0530 Subject: [PATCH 2/3] Add Stack implementation using Linked List --- .../stacks/StackUsingLinkedList.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java index eb02acf0707c..85bff6cfb032 100644 --- a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -2,9 +2,9 @@ /** * Stack implementation using a singly linked list. - * + * * @param the type of elements stored in the stack - * + * * Operations: * - push(T data): Insert an element onto the stack (O(1)) * - pop(): Remove and return the top element (O(1)) @@ -25,8 +25,8 @@ private class Node { } } - private Node top; // top of the stack - private int size; // number of elements in the stack + private Node top; // top of the stack + private int size; // number of elements in the stack /** Constructor: Initializes an empty stack */ public StackUsingLinkedList() { @@ -34,8 +34,8 @@ public StackUsingLinkedList() { size = 0; } - /** - * Pushes an element onto the top of the stack. + /** + * Pushes an element onto the top of the stack. * @param data element to be pushed */ public void push(T data) { @@ -45,7 +45,7 @@ public void push(T data) { size++; } - /** + /** * Removes and returns the top element of the stack. * @return the popped element * @throws IllegalStateException if the stack is empty @@ -60,7 +60,7 @@ public T pop() { return data; } - /** + /** * Returns the top element without removing it. * @return the top element * @throws IllegalStateException if the stack is empty @@ -72,7 +72,7 @@ public T peek() { return top.data; } - /** + /** * Checks whether the stack is empty. * @return true if empty, false otherwise */ @@ -80,7 +80,7 @@ public boolean isEmpty() { return top == null; } - /** + /** * Returns the number of elements in the stack. * @return the size of the stack */ @@ -88,7 +88,7 @@ public int size() { return size; } - /** + /** * Prints the stack elements from top to bottom. */ public void printStack() { @@ -105,8 +105,8 @@ public void printStack() { System.out.println(); } - /** - * Example usage + /** + * Example usage */ public static void main(String[] args) { StackUsingLinkedList stack = new StackUsingLinkedList<>(); @@ -115,11 +115,11 @@ public static void main(String[] args) { stack.push(20); stack.push(30); - stack.printStack(); // Output: 30 20 10 + stack.printStack(); // Output: 30 20 10 System.out.println("Top element: " + stack.peek()); // 30 - System.out.println("Popped: " + stack.pop()); // 30 - stack.printStack(); // 20 10 - System.out.println("Size: " + stack.size()); // 2 + System.out.println("Popped: " + stack.pop()); // 30 + stack.printStack(); // 20 10 + System.out.println("Size: " + stack.size()); // 2 } } From 72c1467b90591a0d2626150bab5627bf4b06fbdc Mon Sep 17 00:00:00 2001 From: "thisispraphullverma@gmail.com" Date: Thu, 9 Oct 2025 19:46:25 +0530 Subject: [PATCH 3/3] feat #6715 Add Stack Implementation Using Linked List in Java --- .../stacks/StackUsingLinkedList.java | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java index 85bff6cfb032..e03df28ae35b 100644 --- a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -104,22 +104,4 @@ public void printStack() { } System.out.println(); } - - /** - * Example usage - */ - public static void main(String[] args) { - StackUsingLinkedList stack = new StackUsingLinkedList<>(); - - stack.push(10); - stack.push(20); - stack.push(30); - - stack.printStack(); // Output: 30 20 10 - - System.out.println("Top element: " + stack.peek()); // 30 - System.out.println("Popped: " + stack.pop()); // 30 - stack.printStack(); // 20 10 - System.out.println("Size: " + stack.size()); // 2 - } }