Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Thomas Wessel
- */
- public class Stack {
- //global variables
- private Node top;
- private Node bottom;
- private int numOfNodes = 0;
- public boolean isEmpty(){
- return numOfNodes == 0;
- }
- public int getSize(){
- return numOfNodes;
- }
- public void popAll(){
- while(!isEmpty()){
- pop();
- }
- }
- public void push(Object newItem){
- //check to see if bottom has a value. Also sets top.
- if(bottom == null){
- //create new node
- bottom = new Node(newItem);
- //set top to bottom
- top = bottom;
- //increment the number of nodes
- numOfNodes++;
- }
- else{
- //Create a new node
- Node toStack = new Node(newItem);
- //set toStack.next to current top
- toStack.setNextNode(top);
- //set top to new node
- top = toStack;
- //increment the number of nodes
- numOfNodes++;
- }
- }
- //this should work
- public Object pop(){
- Object toReturn = top.getcontainedItem();
- top = top.getNextNode();
- //decrement numOfNodes
- numOfNodes --;
- return toReturn;
- }
- //This works
- public Object peek(){
- return top.getcontainedItem();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement