Advertisement
Guest User

Untitled

a guest
Jul 16th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. class StackDequeX {
  2. private int maxSize;
  3. private long[] stackArray;
  4. private int top; //top of stack
  5. private int bottom; //bottom of stack
  6.  
  7. //................................................................................
  8.  
  9. public StackDequeX(int s) { //constructor
  10. maxSize = s; //set array size
  11. stackArray = new long[maxSize]; //create array
  12. top = 0; //no items yet
  13. bottom = 0;
  14. }
  15.  
  16. //.................................................................................
  17. //Putting Item on Top of Stack
  18.  
  19. public void pushTop(long j) {
  20. stackArray[top++] = j;
  21. }
  22.  
  23. //..................................................................................
  24. //Putting Item on Bottom of Stack
  25.  
  26. public void pushBottom(long j) {
  27. if (bottom == 0) {
  28. bottom = maxSize - 1;
  29. stackArray[bottom] = j;
  30. }
  31. else {
  32. stackArray[--bottom] = j;
  33. }
  34. }
  35.  
  36. //...................................................................................
  37. //Remove item from top of stack
  38.  
  39. public long popTop() {
  40. return stackArray[top--];
  41. }
  42.  
  43. //.................................................................................
  44. //Remove item from bottom of stack
  45.  
  46. public long popBottom() {
  47. if (bottom == maxSize - 1) {
  48. return stackArray[bottom--];
  49. }
  50. else
  51. bottom = maxSize - 1;
  52. top--;
  53. return stackArray[bottom--];
  54.  
  55. }
  56.  
  57. //.................................................................................
  58.  
  59. public long peek() { //peek at top of stack
  60. return stackArray[top];
  61. }
  62.  
  63. //.................................................................................
  64.  
  65. public boolean isEmpty() { //true if stack is empty
  66. return (top == 0);
  67. }
  68.  
  69. //..................................................................................
  70.  
  71. public boolean isFull() { //true if stack is full
  72. return (top == maxSize-1);
  73. }
  74.  
  75. //..................................................................................
  76. public void displayStack() {
  77. System.out.println(" ");
  78. System.out.println("Top: " + top);
  79. System.out.println("Bottom: " + bottom);
  80. int i = top;
  81. while(true) {
  82. if (i > 0) {
  83. i--;
  84. System.out.print(stackArray[i] + "\n");
  85. }
  86. else {
  87. i = maxSize - 1;
  88. System.out.print(stackArray[i] + "\n");
  89. }
  90. if(i == bottom)
  91. break;
  92. }
  93. }
  94.  
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement