In: Computer Science
Given the LinkedStack Class as covered in class add a method to the LinkedStack class called PushBottom which will add a new item to the bottom of the stack. The push bottom will increase the number of items in the stack by 1
// defination of pushBottom method
public void pushBottom( T element){
LinearNode<T> toAdd = new LinearNode<T>(element); // create toAdd node and insert the value of element
LinearNode<T> tmp = new LinearNode<T>(null); // create temparory node for storing the value of top
tmp = top;
// while loop for move to the bottom of stack
while (top.next != null) {
top = top.next; // traverse in the stack
}
// now we're at the bottom
top.setNext(toAdd); // insert the value at the bottom of the stack
top=tmp; // top point to the top of the stack
count++; // increase the number of items in the stack by 1
}