Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /**
  2. *
  3. * @author Thomas Wessel
  4. */
  5. public class Stack {
  6.  
  7. //global variables
  8. private Node top;
  9. private Node bottom;
  10. private int numOfNodes = 0;
  11.  
  12. public boolean isEmpty(){
  13. return numOfNodes == 0;
  14. }
  15. public int getSize(){
  16. return numOfNodes;
  17. }
  18.  
  19. public void popAll(){
  20. while(!isEmpty()){
  21. pop();
  22. }
  23. }
  24.  
  25. public void push(Object newItem){
  26.  
  27. //check to see if bottom has a value. Also sets top.
  28. if(bottom == null){
  29. //create new node
  30. bottom = new Node(newItem);
  31. //set top to bottom
  32. top = bottom;
  33. //increment the number of nodes
  34. numOfNodes++;
  35. }
  36. else{
  37. //Create a new node
  38. Node toStack = new Node(newItem);
  39. //set toStack.next to current top
  40. toStack.setNextNode(top);
  41. //set top to new node
  42. top = toStack;
  43. //increment the number of nodes
  44. numOfNodes++;
  45.  
  46. }
  47. }
  48.  
  49. //this should work
  50. public Object pop(){
  51. Object toReturn = top.getcontainedItem();
  52. top = top.getNextNode();
  53. //decrement numOfNodes
  54. numOfNodes --;
  55. return toReturn;
  56. }
  57.  
  58. //This works
  59. public Object peek(){
  60. return top.getcontainedItem();
  61. }
  62.  
  63.  
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement