<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://www.linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Fri May 24, 2013 6:55 pm by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Fri May 24, 2013 6:55 pm</pubDate>
  <lastBuildDate>Fri May 24, 2013 6:55 pm</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://www.linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>Re: Need help with a stack</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=104559#104559</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=67058'&gt;larcky&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Feb 22, 2012 9:47 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi&lt;br /&gt;
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&amp;#058;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;int push&amp;#40; stack* stk, char c &amp;#41;;&lt;br /&gt;
int pop&amp;#40; stack* stk, char* c &amp;#41;;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Then your final while loop would look something like:&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;char c;&lt;br /&gt;
while &amp;#40; pop&amp;#40;stk, &amp;amp;c&amp;#41; &amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; cout &amp;lt;&amp;lt; c;&lt;br /&gt;
&amp;#125;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=104559#104559</comments>
                                        <author>larcky</author>
                                        <pubDate>Wed Feb 22, 2012 9:47 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=104559#104559</guid>
                                      </item>
                                      <item>
                                        <title>Re:  a problem with a stack</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=104554#104554</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66284'&gt;stuarte9&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Wed Feb 22, 2012 3:57 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi Nick,&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;else&amp;quot; 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.&lt;br /&gt;
&lt;br /&gt;
I would really appreciate a hint on the way forward.&lt;br /&gt;
&lt;br /&gt;
Best regards,&lt;br /&gt;
&lt;br /&gt;
Stuart</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=104554#104554</comments>
                                        <author>stuarte9</author>
                                        <pubDate>Wed Feb 22, 2012 3:57 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=104554#104554</guid>
                                      </item>
                                      <item>
                                        <title>Re: Need help with a stack</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=104449#104449</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=4'&gt;evilnick&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Thu Feb 16, 2012 1:04 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hmmm. doesn't the code in main() replicate the error-checking. ie., it checks the stack is full BEFORE pop() ing information from it</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=104449#104449</comments>
                                        <author>evilnick</author>
                                        <pubDate>Thu Feb 16, 2012 1:04 am</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=104449#104449</guid>
                                      </item>
                                      <item>
                                        <title>Need help with a stack</title>
                                        <link>http://www.linuxformat.com/forums/viewtopic.php?p=104097#104097</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://www.linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=66284'&gt;stuarte9&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Jan 27, 2012 4:20 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Hi all,&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;void push&amp;#40;char c, stack* stk&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;if &amp;#40;!full&amp;#40;stk&amp;#41;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;stk-&amp;gt;top++;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;stk-&amp;gt;s&amp;#91;stk-&amp;gt;top&amp;#93; = c;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;else&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;cerr &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Stack is full! Exiting programm.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;exit&amp;#40;1&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
char pop&amp;#40;stack* stk&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;if &amp;#40;!empty&amp;#40;stk&amp;#41;&amp;#41;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;return &amp;#40;stk-&amp;gt;s&amp;#91;stk-&amp;gt;top--&amp;#93;&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;else&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;cerr &amp;lt;&amp;lt; endl &amp;lt;&amp;lt; &amp;quot;Stack is empty! Exiting programm.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;exit&amp;#40;1&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
These are called in main() as follows:-&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
int main&amp;#40;&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;stack stk;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;char str&amp;#91;max_length&amp;#93; = &amp;#123;&amp;quot;My name is Johnathan!&amp;quot;&amp;#125;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;int i = 0;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; str &amp;lt;&amp;lt; endl;&amp;nbsp; &amp;nbsp; // Print the string&lt;br /&gt;
&amp;nbsp; &amp;nbsp;reset&amp;#40;&amp;amp;stk&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;while &amp;#40;str&amp;#91;i&amp;#93;&amp;#41;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Push onto stack&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;push&amp;#40;str&amp;#91;i++&amp;#93;, &amp;amp;stk&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;while &amp;#40;!empty&amp;#40;&amp;amp;stk&amp;#41;&amp;#41;&amp;nbsp; &amp;nbsp; // Print the reverse&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#123;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; pop&amp;#40;&amp;amp;stk&amp;#41;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;#125;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;return &amp;#40;0&amp;#41;;&lt;br /&gt;
&amp;#125;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Stuart</description>
                                        <comments>http://www.linuxformat.com/forums/viewtopic.php?p=104097#104097</comments>
                                        <author>stuarte9</author>
                                        <pubDate>Fri Jan 27, 2012 4:20 pm</pubDate>
                                        <guid isPermaLink="true">http://www.linuxformat.com/forums/viewtopic.php?p=104097#104097</guid>
                                      </item></channel></rss>