Guest User

Stack1gen.java

a guest
Feb 24th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. //Stack1gen.java
  2. //array implementation of stack class
  3.  
  4. public class Stack1gen<T>
  5. {
  6. int MAX = 30; //maximum number of stack entries
  7.  
  8. private int top;
  9. private T[] stack; //array to hold the stack data
  10.  
  11. //default constructor
  12. public Stack1gen()
  13. {
  14. top = MAX; //initialize an empty stack
  15. stack = (T[]) new Object[MAX];
  16. }
  17.  
  18. //copy constructor
  19. // public Stack1(Stack1 s)
  20. // {
  21. // top = s.top;
  22. // for(int i = top; i<=MAX-1; i++)
  23. // {
  24. // stack[i] = s.stack[i];
  25. // }
  26. // }
  27.  
  28. public void push(T y) //push data item y onto the stack
  29. {
  30. assert(top > 0); //check that there is room on the stack
  31. top = top -1;
  32. stack[top] = y;
  33. }
  34.  
  35. public T pop() //pop the top item from the stack and return it
  36. {
  37. assert(top < MAX); //check that there is data on the stack
  38. T x = stack[top];
  39. top = top +1;
  40. return x;
  41. }
  42.  
  43. public int getSize()
  44. {
  45. return MAX-top;
  46. }
  47.  
  48. public T getTop()
  49. {
  50. assert(top < MAX);
  51. return stack[top];
  52. }
  53.  
  54. // public void printStack() //print the contents of the stack, from
  55. // top to bottom, one item per line, without popping the stack items.
  56.  
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment