Question
From text book: p. 158 #47 Add the following methods to the LinkedStack class. In order to practice your linked list related coding skills, code
From text book: p. 158 #47 Add the following methods to the LinkedStack class. In order to practice your linked list related coding skills, code each of these methods by accessing the internal variables of the LinkedStack, 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 LinkedStack 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 elements on the stack. d. boolean swapStart() - if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true. e. T poptop() - the "classic" top operation, if the stack is empty it throws StackUndrflowException; otherwise it both removes and returns the top element of the stack. |
Set up: Create a new java project called Lab5 and place the .java files in the src folder. Submission Instructions: Run the Lab5Tester.java to confirm your solution is correct. Submit the LinkedStack.java file only. public class LinkedStack LLNode private Object setLink; public LinkedStack() { top =null; } @Override public void push(T element) throws StackOverflowException { LLNode newNode.setLink(top); top = newNode; } @Override public void pop() throws StackUnderflowException { if(isEmpty()) { throw new StackUnderflowException(); } top = top.getLink(); } @Override public T top() throws StackUnderflowException { if(isEmpty()) { throw new StackUnderflowException(); } return top.getInfo(); } @Override public boolean isEmpty() { return (top== null); } @Override public boolean isFull() { return false; } public String toString() { String s = setLink.toString(); for(int i=1; i return s; } public String size() { // TODO Auto-generated method stub return null; } public void popSome(int i) { } public void swapStart() { LLNode LLNode } public String poptop() { return null; } } |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started