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:
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:
0 comments:
Post a Comment