| View previous topic :: View next topic |
| Author |
Message |
stuarte9
Joined: Mon Mar 08, 2010 5:03 pm Posts: 31 Location: Scotland
|
Posted: Fri Jan 27, 2012 4:20 pm Post subject: Need help with a stack |
|
|
Hi all,
I've just coded up a small prog to push characters onto a stack and then pop them off again. This is working fine as it is. I then modified functions push() to test to see if the stack was full and if so to print an error message to cerr. In a similar manner I've modified function pop() to test to see if the stack was empty and if so to also print an error message to cerr.
| Code: | void push(char c, stack* stk)
{
if (!full(stk))
{
stk->top++;
stk->s[stk->top] = c;
}
else
{
cerr << endl << "Stack is full! Exiting programm." << endl;
exit(1);
}
}
char pop(stack* stk)
{
if (!empty(stk))
{
return (stk->s[stk->top--]);
}
else
{
cerr << endl << "Stack is empty! Exiting programm." << endl;
exit(1);
}
}
|
These are called in main() as follows:-
| Code: |
int main()
{
stack stk;
char str[max_length] = {"My name is Johnathan!"};
int i = 0;
cout << str << endl; // Print the string
reset(&stk);
while (str[i]) // Push onto stack
{
push(str[i++], &stk);
}
while (!empty(&stk)) // Print the reverse
{
cout << pop(&stk);
}
cout << endl;
return (0);
} |
My problem is that I just can't see how to exercise the else clauses. Could someone please point me in the right direction ? Thanks in advance.
Stuart |
|
| Back to top |
|
 |
evilnick Moderator

Joined: Mon Apr 04, 2005 12:47 pm Posts: 151 Location: LXF towers
|
Posted: Thu Feb 16, 2012 1:04 am Post subject: |
|
|
| Hmmm. doesn't the code in main() replicate the error-checking. ie., it checks the stack is full BEFORE pop() ing information from it |
|
| Back to top |
|
 |
stuarte9
Joined: Mon Mar 08, 2010 5:03 pm Posts: 31 Location: Scotland
|
Posted: Wed Feb 22, 2012 3:57 pm Post subject: Re: a problem with a stack |
|
|
Hi Nick,
Thanks for the reply. Yes, there does seem to be redundant error checking of the stack. The problem is, I just don't see how to implement the "else" clause in pop() without this redundancy. Any ideas ? I know it's such a simple thing but I seem to have developed a bit of a mental block on this.
I would really appreciate a hint on the way forward.
Best regards,
Stuart |
|
| Back to top |
|
 |
larcky
Joined: Sun Nov 21, 2010 6:28 pm Posts: 19 Location: England
|
Posted: Wed Feb 22, 2012 9:47 pm Post subject: |
|
|
Hi
If you were writing a Stack class for a client it'd be a safe bet that they'd call pop() and push() without bothering to check first if it was OK to do so. They'd expect your code to deal with that for them. So what about:
| Code: | int push( stack* stk, char c );
int pop( stack* stk, char* c );
|
These check internally if it's OK to do the operation and return 1 for success or 0 for failure. Because pop() takes a pointer to char your function can write to it and return that value back to the calling environment.
Then your final while loop would look something like:
| Code: | char c;
while ( pop(stk, &c) )
{
cout << c;
} |
|
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|