Question: Repeat Exercise 28, but add the methods to the LinkedStack class. Here is question 28: I am not sure how to use linked list strategies

  1. Repeat Exercise 28, but add the methods to the LinkedStack class. Here is question 28:

I am not sure how to use linked list strategies to implement these methods

 Repeat Exercise 28, but add the methods to the LinkedStack class.

This is the Linked Stack file

7 package ch02.stacks; 8 9 import support.LLNode; 10 11 public class LinkedStack implements StackInterface 12 { 13 protected LLNode top; // reference to the top of this stack 14 15 public LinkedStack() 16 { 17 top = null; 18 } 19 20 public void push(T element) 21 // Places element at the top of this stack. 22 { 23 LLNode newNode = new LLNode(element); 24 newNode.setLink(top); 25 top = newNode; 26 } 27 28 public void pop() 29 // Throws StackUnderflowException if this stack is empty, 30 // otherwise removes top element from this stack. 31 { 32 if (isEmpty()) 33 throw new StackUnderflowException("Pop attempted on an empty stack."); 34 else 35 top = top.getLink(); 36 } 37 38 public T top() 39 // Throws StackUnderflowException if this stack is empty, 40 // otherwise returns top element of this stack. 41 { 42 if (isEmpty()) 43 throw new StackUnderflowException("Top attempted on an empty stack."); 44 else 45 return top.getInfo(); 46 } 47 48 public boolean isEmpty() 49 // Returns true if this stack is empty, otherwise returns false. 50 { 51 return (top == null); 52 } 53 54 public boolean isFull() 55 // Returns false - a linked stack is never full 56 { 57 return false; 58 } 59 60 } 61 62

28. Add the following methods to the ArrayBounded Stack class, and create a test driver for each to show that they work correctly. In order to practice your array related cod- ing skills, code each of these methods by accessing the internal variables of the Ar- rayBounded Stack, not by calling the previously defined public methods of the class. a. String toString()-creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method. b. int size()-returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method. C. void popSome(int count)-removes the top count elements from the stack; throws StackUnderflowException if there are less than count ele- ments on the stack d. boolean swapStart()-if there are less than two elements on the stack re- turns false; otherwise it reverses the order of the top two elements on the stack and returns true. e. T poptop ( )the "classic" pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack. 28. Add the following methods to the ArrayBounded Stack class, and create a test driver for each to show that they work correctly. In order to practice your array related cod- ing skills, code each of these methods by accessing the internal variables of the Ar- rayBounded Stack, not by calling the previously defined public methods of the class. a. String toString()-creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method. b. int size()-returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method. C. void popSome(int count)-removes the top count elements from the stack; throws StackUnderflowException if there are less than count ele- ments on the stack d. boolean swapStart()-if there are less than two elements on the stack re- turns false; otherwise it reverses the order of the top two elements on the stack and returns true. e. T poptop ( )the "classic" pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!

Q:

\f