View Single Post
Join Date: Sep 2008
Posts: 91
Reputation: ezkonekgal is an unknown quantity at this point 
Solved Threads: 1
ezkonekgal ezkonekgal is offline Offline
Junior Poster in Training

i need help on stack. If pop: stack is empty.

 
0
  #1
Jan 12th, 2009
hello.. i really need help.. I don't know why i keep on getting an empty stack if i pop a token from stack even if user has already push a token into it..

  1. public class MStack implements Stack{
  2.  
  3. public Node top;
  4. public Node[] stack;
  5. public static int T[];
  6. public static int B[];
  7. public static int oldT[];
  8. public static int i;
  9. int size2;
  10. int noOfStack;
  11.  
  12.  
  13. class Node{
  14. Object info;
  15. Node link;
  16. }
  17.  
  18. public MStack(){
  19.  
  20. }
  21.  
  22. public MStack(int size, int noOfStack){
  23. size2= size;
  24. stack = new Node[size];
  25. T = new int[noOfStack + 1];
  26. B = new int[noOfStack + 1];
  27. for (int i = 0; i < noOfStack+1; i++){
  28. B[i]= (int)Math.floor(size/noOfStack) * i -1;
  29. T[i] = B[i];
  30. }
  31. }
  32.  
  33. public Object top(int i){
  34. if (isEmpty(i)){
  35. System.out.println("Stack empty.");
  36. }return top.info;
  37. }
  38.  
  39. public void push(String item,int i ) {
  40. if (T[i]== B[i+1]){
  41. System.out.println("Stack"+ i + "is full.");
  42. }else{
  43. Node newNode = new Node();
  44. newNode.info = item;
  45. newNode.link = top;
  46. top = newNode;
  47. System.out.println("You have pushed an element into stack");
  48. }
  49. }
  50.  
  51. public Object pop(int i ){
  52. Node temp[] = new Node[noOfStack];
  53. if (isEmpty(i)){
  54. System.out.println("Stack has nothing inside it");
  55. }
  56. temp[i] = top;
  57. top = top.link;
  58. return temp[i].info;
  59. }
  60.  
  61. public boolean isEmpty(int i){
  62. return (T[i] == B[i]);
  63. }
  64.  
  65. public int size(int i){
  66. int size = T[i] - B[i] ;
  67. return size;
  68. }
  69. }

