Need help with a stack

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Need help with a stack

Postby stuarte9 » Fri Jan 27, 2012 4:20 pm

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: Select all
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: Select all
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
stuarte9
 
Posts: 60
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland

Postby evilnick » Thu Feb 16, 2012 1:04 am

Hmmm. doesn't the code in main() replicate the error-checking. ie., it checks the stack is full BEFORE pop() ing information from it
User avatar
evilnick
Moderator
 
Posts: 151
Joined: Mon Apr 04, 2005 11:47 am
Location: LXF towers

Re: a problem with a stack

Postby stuarte9 » Wed Feb 22, 2012 3:57 pm

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
stuarte9
 
Posts: 60
Joined: Mon Mar 08, 2010 5:03 pm
Location: Scotland

Postby larcky » Wed Feb 22, 2012 9:47 pm

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: Select all
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: Select all
char c;
while ( pop(stk, &c) )
{
    cout << c;
}
larcky
 
Posts: 19
Joined: Sun Nov 21, 2010 6:28 pm
Location: England


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron