Array Stack help

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 putpublic 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?

[736 byte] By [RussWLW2004a] at [2007-11-26 21:56:16]
# 1

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).

hunter9000a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 2

> -Write the code for isEmpty()- I putpublic

> 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.

}

hunter9000a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 3

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?

RussWLW2004a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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.

hunter9000a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 5
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?
RussWLW2004a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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.

hunter9000a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 7
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?
RussWLW2004a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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

hunter9000a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...
# 9
alright i understand now thanks a bunch, i might message you possibly for more help in comming days.
RussWLW2004a at 2007-7-10 3:52:38 > top of Java-index,Java Essentials,Java Programming...