Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help with a stack

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
stuarte9



Joined: Mon Mar 08, 2010 5:03 pm
Posts: 53
Location: Scotland

PostPosted: Fri Jan 27, 2012 4:20 pm    Post subject: Need help with a stack Reply with quote

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
View user's profile Send private message
evilnick
Moderator


Joined: Mon Apr 04, 2005 12:47 pm
Posts: 151
Location: LXF towers

PostPosted: Thu Feb 16, 2012 1:04 am    Post subject: Reply with quote

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
View user's profile Send private message
stuarte9



Joined: Mon Mar 08, 2010 5:03 pm
Posts: 53
Location: Scotland

PostPosted: Wed Feb 22, 2012 3:57 pm    Post subject: Re: a problem with a stack Reply with quote

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
View user's profile Send private message
larcky



Joined: Sun Nov 21, 2010 6:28 pm
Posts: 19
Location: England

PostPosted: Wed Feb 22, 2012 9:47 pm    Post subject: Reply with quote

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 user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast