{"id":577,"date":"2013-05-15T15:25:34","date_gmt":"2013-05-15T14:25:34","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=577"},"modified":"2013-05-15T15:28:41","modified_gmt":"2013-05-15T14:28:41","slug":"algorithms-stack","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=577","title":{"rendered":"Algorithms: Stack"},"content":{"rendered":"<p>Simple implementation of Stack with exception handling in C++ using templates<!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full\" alt=\"\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/img_51939a408ec78.png\" width=\"1014\" height=\"354\" \/><\/p>\n<pre lang=\"cpp\">#include&lt;iostream.h&gt;\r\ntemplate &lt;class T&gt;\r\n  class StackItem {\r\n    T item;\r\n\r\n    public:\r\n      StackItem&lt;T&gt; *next;\r\n      T GetItem() { return item; }\r\n      StackItem(T data) {\r\n        this-&gt;item = data;\r\n      }\r\n};\r\n\r\ntemplate &lt;class T&gt;\r\n  class Stack {\r\n    StackItem&lt;T&gt; *top;\r\n      public:\r\n        Stack() {\r\n          this-&gt;top = NULL;\r\n        }\r\n\r\n        void Push(T data) {\r\n          StackItem&lt;T&gt; *it = new StackItem&lt;T&gt;(data);\r\n          if(top == NULL) {\r\n            top = it;\r\n            top-&gt;next = NULL;\r\n          } else {\r\n            it-&gt;next = top;\r\n            top = it;\r\n          }\r\n        }\r\n\r\n        T Pop() {\r\n          if(top == NULL) {\r\n            throw \"Stack is empty !!!\";\r\n          } else {\r\n            StackItem&lt;T&gt; *it = top;\r\n            top = it-&gt;next;\r\n            return it-&gt;GetItem();\r\n          }\r\n        }\r\n};\r\n\r\nint main (int argc, char* argv) {\r\n  Stack&lt;int&gt; stack;\r\n\/\/ Add some items\r\n  for(int i = 0; i&lt;6; i++) {\r\n    stack.Push(i);\r\n    cout &lt;&lt; \"Stack loaded with new value: \" &lt;&lt; i &lt;&lt; endl;\r\n  }\r\n\/\/ Print output with some indexes out of range\r\n  for(int i = 0; i&lt;7; i++) {\r\n    try {\r\n      int value = stack.Pop();\r\n      cout &lt;&lt; \"Value removed from stack: \" &lt;&lt; value &lt;&lt; endl;\r\n    } catch (const char* err_msg) {\r\n      cout &lt;&lt; err_msg &lt;&lt; endl;\r\n    }\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple implementation of Stack with exception handling in C++ using templates<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Algorithms: Stack","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,150,9],"tags":[148,149,153,151,154,152],"class_list":["post-577","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-c-development","category-development","tag-algorithms-2","tag-cpp","tag-exception","tag-stack","tag-template","tag-throw"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-9j","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=577"}],"version-history":[{"count":3,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}