I'm using a "stack" in a separate function (the reason I quote stack is that for my program, it's just an array that I will treat like a stack)... How can I keep track of where I am in my "stack" ?
I'm using a "stack" in a separate function (the reason I quote stack is that for my program, it's just an array that I will treat like a stack)... How can I keep track of where I am in my "stack" ?
Stack 'pointer'.
That is what a microprocessor uses and they seem to manage OK
Push = put value at pointer and increment
pop = take value at pointer minus one and decrement
or vice versa depending on which direction your stack goes.
Code:// untested :) int stack[100]; int sp=0; //stack pointer push(x){ stack[sp]=x; sp++; } pop(x){ x=stack[sp-1]; sp--; }
Last edited by esbo; 12-14-2008 at 07:46 PM.
hmm I need to do this without using a global variable
It's not a global variable; it belongs to your stack object, and is defined in your struct. (That is to say, your stack is more than just an array, right? Right?) If somehow you do just have an array, then every function will just take an extra argument that is your current stack index.
Sparrowhawk,
I just finished up my first Comp Sci class where we covered different implementations of stacks using arrays as well as data structures. It's not really practical IMHO to do it with just an array, but see for yourself... Check out Lecture #22, "Stacks and Queues."
http://www.cs.umbc.edu/courses/under...ll08/lectures/
-Dave
Note the stack pointer always points to an empty place.
If it was a stack of trays it would point to one above the stack of trays.
Well it does in my example anyway, you could have it poiint to the top
tray but then usually you start with no trays.
Because the variable belongs to the stack, not to the universe?
Edit: I seem to recall (in the spring time, I believe, but I'm not quite bored enough to do a search) someone here who had implemented a stack with a global variable just as you describe. Except he had two stacks. Everything seemed to work fine, until he actually tried to use that second stack....
Last edited by tabstop; 12-14-2008 at 08:28 PM.
Ha, I actually found what I needed in there... It was a very simple solution to fix my pop too... Just make a temp value and copy the current value of top to it, use that to return then decrement top itself.
That seems very obvious all of the sudden. Thanks.
[edit] - The problem I was having was trying to figure out how to return the value while also changing the position without having to take two args.
You could have a single function with a static variable and a direction indicator.
Of course the function is going to be global so your someone can screw your stack up anyway.Code://untested Stackit (x, direction) { static int stack[100]; static int sp=0; // can you initialie statics like this??? if (direction==PUSH){ stack[sp]=x; sp++; } else {//POP x=stack[sp-1]; sp--; } }
I would probably use the first method myself, it seems less messy.
Last edited by esbo; 12-14-2008 at 08:40 PM.
i Thought push is assembly ?