Showing posts with label stack. Show all posts
Showing posts with label stack. Show all posts

Sunday, 2 July 2017

C++ program of Stack

In this program we will discuss about working of stack and we will see how to insert and delete data from stack.

//Program starts
#include<iostream>
#include<conio.h>
using namespace std;

class stack{
          private:
                    int d,n,a[5],top;
          public:
                    stack();
                    void create();
                    void push();
                    void pop();
                    void show();
};
stack::stack()
{
          n=5;
          top=-1;
}

void stack::create()
{
          for(int i=0;i<n;i++)
          {
                    cout<<"Enter data at ["<<i<<"] : ";
                    cin>>a[i];
          }
         
          top=4;
}

void stack::push()
{
          if(top==4)
          {
                    cout<<"\n Stack is full \n";
          }
          else
          {
                    cout<<"Enter data : ";
                    cin>>d;
                    top++;
                    a[top]=d;
          }
}

void stack::pop()
{
          if(top==-1)
          {
                    cout<<"\n Stack underflow \n";
          }
          else
          {
                    top--;
          }
}

void stack::show()
{
          if(top==-1)
          {
                    cout<<"\nStack is empty\n ";
          }
          else
          {
         
          for(int i=0;i<=top;i++)
          {
                    cout<<"Data at ["<<i<<"] = "<<a[i]<<endl;
          }
}
}

int main()
{
          int c;
          stack s1;
          s1.create();
         
          do{
                    cout<<"\nPress 1 to insert data\nPress 2 to delete data\n Press 3 to show data\nPress 4 to exit\nValue = ";
                    cin>>c;
                   
                    switch(c)
                    {
                              case 1:
                                        s1.push();
                                        break;
                                       
                              case 2:
                                        s1.pop();
                                        break;
                                       
                              case 3:
                                        s1.show();
                                        break;
                                       
                              case 4:
                                        return 0;
                                        break;
                    }       
          }
          while(1);
}

 OUTPUT:




Share:

Saturday, 4 March 2017

C++ program of data insertion and deletion in stack

In this program we will learn how to insert and delete data in a stack.
Stack works on the principal of LIFO last in first out so new data can only be inserted and delete from the last index.
e.g if we take the array of 5 elements, 0,1,2,3,4. so the data existing on the last index (4) an only be inserted or deleted.


The code is given below.

//Code starts 

#include<iostream>
#include<conio.h>

using namespace std;

class stack{

private:
int a[10],top;

public:
void create();   //Function of inserting data
void print();     //Function of printing data
void push(int data);   //Function of inserion
void pop();    //Function of deletion
stack();     //Constructor
};

stack::stack()
{
top=-1;
}

void stack::create()
{
for(int i=0;i<5;i++)
{
cout<<"Enter data in index ["<<i<<"] : ";
cin>>a[i];
}
top=4;

}


void stack::print()
{
cout<<"Your entered data "<<endl;
for(int i=0;i<=top;i++)
{
cout<<"Data at index ["<<i<<"] : "<<a[i]<<endl;
}
}

void stack::push(int data)
{
cout<<"Insert data in stack : ";
cin>>data;
if(top==5)
{
cout<<"Stack is full";

}
else{
top++;

a[top]=data;
}
cout<<"Stack after insertion of "<<data<<" at index 5"<<endl;

}


void stack::pop()   
{
if(top==-1)
{
cout<<"Stack is full";
}
else{
cout<<"Data after insertion of index 5"<<endl;

top--;
}

}

int main()   //Main function starts
{
stack s; //Creating object of class

int x,d;  //Declaring two variable

do{

cout<<"Enter 1 to enter data : "<<endl;
cout<<"Enter 2 to show entered data : "<<endl;
cout<<"Enter 3 to insert new data in stack : "<<endl;
cout<<"Enter 4 to delete data from stack : "<<endl;
cout<<"Enter 5 to exit from program : "<<endl;
cout<<"      Value =     ";

cin>>x;
cout<<"---------------------------------------------"<<endl;

switch (x) //Starting switch function to switch the given value from user


 {

 case 1:
 
  s.create();   //Calling function of create from class
  cout<<"---------------------------------------------"<<endl;

 break;
 
 case 2:
 
   s.print();      //Calling function of print from class
   cout<<"---------------------------------------------"<<endl;

 break;
 
 case 3:
 
  s.push(d);    //Calling function of push from class
  cout<<"---------------------------------------------"<<endl;

  s.print();    //Calling function of print from class
        cout<<"---------------------------------------------"<<endl;


 break;
 
 case 4:
 
  s.pop();    //Calling function of pop from class
      cout<<"---------------------------------------------"<<endl;

  s.print();   //Calling function of print from class
  cout<<"---------------------------------------------"<<endl;

break;

case 5:

cout<<"Program terminated "<<endl;
return 1;  //Returning value 1 to terminate program

break;

}
}
while(1);  //Program will be ended when it returns value 1

getch();
}
//Code ends

OUTPUT:




Share: