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

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

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 Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: SamCris is an unknown quantity at this point 
Solved Threads: 0
SamCris's Avatar
SamCris SamCris is offline Offline
Newbie Poster

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

 
0
  #2
Jan 12th, 2009
hello! may i know wat T[] and oldT[] represent? how do u derive them?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is online now Online
Posting Virtuoso

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

 
0
  #3
Jan 12th, 2009
Can you post the error you are getting? It should include the line number, method names, and error name... info that would be incredibly helpful in helping you.
Reply With Quote Quick reply to this message  
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

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

 
0
  #4
Jan 12th, 2009
Originally Posted by SamCris View Post
hello! may i know wat T[] and oldT[] represent? how do u derive them?
T[] refers to he top and oldT[] is the old top.. becuase in the garwick's algo, oldT will get new T when new base pointers, represented as B[], are computed.
Reply With Quote Quick reply to this message  
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

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

 
0
  #5
Jan 12th, 2009
Originally Posted by BestJewSinceJC View Post
Can you post the error you are getting? It should include the line number, method names, and error name... info that would be incredibly helpful in helping you.

when you put an item in the shelf, is done by push method, and then try to retreive the item, done by pop, i doesn't have error, only just print's stack empty.. because i did specify in the code that if empty print to console Satck is empty. the program continues to run.

ok w8, i got this recenlty

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at MStack.push(MStack.java:45)
at BookShelf.options(BookShelf.java:48)
at BookShelf.<init>(BookShelf.java:24)
at BookShelf.main(BookShelf.java:19)
Last edited by ezkonekgal; Jan 13th, 2009 at 12:02 am.
Reply With Quote Quick reply to this message  
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

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

 
0
  #6
Jan 13th, 2009
Those T[], B[], and oldT[], keeps track of which stack you want, either stack1, stack2, and stack3.
If i chose stack1 to put an item, it pushes and is ok,but if i try to pop the item in which i put in stack1, it says stack empty. If i try to view what is inside the stack 1 2 3, i get stack 1 = the item i just pushed, stack 2 = the item i just pushed, and stack 3 = the item i just pushed, which is wrong because i only pushed an item in stack 1.
Last edited by ezkonekgal; Jan 13th, 2009 at 12:22 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: SamCris is an unknown quantity at this point 
Solved Threads: 0
SamCris's Avatar
SamCris SamCris is offline Offline
Newbie Poster

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

 
-1
  #7
Jan 13th, 2009
kung pwde k mgbisaya ok lng, pasabta lng ko sa T[] ug OldT[] ug nganong k2ng sa Fig 1.13 (page 35) sa JEDI course notes dili masabtan
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #8
Jan 13th, 2009
Originally Posted by SamCris View Post
kung pwde k mgbisaya ok lng, pasabta lng ko sa T[] ug OldT[] ug nganong k2ng sa Fig 1.13 (page 35) sa JEDI course notes dili masabtan
Did you just swallow an ostrich size fly ?
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 3
Reputation: SamCris is an unknown quantity at this point 
Solved Threads: 0
SamCris's Avatar
SamCris SamCris is offline Offline
Newbie Poster

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

 
0
  #9
Jan 13th, 2009
Originally Posted by verruckt24 View Post
Did you just swallow an ostrich size fly ?
LOL!!! ) im sorry i had to speak in our dialect... its just that i cud express myself better that way, sorry
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

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

 
0
  #10
Jan 13th, 2009
Originally Posted by SamCris View Post
LOL!!! ) im sorry i had to speak in our dialect... its just that i cud express myself better that way, sorry
and you have any reason to believe there's any one other than you on this forum that has even the slightest idea of what you said in that post?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC