User Menu

Login



Share with

Stack PDF Print E-mail

Stack
Stack is one of the important data structures that every computer programmer should be aware of. It follows the simple LIFO (Last In First Out) principle. Implementation of stack can be done in many ways. One of the simplest way is using Arrays. Here an array is initialized to a maximum value first, lets call it capacity. As and when we push elements onto the array, its size will get increased. As and when we pop elements from the array, its size will get decreased.

Stack

Array Implementation of stack

//Program for Stack implementation through Array
#include <iostream.h>
#include<ctype.h>
#include<conio.h>

#define MAXSIZE 3
int stack[MAXSIZE];
int top;  //index pointing to the top of stack

int main()
{
void push(int);
int pop();
int item,num;
cout<<"Enter the data to stack.";
cin>>num;
push(num);
cout<<"Enter the data to stack.";
cin>>num;
push(num);
cout<<"Enter the data to stack.";// Use to check stack full condition
cin>>num;
push(num);
cout<<"Enter the data to stack.";
cin>>num;
push(num);
item=pop();
cout<<endl<<"Top Element of the stack is "<<item;
item=pop();
cout<<endl<<"Top Element of the stack is "<<item;
item=pop();
cout<<endl<<"Top Element of the stack is "<<item;
item=pop(); // use to check stack empty condition
return 0;
}
void push(int y)
{
   if(top==MAXSIZE)
   {
   cout<<endl<<"STACK FULL";
   return;
   }
   else
   {
   top++;
   stack[top]=y;
   }
}
int pop()
{
int a;
top--;
   if(top<0)
   {
   cout<<endl<<"STACK EMPTY";
   return 0;
   }
   else
   {
   a=stack[top];
   }
return a;
}

Stack implementation as Linked List

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
      int data;
      node *link;
};
node *top;
int main()
{
void push(int);
void pop();
void display();
int wish, num,will,a;
wish = 1;
top = NULL;
clrscr();
cout<<"Program for Stack as Linked List demo.";
while(wish == 1)
      {
cout<<endl<<"Main Menu \n1.Enter data in stack\n2.Delete from stack\n3. Display Stack\n0. For Exit";
      cin>>will;
      switch(will)
            {
            case 1:
                  cout<<endl<<"Enter the data";
                  cin>>num;
                  push(num);
                  break;
            case 2:
                  pop();
                  break;
            case 3:
                  display();
                  break;
            case 0:
                  exit(0);
            }
      cout<<endl<<"Do you want to continue, press 1";
      cin>>wish;
      }
return 0;
}
//THIS FUNCTION INSERT NODE IN THE TOP OF THE STACK
void push(int y)
{
node *x;
x= new node();
x->data = y;
x->link = top;
top = x;
}
//THIS FUNCTION REMOVES TOP NODE FROM THE STACK AND RETURNS ITS VALUE
void pop()
{
int a;
if(top==NULL)
{
cout<<"STACK EMPTY...";
return ;
}
else
{
a=top->data;
cout<<endl<<"The value at the top of stack is "<<a;
top=top->link;
}
}
//THIS FUNCTION DISPLAY ELEMENTS OF STACK
void display()
{

node * temp;
temp = top;

             if(top == NULL)
      {
      cout<<"Empty Stack";
      return;
      }
      while(temp!=NULL)
      {
      cout<<temp->data<<endl;
      temp=temp->link;
      }
}

scroll back to top