10 Replies Latest reply: Aug 30, 2007 11:53 AM by 807605 RSS

    Array Stack help

    807605
      learning stacks in my java class.
      have an assignment with some questions.

      please someone correct me if im wrong, but when you push a number into a stack it goes into an array bassicaly..now does that start at index 0? i mean the first open slot?

      cause if im thinking right the question im givin is that:

      Convention 1: We store items at the low end, starting at index 0(to my knowledge that would be the begging of the array) We use the variable count which equals the number of items on the stack.

      sample questions being asked::

      -Write the code for isEmpty() - I put public boolean isEmpty()

      am i on the right course or is it asking the actual code used to find if it is empty in the code?
        • 1. Re: Array Stack help
          807605
          To implement a stack in the way you described, you would allocate the array to a large enough size, since you can't resize an array. Keep a counter that gives you the index where the last element is, which is the top. To push, add the element at that spot, and increment the counter. To pop, return the element at that spot, and decrement the counter. If the stack is empty, that means that the counter is at the bottom of the stack (position 0).
          • 2. Re: Array Stack help
            807605
            -Write the code for isEmpty() - I put public
            boolean isEmpty()

            am i on the right course or is it asking the actual
            code used to find if it is empty in the code?
            Yes, that's a good start for the method.
            boolean isEmpty() {
               // put the code here.
            }
            • 3. Re: Array Stack help
              807605
              good im on the right track than because it doesnt ask more than that other than just give the code.

              so if thats right for that, further down the sheet it says.

              convention 2: We store items at the high end starting at index size-1 ane use a variable top: the index where the last item was pushed.

              would the codes be the same than?
              • 4. Re: Array Stack help
                807605
                good im on the right track than because it doesnt ask
                more than that other than just give the code.

                so if thats right for that, further down the sheet it
                says.

                convention 2: We store items at the high end starting
                at index size-1 ane use a variable top: the index
                where the last item was pushed.

                would the codes be the same than?
                Basically, you'd just start the counter at the end instead of the beginning, and move it in the other direction when pushing/popping.
                • 5. Re: Array Stack help
                  807605
                  so at default you push pop at the begging but if you want to start at the end you'll have to start the counter from there?
                  • 6. Re: Array Stack help
                    807605
                    so at default you push pop at the begging but if you
                    want to start at the end you'll have to start the
                    counter from there?
                    Yes, you start the counter at the "bottom" of the stack, which you can implement to be either the beginning or end of the array. You move the counter towards the "top" as you push, and towards the "bottom" as you pop. When the counter is at the bottom, the stack's empty.
                    • 7. Re: Array Stack help
                      807605
                      im guessing im starting to understand lol.

                      so say i want to put [ A B C in a stack.

                      in an array A would begin at Index 0 right?
                      • 8. Re: Array Stack help
                        807605
                        im guessing im starting to understand lol.

                        so say i want to put [ A B C in a stack.

                        in an array A would begin at Index 0 right?
                        If you implement the stack to start at 0:
                        push('A');
                        -------------------
                        |  A  |     |     |
                        -------------------
                           ^
                           |
                        counter=0
                        push('B');
                        -------------------
                        |  A  |  B  |     |
                        -------------------
                                 ^
                                 |
                              counter=1
                        push('C');
                        -------------------
                        |  A  |  B  |  C  |
                        -------------------
                                       ^
                                       |
                                    counter=2
                        • 9. Re: Array Stack help
                          807605
                          alright i understand now thanks a bunch, i might message you possibly for more help in comming days.
                          • 10. Array Problems
                            807605
                            i have created this list of 10 integers array 0,1,2,3,4,5,6,7,8,9;
                            i have to pop them out 9,8,7,6,5,4,3,2,1,0
                            so far i only managed to be able to print out the list of 10 integers.
                            Could any kind soul help???

                            System.out.println("\n");
                            System.out.println("OPERATION: Reverse an array using a stack");

                            System.out.println("\n");

                            System.out.println("Set up an array of 10 integer objects");

                            int i,j;
                                 //set 10 array
                            int[] nums1 = new int[10];
                            int[] nums2 = new int[10];

                            for(i=0; i < nums1.length; i++) nums1[i] = i;
                            System.out.print("Array 1= ");
                            for(i=0; i < nums2.length; i++)
                            System.out.print(i);

                            System.out.print("\n");

                            System.out.print("Stack Content: bottom " + s1 + " top");

                            System.out.println("\n");

                            System.out.println("Get objects from Array 1, add to stack");
                            s1.push(i);
                            System.out.println("Stack Content: bottom " + s1 + " top");