Advertisement
Buffet_Time

Untitled

Apr 7th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. 1
  2.  
  3. package One;
  4.  
  5. /**
  6. * Created by Daniel on 4/6/2016.
  7. */
  8.  
  9. public class ArrayStack
  10. {
  11. private static final int SIZE = 10; //size of array
  12. private Object[] item; //items within the stack
  13. private int numItem; //#of items
  14.  
  15. public ArrayStack()
  16. {
  17. item = new Object[SIZE];
  18. numItem = 0;
  19.  
  20. }
  21.  
  22. // Pushes (adds)
  23. public void push(Object x)
  24. {
  25.  
  26. if(item.length-1 == numItem)
  27. {
  28. item[numItem] = x;
  29. numItem--;
  30.  
  31. }
  32.  
  33. }
  34.  
  35. //Pops (removes)
  36. public Object pop()
  37. {
  38.  
  39. if(numItem == item.length-1)
  40. {
  41. item[numItem] = null;
  42. numItem--;
  43. }
  44.  
  45. return item[numItem];
  46.  
  47. }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. ---------------
  54.  
  55. 2
  56.  
  57.  
  58.  
  59. package Two;
  60.  
  61. /**
  62. * Created by Daniel on 4/6/2016.
  63. */
  64.  
  65. public class LinkedStack
  66. {
  67. private static class Node
  68. {
  69. int item;
  70. Node next;
  71. }
  72.  
  73. private Node top = null; //node that is at the top of the stack is null
  74.  
  75. public void push(int n)
  76. {
  77. Node newTop; //holds new top stack node
  78. newTop = new Node();
  79. newTop.item = n;
  80. newTop.next = top; //new node refers to old top
  81. top = newTop; //new top
  82. }
  83.  
  84. public int pop()
  85. {
  86. int topItem = top.item;
  87. top = top.next; //popped item
  88. return topItem; //previous node is now the top
  89.  
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement