Linux Format forumsHelp, discussion, magazine feedback and more2012-02-22T21:47:48+00:00http://linuxformat.com/forums/feed.php?t=145222012-02-22T21:47:48+00:002012-02-22T21:47:48+00:00http://linuxformat.com/forums/viewtopic.php?t=14522&p=104559#p104559If 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.

]]>2012-02-22T15:57:58+00:002012-02-22T15:57:58+00:00http://linuxformat.com/forums/viewtopic.php?t=14522&p=104554#p104554Thanks 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.

]]>2012-02-16T01:04:35+00:002012-02-16T01:04:35+00:00http://linuxformat.com/forums/viewtopic.php?t=14522&p=104449#p104449Statistics: Posted by evilnick — Thu Feb 16, 2012 1:04 am
]]>2012-01-27T16:20:32+00:002012-01-27T16:20:32+00:00http://linuxformat.com/forums/viewtopic.php?t=14522&p=104097#p104097I'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.