-
More Stacks
I have one question about stack size
Code:
int StackSize(Stack *s)
{
int count=0;
if(Push(item, *s))
count++;
else if (Pop(*item,*s))
count--;
return count;
}
i have other errors in my code, so i dont know if this is legal, the prototype for push is
int Push(StackE item, Stack *s)
int Pop(StackE *item, Stack *s)
or should do something with my top var to get the size?
also for my Push
Code:
int Push(StackEntry item, Stack *s) {
Node *nptr = MakeNode(item);
{
*nptr = s->top;
if(StackFull(s)) return 1;
else
{
*item = nptr->entry;
s->top++ = nptr->next;
return 0;
}
}
Thanks for the help
-
Actually, for a 'pop' statement, you should not pass it any arguments, unless you have multiple stacks, and you're passing it the stack to pop from. Example:
Code:
Data pop( Stack **s )
{
Stack *temp;
Data d;
temp = *s->next;
d = *s->data;
free( *s );
*s = temp;
return d;
}
What we do is this:
1) pass a pointer to the stack so we can update it
2) make a temp variable hold the next item on the stack
3) copy by value the data from the top of the stack
4) free the top item on the stack
5) update the stack pointer to point to the new "top"
6) return by value the data that was on the top
Now, this is much simpler if you just have a single global stack:
Code:
Data pop( void )
{
Stack *s;
Data d;
s = ourStack;
ourStack = ourStack->next;
d = s->data;
free( s );
return d;
}
Basicly it's the same, but you don't have to screw with pointers to pointers. Note: I left out error checking, you can do that on your own.
Push works basicly the same way. You can either push to a global stack, by just passing the data, or you can pass both the stack to push to, and the data to push.
Quzah.
-
>but you don't have to screw with pointers to pointers
But what fun is that? :rolleyes:
I go play in the C++ boards and you take over back here, I suppose I should keep a better watch ;)
-Prelude
-
Don't worry about it. You're not missing much. 90% of the posts are from lavon, who is just posting the same thing over and over anyway... Heh.
Quzah.
-
I noticed, we've both told him to remove the &'s from printf and he still hasn't done it yet it appears :P
-Prelude