LinkedStack.java— fill in the missing code in the methods isEmpty, size, and toS
ID: 3915930 • Letter: L
Question
LinkedStack.java— fill in the missing code in the methods isEmpty, size, and toStringmodel your toString method on the code you wrote in BuildLinkedList, using a loop to traverse the linked list.
Download the code in the file TestLinkedStack.txt and cut and paste this at the end of your LinkedStack class as a test harness. Run this as the main method to show new methods in LinkedStack work Download the code in the file TestLinkedStack.txt and cut and paste this at the end of your LinkedStack class as a test harness. Run this as the main method to show new methods in LinkedStack work
public class LinkedStack /*indicates number of elements stored / private int count; /*pointer to top of stack * private LinearNode top; ek * Creates an empty stack. public LinkedStackO count = 0; top = null; * Adds the specified element to the top of this stack. eparam element element to be pushed on stack public void push (T element) LinearNodeeT> temp = new LinearNodeeT> (element); temp.setNext(top); top temp; count++;
Explanation / Answer
The LinkedStack class already shows the isEmpty() and size() methods completed. Only toString() needs to be completed. Given below is the code for toString() method of LinkedStack
public String toString(){
LinearNode<T> current = top;
String s = "[top]";
while(current != null){
s += " " + current.getElement();
current = current.getNext();
}
return s;
}
Just copy paste the above code into your LinkedStack's toString() method. And then copy the main method from your TestLinkedStack.txt into LinkedStack class and then run the code. Without the main() copied, you won't be able to run the code. Please do rate the answer if it helped. Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.