Skip to content

Commit c3b0fd1

Browse files
Add Stack implementation using Linked List
1 parent b1aa896 commit c3b0fd1

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.thealgorithms.stacks;
2+
3+
/**
4+
* Stack implementation using a singly linked list.
5+
*
6+
* @param <T> the type of elements stored in the stack
7+
*
8+
* Operations:
9+
* - push(T data): Insert an element onto the stack (O(1))
10+
* - pop(): Remove and return the top element (O(1))
11+
* - peek(): View the top element without removing it (O(1))
12+
* - isEmpty(): Check if the stack is empty (O(1))
13+
* - size(): Return the number of elements (O(1))
14+
*/
15+
public class StackUsingLinkedList<T> {
16+
17+
/** Inner class representing a node in the linked list */
18+
private class Node {
19+
T data;
20+
Node next;
21+
22+
Node(T data) {
23+
this.data = data;
24+
this.next = null;
25+
}
26+
}
27+
28+
private Node top; // top of the stack
29+
private int size; // number of elements in the stack
30+
31+
/** Constructor: Initializes an empty stack */
32+
public StackUsingLinkedList() {
33+
top = null;
34+
size = 0;
35+
}
36+
37+
/**
38+
* Pushes an element onto the top of the stack.
39+
* @param data element to be pushed
40+
*/
41+
public void push(T data) {
42+
Node newNode = new Node(data);
43+
newNode.next = top;
44+
top = newNode;
45+
size++;
46+
}
47+
48+
/**
49+
* Removes and returns the top element of the stack.
50+
* @return the popped element
51+
* @throws IllegalStateException if the stack is empty
52+
*/
53+
public T pop() {
54+
if (isEmpty()) {
55+
throw new IllegalStateException("Stack is empty. Cannot pop.");
56+
}
57+
T data = top.data;
58+
top = top.next;
59+
size--;
60+
return data;
61+
}
62+
63+
/**
64+
* Returns the top element without removing it.
65+
* @return the top element
66+
* @throws IllegalStateException if the stack is empty
67+
*/
68+
public T peek() {
69+
if (isEmpty()) {
70+
throw new IllegalStateException("Stack is empty. Cannot peek.");
71+
}
72+
return top.data;
73+
}
74+
75+
/**
76+
* Checks whether the stack is empty.
77+
* @return true if empty, false otherwise
78+
*/
79+
public boolean isEmpty() {
80+
return top == null;
81+
}
82+
83+
/**
84+
* Returns the number of elements in the stack.
85+
* @return the size of the stack
86+
*/
87+
public int size() {
88+
return size;
89+
}
90+
91+
/**
92+
* Prints the stack elements from top to bottom.
93+
*/
94+
public void printStack() {
95+
if (isEmpty()) {
96+
System.out.println("Stack is empty.");
97+
return;
98+
}
99+
Node current = top;
100+
System.out.print("Stack (top -> bottom): ");
101+
while (current != null) {
102+
System.out.print(current.data + " ");
103+
current = current.next;
104+
}
105+
System.out.println();
106+
}
107+
108+
/**
109+
* Example usage
110+
*/
111+
public static void main(String[] args) {
112+
StackUsingLinkedList<Integer> stack = new StackUsingLinkedList<>();
113+
114+
stack.push(10);
115+
stack.push(20);
116+
stack.push(30);
117+
118+
stack.printStack(); // Output: 30 20 10
119+
120+
System.out.println("Top element: " + stack.peek()); // 30
121+
System.out.println("Popped: " + stack.pop()); // 30
122+
stack.printStack(); // 20 10
123+
System.out.println("Size: " + stack.size()); // 2
124+
}
125+
}

0 commit comments

Comments
 (0)