{"id":26718,"date":"2017-12-24T15:17:20","date_gmt":"2017-12-24T09:47:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26718"},"modified":"2017-12-24T15:20:14","modified_gmt":"2017-12-24T09:50:14","slug":"cpp-algorithm-introduction-stack","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cpp-algorithm-introduction-stack\/","title":{"rendered":"Cpp Algorithm &#8211; Introduction to Stack"},"content":{"rendered":"<p>Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).<span id=\"more-2713\"><\/span><\/p>\n<p>Mainly the following three basic operations are performed in the stack:<\/p>\n<ul>\n<li><strong>Push: <\/strong>Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.<\/li>\n<li><strong>Pop:<\/strong> Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.<\/li>\n<li>Peek or Top: Returns top element of stack.<\/li>\n<li><strong>isEmpty: <\/strong>Returns true if stack is empty, else fals.<\/li>\n<li><strong>How to understand a stack practically?<\/strong><br \/>\nThere are many real life examples of stack. Consider the simple example of plates stacked over one another in canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO\/FILO order.<strong>Time Complexities of operations on stack:<\/strong><\/li>\n<li>push(), pop(), esEmpty() and peek() all take O(1) time. We do not run any loop in any of these operations.<\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li><strong>Applications of stack:<\/strong>\n<ul>\n<li>Balancing of symbols<\/li>\n<li>Infix to Postfix \/Prefix conversion<\/li>\n<li>Redo-undo features at many places like editors, photoshop.<\/li>\n<li>Forward and backward feature in web browsers<\/li>\n<li>Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem.<\/li>\n<li>Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen problem and sudoku solver<\/li>\n<\/ul>\n<p>&nbsp;<\/li>\n<li><strong>Implementation:<\/strong><br \/>\nThere are two ways to implement a stack:<\/p>\n<ul>\n<li>Using array<\/li>\n<li>Using linked list<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<ul>\n<li><strong>Implementing Stack using Arrays<\/strong><\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/* C++ program to implement basic stack<br\/>   operations *\/<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/> <br\/>#define MAX 1000<br\/> <br\/>class Stack<br\/>{<br\/>    int top;<br\/>public:<br\/>    int a[MAX];    \/\/Maximum size of Stack<br\/> <br\/>    Stack()  { top = -1; }<br\/>    bool push(int x);<br\/>    int pop();<br\/>    bool isEmpty();<br\/>};<br\/> <br\/>bool Stack::push(int x)<br\/>{<br\/>    if (top &gt;= MAX)<br\/>    {<br\/>        cout &lt;&lt; &quot;Stack Overflow&quot;;<br\/>        return false;<br\/>    }<br\/>    else<br\/>    {<br\/>        a[++top] = x;<br\/>        return true;<br\/>    }<br\/>}<br\/> <br\/>int Stack::pop()<br\/>{<br\/>    if (top &lt; 0)<br\/>    {<br\/>        cout &lt;&lt; &quot;Stack Underflow&quot;;<br\/>        return 0;<br\/>    }<br\/>    else<br\/>    {<br\/>        int x = a[top--];<br\/>        return x;<br\/>    }<br\/>}<br\/> <br\/>bool Stack::isEmpty()<br\/>{<br\/>    return (top &lt; 0);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    struct Stack s;<br\/>    s.push(10);<br\/>    s.push(20);<br\/>    s.push(30);<br\/> <br\/>    cout &lt;&lt; s.pop() &lt;&lt; &quot; Popped from stack\\n&quot;;<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Pros:<\/strong> Easy to implement. Memory is saved as pointers are not involved.<br \/>\n<strong>Cons:<\/strong> It is not dynamic. It doesn\u2019t grow and shrink depending on needs at runtime.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>10 pushed to stack\r\n20 pushed to stack\r\n30 pushed to stack\r\n30 popped from stack\r\nTop item is 20<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ Algorithm -Introduction to Stack &#8211; Data Structure &#8211; Stack is a linear data structure which follows a particular order in which the operations <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,79607],"tags":[79664,75619,79731,70452,75633,79663,79660,70447,79661,79662,75604],"class_list":["post-26718","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-stack","tag-application-of-queue-in-data-structure","tag-application-of-stack-in-data-structure","tag-introduction-to-stack-in-8086","tag-push-and-pop-operation-in-stack-in-data-structure","tag-queue-in-data-structure","tag-stack-data-structure-java","tag-stack-in-data-structure-pdf","tag-stack-in-data-structure-with-example","tag-what-is-queue-in-data-structure","tag-what-is-stack-in-c","tag-what-is-stack-in-data-structure"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26718"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26718\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}