Skip to content

Commit db02e78

Browse files
committed
fix: resolve merge conflict
2 parents 5074d09 + 6e4c0fd commit db02e78

File tree

2 files changed

+192
-62
lines changed

2 files changed

+192
-62
lines changed

src/main/java/com/thealgorithms/datastructures/stacks/StackUsingLinkedList.java

Lines changed: 191 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,196 @@
77
* @param <T> the type of elements held in this stack
88
* @author Your Name
99
*/
10-
public class StackUsingLinkedList<T> {
10+
public class StackUsingLinkedList < T > {
11+
12+
/**
13+
* Inner class representing a node in the linked list.
14+
*/
15+
private static class Node < T > {
16+
T data;
17+
Node < T > next;
1118

1219
/**
13-
* Inner class representing a node in the linked list.
20+
* Constructs a new node with the given data.
21+
*
22+
* @param data the data to store in this node
1423
*/
15-
private static class Node<T> {
16-
T data;
17-
Node<T> next;
18-
19-
/**
20-
* Constructs a new node with the given data.
21-
*
22-
* @param data the data to store in this node
23-
*/
24-
Node(T data) {
25-
this.data = data;
26-
this.next = null;
27-
}
24+
Node(T data) {
25+
this.data = data;
26+
this.next = null;
2827
}
28+
}
2929

30-
private Node<T> top;
31-
private int size;
30+
private Node < T > top;
31+
private int size;
3232

33-
/**
34-
* Constructs an empty stack.
35-
*/
36-
public StackUsingLinkedList() {
37-
this.top = null;
38-
this.size = 0;
33+
/**
34+
* Constructs an empty stack.
35+
*/
36+
public StackUsingLinkedList() {
37+
this.top = null;
38+
this.size = 0;
39+
}
40+
41+
/**
42+
* Pushes an element onto the top of this stack.
43+
* Time Complexity: O(1)
44+
*
45+
* @param data the element to be pushed onto this stack
46+
*/
47+
public void push(T data) {
48+
Node < T > newNode = new Node < > (data);
49+
newNode.next = top;
50+
top = newNode;
51+
size++;
52+
}
53+
54+
/**
55+
* Removes and returns the element at the top of this stack.
56+
* Time Complexity: O(1)
57+
*
58+
* @return the element at the top of this stack
59+
* @throws IllegalStateException if this stack is empty
60+
*/
61+
public T pop() {
62+
if (isEmpty()) {
63+
throw new IllegalStateException("Stack is empty. Cannot pop element.");
3964
}
65+
T data = top.data;
66+
top = top.next;
67+
size--;
68+
return data;
69+
}
4070

41-
/**
42-
* Pushes an element onto the top of this stack.
43-
* Time Complexity: O(1)
44-
*
45-
* @param data the element to be pushed onto this stack
46-
*/
47-
public void push(T data) {
48-
Node<T> newNode = new Node<>(data);
49-
newNode.next = top;
50-
top = newNode;
51-
size++;
71+
/**
72+
* Returns the element at the top of this stack without removing it.
73+
* Time Complexity: O(1)
74+
*
75+
* @return the element at the top of this stack
76+
* @throws IllegalStateException if this stack is empty
77+
*/
78+
public T peek() {
79+
if (isEmpty()) {
80+
throw new IllegalStateException("Stack is empty. Cannot peek element.");
5281
}
82+
return top.data;
83+
}
5384

54-
/**
55-
* Removes and returns the element at the top of this stack.
56-
* Time Complexity: O(1)
57-
*
58-
* @return the element at the top of this stack
59-
* @throws IllegalStateException if this stack is empty
60-
*/
61-
public T pop() {
62-
if (isEmpty()) {
63-
throw new IllegalStateException("Stack is empty. Cannot pop element.");
64-
}
65-
T data = top.data;
66-
top = top.next;
67-
size--;
68-
return data;
85+
/**
86+
* Checks if this stack is empty.
87+
* Time Complexity: O(1)
88+
*
89+
* @return {@code true} if this stack contains no elements, {@code false} otherwise
90+
*/
91+
public boolean isEmpty() {
92+
return top == null;
93+
}
94+
95+
/**
96+
* Returns the number of elements in this stack.
97+
* Time Complexity: O(1)
98+
*
99+
* @return the number of elements in this stack
100+
*/
101+
public int size() {
102+
return size;
103+
}
104+
105+
/**
106+
* Removes all elements from this stack.
107+
* Time Complexity: O(1)
108+
*/
109+
public void clear() {
110+
top = null;
111+
size = 0;
112+
}
113+
114+
/**
115+
* Returns a string representation of this stack.
116+
* The string representation consists of a list of the stack's elements
117+
* from top to bottom, enclosed in square brackets ("[]").
118+
*
119+
* @return a string representation of this stack
120+
*/
121+
@Override
122+
public String toString() {
123+
if (isEmpty()) {
124+
return "[]";
69125
}
70126

71-
/**
72-
* Returns the element at the top of this stack without removing it.
73-
* Time Complexity: O(1)
74-
*
75-
* @return the element at the top of this stack
76-
* @throws IllegalStateException if this stack is empty
77-
*/
78-
public T peek() {
79-
if (isEmpty()) {
80-
throw new IllegalStateException("Stack is empty. Cannot peek element.");
81-
}
82-
return top.data;
127+
StringBuilder sb = new StringBuilder("[");
128+
Node < T > current = top;
129+
while (current != null) {
130+
sb.append(current.data);
131+
if (current.next != null) {
132+
sb.append(", ");
133+
}
134+
current = current.next;
135+
}
136+
sb.append("]");
137+
return sb.toString();
138+
}
139+
140+
/**
141+
* Demonstration of the StackUsingLinkedList implementation.
142+
*
143+
* @param args command line arguments (not used)
144+
*/
145+
public static void main(String[] args) {
146+
// Example 1: Stack of Integers
147+
System.out.println("=== Integer Stack Demo ===");
148+
StackUsingLinkedList < Integer > intStack = new StackUsingLinkedList < > ();
149+
150+
// Push elements
151+
intStack.push(10);
152+
intStack.push(20);
153+
intStack.push(30);
154+
intStack.push(40);
155+
System.out.println("Stack after pushing 10, 20, 30, 40: " + intStack);
156+
System.out.println("Size: " + intStack.size());
157+
158+
// Peek
159+
System.out.println("Top element (peek): " + intStack.peek());
160+
161+
// Pop elements
162+
System.out.println("Popped: " + intStack.pop());
163+
System.out.println("Popped: " + intStack.pop());
164+
System.out.println("Stack after popping twice: " + intStack);
165+
System.out.println("Size: " + intStack.size());
166+
167+
// Check if empty
168+
System.out.println("Is stack empty? " + intStack.isEmpty());
169+
170+
// Pop remaining elements
171+
intStack.pop();
172+
intStack.pop();
173+
System.out.println("Is stack empty after popping all? " + intStack.isEmpty());
174+
175+
// Example 2: Stack of Strings
176+
System.out.println("\n=== String Stack Demo ===");
177+
StackUsingLinkedList < String > stringStack = new StackUsingLinkedList < > ();
178+
179+
stringStack.push("Hello");
180+
stringStack.push("World");
181+
stringStack.push("Java");
182+
System.out.println("Stack: " + stringStack);
183+
System.out.println("Top: " + stringStack.peek());
184+
System.out.println("Popped: " + stringStack.pop());
185+
System.out.println("Stack after pop: " + stringStack);
186+
187+
// Example 3: Exception handling
188+
System.out.println("\n=== Exception Handling Demo ===");
189+
StackUsingLinkedList < Integer > emptyStack = new StackUsingLinkedList < > ();
190+
try {
191+
emptyStack.pop();
192+
} catch (IllegalStateException e) {
193+
System.out.println("Caught exception: " + e.getMessage());
194+
}
195+
196+
try {
197+
emptyStack.peek();
198+
} catch (IllegalStateException e) {
199+
System.out.println("Caught exception: " + e.getMessage());
83200
}
84201

85202
/**
@@ -210,4 +327,17 @@ public static void main(String[] args) {
210327
System.out.println("Stack after clear: " + stack);
211328
System.out.println("Is empty? " + stack.isEmpty());
212329
}
330+
331+
// Example 4: Clear operation
332+
System.out.println("\n=== Clear Operation Demo ===");
333+
StackUsingLinkedList < Integer > stack = new StackUsingLinkedList < > ();
334+
stack.push(1);
335+
stack.push(2);
336+
stack.push(3);
337+
System.out.println("Stack before clear: " + stack);
338+
stack.clear();
339+
System.out.println("Stack after clear: " + stack);
340+
System.out.println("Is empty? " + stack.isEmpty());
341+
}
342+
213343
}

src/main/java/com/thealgorithms/maths/Area.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static double surfaceAreaCylinder(final double radius, final double heigh
9696
throw new IllegalArgumentException(POSITIVE_RADIUS);
9797
}
9898
if (height <= 0) {
99-
throw new IllegalArgumentException(POSITIVE_RADIUS);
99+
throw new IllegalArgumentException(POSITIVE_HEIGHT);
100100
}
101101
return 2 * (Math.PI * radius * radius + Math.PI * radius * height);
102102
}

0 commit comments

Comments
 (0)