this is another class. This has the main type:

  1. import java.io.*;
  2.  
  3. public class BookShelf extends MStack {
  4.  
  5. public static InputStreamReader ir = new InputStreamReader(System.in);
  6. public static BufferedReader bf = new BufferedReader(ir);
  7. public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  8. public static MStack bookShelf;
  9. public static int size;
  10. public static int shelf;
  11.  
  12. public static void main (String []args) throws IOException{
  13. System.out.println("What size do you want your bookshelf to be?(integer):");
  14. size = Integer.parseInt(in.readLine());
  15. System.out.println("How many shelves do you want your bookshelf to have?:");
  16. shelf = Integer.parseInt(in.readLine());
  17. bookShelf = new MStack(size, shelf);
  18.  
  19. new BookShelf();
  20. }
  21.  
  22. public BookShelf(){
  23. super();
  24. options();
  25. }
  26. public void options(){
  27. try{
  28. do{
  29. System.out.println("\nPlease choose from below. Use numbers to choose.");
  30. System.out.println("1. Place book on shelf");
  31. System.out.println("2. Retrieve book from shelf");
  32. System.out.println("3. View books on each shelf");
  33. System.out.println("4. Exit");
  34. int choice = Integer.parseInt(in.readLine());
  35. if (choice == 1){
  36. System.out.println("Which shelf do you want to place a book?");
  37. System.out.println("Enter either 1, 2 or 3");
  38. int placement = Integer.parseInt(in.readLine());
  39. if(placement == 1){
  40. System.out.println("Enter element:");
  41. String item = bf.readLine();
  42. garwick(placement);
  43. bookShelf.push(item, placement);
  44. }if(placement == 2){
  45. System.out.println("Enter an element:");
  46. String item = bf.readLine();
  47. garwick(placement);
  48. bookShelf.push(item, placement);
  49. }if(placement == 3){
  50. System.out.println("Enter an element:");
  51. String item = bf.readLine();
  52. garwick(placement);
  53. bookShelf.push(item, placement);
  54. }
  55. }else if (choice == 2){
  56. System.out.println("Which shelf do you want to retrieve a book, 1, 2, or 3?");
  57. int choice2 = Integer.parseInt(in.readLine());
  58. switch (choice2){
  59. case 1 : if (bookShelf.isEmpty(choice2)) {
  60. System.out.println( " Shelf " + choice2 + " is empty!");
  61. }else {
  62. System.out.println("Item" + bookShelf.pop(choice2)+ " is retrieved.");
  63. }break;
  64. case 2 : if (bookShelf.isEmpty(choice2)) {
  65. System.out.println(" Shelf " + choice2 + " is empty!");
  66. }else {
  67. System.out.println("Item" + bookShelf.pop(choice2)+ " is retrieved.");
  68. }break;
  69. case 3 : if (bookShelf.isEmpty(choice2)) {
  70. System.out.println(" Shelf " + choice2 + " is empty!");
  71. }else {
  72. System.out.println("Item" + bookShelf.pop(choice2)+ " is retrieved.");
  73. }break;
  74. }
  75. }else if(choice == 3){
  76. System.out.println("Recent items");
  77. for (int i = 1; i <= shelf; ++i) {
  78. System.out.println("shelf #" + i + ": " + bookShelf.top(i));
  79. }
  80. System.out.println("shelf #" + i + ": Empty");
  81. }else if (choice == 4){
  82. System.out.println("You have exited. Thank you!");
  83. break;
  84. }
  85. }while (true);
  86. }catch(IOException e){}
  87. }
  88.  
  89. /*
  90. * Method that implements garwick's algorithm for reallocating free space
  91. * within a multiple stack
  92. */
  93. public void garwick(int i) {
  94. int diff[] = new int[shelf + 1];
  95. int size[] = new int[shelf + 1];
  96. int totalSize = 0;
  97. double freecells, incr = 0;
  98. double alpha, beta, sigma = 0, tau = 0;
  99. /* Compute for the allocation factors */
  100. for (int j = 1; j <= bookShelf.noOfStack; j++) {
  101. size[j] = bookShelf.size(j);
  102. if ((bookShelf.T[j] - bookShelf.oldT[j]) > 0){
  103. diff[j] = bookShelf.T[j] - bookShelf.oldT[j];
  104. }else{
  105. diff[j] = 0;
  106. totalSize += size[j];
  107. incr += diff[j];
  108. }
  109. }
  110. diff[i]++;
  111. size[i]++;
  112. totalSize++;
  113. incr++;
  114. freecells = bookShelf.size2 - totalSize;
  115. alpha = 0.10 * freecells / bookShelf.noOfStack;
  116. beta = 0.90 * freecells / incr;
  117. /* If all stacks are full */
  118. if (freecells < 1)
  119. throw new StackException("Stack overflow: All Stacks are full");
  120. /* Compute for new bases */
  121. for (int j = 2; j <= bookShelf.noOfStack; j++) {
  122. tau = sigma + alpha + diff[j - 1] * beta;
  123. bookShelf.B[j] = bookShelf.B[j - 1] + size[j - 1] + (int) Math.floor(tau)- (int) Math.floor(sigma);
  124. sigma = tau;
  125. }
  126. /* Restore size of the overflowed stack to its old value */
  127. size[i]--;
  128. System.out.println("size[" + i + "] = " + size[i]);
  129. /* Compute for new top addresses */
  130. for (int j = 1; j <= bookShelf.noOfStack; j++) {
  131. bookShelf.T[j] = bookShelf.B[j] + size[j];
  132. }
  133. bookShelf.oldT = bookShelf.T;
  134. }
  135. }

It implements the Stack interface.
I run this on eclipse compiler.
Can someone help me please.. thanks a lot.. i really need to finish this asap..
Last edited by ezkonekgal; Jan 12th, 2009 at 9:11 am.
Reply With Quote