What is Stack - Operations, Array representation of Stack in C

In this tutorial, we learn stack data structure its operations, array representation of stack in c. Data structure follows a particular order in which the operations are performed.

What is Stack

Stack is a linear data structure in which objects are just like a pile of the CD on CD spindle(whose one end is closed and other is open) kept on top of each other.

Think about the things you can do with such a pile of CD: 
  • Put a new CD on top
  • Remove the CD plate
The insert(PUSH) and delete(POP) operation occur only at one end of the stack. Stack work on LIFO(Last IN first OUT) order the element which is at the top will delete first and top pointer represent the last element of the stack. 

Below is a simple representation of a stack with PUSH and POP operations: 

Data Structure and Algorithms - Stack

Stack Operations

  1. PUSH(Insert): If the top of the stack at top=n-1, where n=size of the stack then the stack is overflow and insertion is not possible. If the stack does not overflow then the pointer top is firstly incremented by one or top=top+1, and then the element is inserted.
  2. POP(Delete): If the top of the stack at top=-1, then the stack is underflow and deletion is not possible. If the stack does not underflow then print the deleted element to know which element is deleted from a stack, and then top=top-1.
  3. Display: Display is possible when any element present in the stack or top!=-1 and not possible if the stack is empty.

Array representation of Stack in C 

using namespace std;
#include<iostream> //iostream as header
#define size 100

int stack[size],choice,n,top,item,i; //gobal variables declare

void push(); //functions declare
void pop();
void display();

int main()
{
    top=-1;
    cout<<"\n Enter the size of STACK[MAX=100]:";
    cin>>n;
    cout<<"\n\t STACK OPERATIONS USING ARRAY";
    cout<<"\n\t--------------------------------";
    cout<<"\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT";
    while(1)
    {
        cout<<"\n Enter the Choice:";
        cin>>choice;
        switch(choice)
        {
            case 1:
            {
                push();
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                display();
                break;
            }
            case 4:
            {
                exit(0);
                break;
            }
            default:
            {
                printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
            }
                 
        }
    }
    return 0;
}
void push() //function definition 
{
    if(top>=n-1)
    {
        cout<<"\n\tSTACK is Overflow";
    }
    else
    {
        cout<<" Enter a value to be Pushed:";
        cin>>item;
        top++;
        stack[top]=item;
    }
}
void pop()
{
    if(top<=-1)
    {
        cout<<"\n\t Stack is Underflow";
    }
    else
    {
        cout<<"\n\t The Popped elements is"<=0)
    {
        cout<<"\n The Elements in STACK \n";
        for(i=top; i>=0; i--)
            cout<<"\n Stack[i]";
        cout<<"\n Press Next Choice";
    }
    else
    {
        cout<<"\n The STACK is empty";
    }
    
}

OUTPUT



 Enter the size of STACK[MAX=100]:5

         STACK OPERATIONS USING ARRAY
        --------------------------------
         1.PUSH
         2.POP
         3.DISPLAY
         4.EXIT
 Enter the Choice:3

 The STACK is empty
 Enter the Choice:2

         Stack is Underflow
 Enter the Choice:1
 Enter a value to be Pushed:10

 Enter the Choice:1
 Enter a value to be Pushed:20

 Enter the Choice:1
 Enter a value to be Pushed:30

 Enter the Choice:1
 Enter a value to be Pushed:40

 Enter the Choice:1
 Enter a value to be Pushed:50

 Enter the Choice:1

        STACK is Overflow
 Enter the Choice:3

 The Elements in STACK
50
40
30
20
10

 Press Next Choice
 Enter the Choice:2

         The Popped elements is50
 Enter the Choice:2

         The Popped elements is40
 Enter the Choice:3

 The Elements in STACK
30
20
10

 Press Next Choice
 Enter the Choice:2

         The Popped elements is30
 Enter the Choice:2

         The Popped elements is20
 Enter the Choice:2

         The Popped elements is10
 Enter the Choice:2

         Stack is Underflow
 Enter the Choice:3

 The STACK is empty
 Enter the Choice:4


If you have any problem stack - operations, array representation of stack in c than comment on your problem.

Happy Coding !!

No comments:

If you have any doubts, Please let me know

Powered by Blogger.