@@ -199,135 +199,6 @@ public static void main(String[] args) {
199199 System .out .println ("Caught exception: " + e .getMessage ());
200200 }
201201
202- /**
203- * Checks if this stack is empty.
204- * Time Complexity: O(1)
205- *
206- * @return {@code true} if this stack contains no elements, {@code false} otherwise
207- */
208- public boolean isEmpty () {
209- return top == null ;
210- }
211-
212- /**
213- * Returns the number of elements in this stack.
214- * Time Complexity: O(1)
215- *
216- * @return the number of elements in this stack
217- */
218- public int size () {
219- return size ;
220- }
221-
222- /**
223- * Removes all elements from this stack.
224- * Time Complexity: O(1)
225- */
226- public void clear () {
227- top = null ;
228- size = 0 ;
229- }
230-
231- /**
232- * Returns a string representation of this stack.
233- * The string representation consists of a list of the stack's elements
234- * from top to bottom, enclosed in square brackets ("[]").
235- *
236- * @return a string representation of this stack
237- */
238- @ Override
239- public String toString () {
240- if (isEmpty ()) {
241- return "[]" ;
242- }
243-
244- StringBuilder sb = new StringBuilder ("[" );
245- Node <T > current = top ;
246- while (current != null ) {
247- sb .append (current .data );
248- if (current .next != null ) {
249- sb .append (", " );
250- }
251- current = current .next ;
252- }
253- sb .append ("]" );
254- return sb .toString ();
255- }
256-
257- /**
258- * Demonstration of the StackUsingLinkedList implementation.
259- *
260- * @param args command line arguments (not used)
261- */
262- public static void main (String [] args ) {
263- // Example 1: Stack of Integers
264- System .out .println ("=== Integer Stack Demo ===" );
265- StackUsingLinkedList <Integer > intStack = new StackUsingLinkedList <>();
266-
267- // Push elements
268- intStack .push (10 );
269- intStack .push (20 );
270- intStack .push (30 );
271- intStack .push (40 );
272- System .out .println ("Stack after pushing 10, 20, 30, 40: " + intStack );
273- System .out .println ("Size: " + intStack .size ());
274-
275- // Peek
276- System .out .println ("Top element (peek): " + intStack .peek ());
277-
278- // Pop elements
279- System .out .println ("Popped: " + intStack .pop ());
280- System .out .println ("Popped: " + intStack .pop ());
281- System .out .println ("Stack after popping twice: " + intStack );
282- System .out .println ("Size: " + intStack .size ());
283-
284- // Check if empty
285- System .out .println ("Is stack empty? " + intStack .isEmpty ());
286-
287- // Pop remaining elements
288- intStack .pop ();
289- intStack .pop ();
290- System .out .println ("Is stack empty after popping all? " + intStack .isEmpty ());
291-
292- // Example 2: Stack of Strings
293- System .out .println ("\n === String Stack Demo ===" );
294- StackUsingLinkedList <String > stringStack = new StackUsingLinkedList <>();
295-
296- stringStack .push ("Hello" );
297- stringStack .push ("World" );
298- stringStack .push ("Java" );
299- System .out .println ("Stack: " + stringStack );
300- System .out .println ("Top: " + stringStack .peek ());
301- System .out .println ("Popped: " + stringStack .pop ());
302- System .out .println ("Stack after pop: " + stringStack );
303-
304- // Example 3: Exception handling
305- System .out .println ("\n === Exception Handling Demo ===" );
306- StackUsingLinkedList <Integer > emptyStack = new StackUsingLinkedList <>();
307- try {
308- emptyStack .pop ();
309- } catch (IllegalStateException e ) {
310- System .out .println ("Caught exception: " + e .getMessage ());
311- }
312-
313- try {
314- emptyStack .peek ();
315- } catch (IllegalStateException e ) {
316- System .out .println ("Caught exception: " + e .getMessage ());
317- }
318-
319- // Example 4: Clear operation
320- System .out .println ("\n === Clear Operation Demo ===" );
321- StackUsingLinkedList <Integer > stack = new StackUsingLinkedList <>();
322- stack .push (1 );
323- stack .push (2 );
324- stack .push (3 );
325- System .out .println ("Stack before clear: " + stack );
326- stack .clear ();
327- System .out .println ("Stack after clear: " + stack );
328- System .out .println ("Is empty? " + stack .isEmpty ());
329- }
330-
331202 // Example 4: Clear operation
332203 System .out .println ("\n === Clear Operation Demo ===" );
333204 StackUsingLinkedList <Integer > stack = new StackUsingLinkedList <>();
0 commit comments