# Stack: Data Structure algorithm

 stack
Stack is a data structure which has ordered list of similar data type. Stack is a Last In First Out (LIFO) structure. In simple words, stack is a like a bag; whatever you put at last comes out first. Now, lets understand the algorithm for stack.

• start
• Display push or pop
• If push
• Check for Stack overflow
• If flase
• push data into stack
• Increment stack top pointer
• If true
• Display “Stack overflow”
• Stop
• If pop
• Check for Stack underflow
• If false
• Display stack top data
• Decrement  stack top pointer
• if true
• Display stack underflow
• Stop
• Stop
Lets understand this with an example of stack using simple array which stores integer type data.

``` //Program of Stack using array<br />//author Sujay K<br />//@ codesexplorer.blogspot.in<br />#include<stdio.h><br />#define size 10<br />int top=-1;<br />void push(int *,int );<br />int pop(int *);<br />int full();<br />int empty(); <br />int main()<br />{<br /> int a[10],c,r;<br /> do<br /> {<br /> printf("n Enter:n 1-&gt; Pushn 2-&gt; Popn 3-&gt; Stack top elementn 0-&gt; exit");<br /> scanf("%d",&amp;c);<br /> switch(c)<br /> {<br /> case 1: printf("n Enter data: ");<br /> scanf("%d",&amp;r);<br /> push(a,r);<br /> break;<br /> case 2: r=pop(a);<br /> printf("n Poped data: %d",r);<br /> break;<br /> case 3: if(full())<br /> printf("n Stack overflow");<br /> else if(empty())<br /> printf("n Stack Underflow");<br /> else<br /> printf("n Stack top element: %d",a[top]);<br /> break;<br /> case 0: break;<br /> default: printf("n You hit a wrong choice, Try again");<br /> }<br /> }while(c!=0);<br /> return 0;<br />}<br /><br />void push(int *s, int data)<br />{<br /> if(!full())<br /> s[++top]=data;<br /> else<br /> printf("n Stack Overflow");<br />}<br /><br />int pop(int *s)<br />{<br /> if(!empty())<br /> return s[top--];<br /> else<br /> {<br /> printf("n Stack underflow");<br /> return -1;<br /> }<br /> return 0;<br />}<br /><br />int full()<br />{<br /> if(top&gt;=size)<br /> return 1;<br /> else<br /> return 0;<br />}<br /><br />int empty()<br />{<br /> if(top&lt0)<br /> return 1;<br /> else <br /> return 0;<br />}<br />&nbsp; &nbsp;<br /> 1 //Program of Stack using array<br />//author Sujay K<br />//@ codesexplorer.blogspot.in<br />#include<stdio.h><br />#define size 10<br />int top=-1;<br />void push(int *,int );<br />int pop(int *);<br />int full();<br />int empty(); <br />int main()<br />{<br />    int a[10],c,r;<br />    do<br />    {<br />    printf("n Enter:n 1-&gt; Pushn 2-&gt; Popn 3-&gt; Stack top elementn 0-&gt; exit");<br />    scanf("%d",&amp;c);<br />    switch(c)<br />    {<br />        case 1: printf("n Enter data: ");<br />                scanf("%d",&amp;r);<br />                push(a,r);<br />                break;<br />        case 2: r=pop(a);<br />                printf("n Poped data: %d",r);<br />                break;<br />        case 3: if(full())<br />                   printf("n Stack overflow");<br />                else if(empty())<br />                   printf("n Stack Underflow");<br />                else<br />                   printf("n Stack top element: %d",a[top]);<br />                break;<br />        case 0: break;<br />        default: printf("n You hit a wrong choice, Try again");<br />    }<br />    }while(c!=0);<br />    return 0;<br />}<br /><br />void push(int *s, int data)<br />{<br />    if(!full())<br />        s[++top]=data;<br />    else<br />        printf("n Stack Overflow");<br />}<br /><br />int pop(int *s)<br />{<br />    if(!empty())<br />        return s[top--];<br />    else<br />    {<br />       printf("n Stack underflow");<br />       return -1;<br />    }<br />    return 0;<br />}<br /><br />int full()<br />{<br />    if(top&gt;=size)<br />       return 1;<br />    else<br />       return 0;<br />}<br /><br />int empty()<br />{<br />    if(top&lt0)<br />       return 1;<br />    else <br />       return 0;<br />}<br />&nbsp; &nbsp;<br /> ```

Hope you understood stack algorithm and program, Try your own using algorithm. We can  implement stack using dynamic memory allocation also. I will explain you about that when we discuss Linked lists. You can download this program by below link.