Saturday 2 February 2019

stack program in c++ | stack meaning | program | output


stack Program in c++,


stack is  a data structure, basically stack is a linear data structure represented in real physical stack or pile.

In stack data structure insertion and deletion of the element is done only one end called as top.  The basic implementation of the stack is LIFO(Last in First Out), which means the element which are entered first is popped in the last.











Stack Program using class:





#include<iostream>
using namespace std;
#define size 5
class stack
{
int stack[size];
int tos;

public:
void temp();
void push();
int pop();
void display();
};
void stack::temp()
{
tos=0;
}
void stack::push()
{
int i;
if(tos==size)
{
cout<<"stack overflow\n";
return;
}

cout<<"enter element\n";
cin>>i;
stack[tos]=i;
tos++;
}
int stack::pop()
{
if(tos==0)
{
cout<<"stack underflow\n";
return 1;
}

tos--;
cout<<"popped element is"<<stack[tos]<<"\t";
return 0;
}
void stack::display()
{
if(tos==0)
{
cout<<"stack empty\n";
return;
}
for(int i=0;i<tos;i++)
{
cout<<stack[i]<<"\t";
}
}
int main()
{
int ch,i;
stack s;
s.temp();
while(1)
{
cout<<"\n 1.push \n 2.pop \n 3.display \n 4. exit\n";
cout<<"enter the element\n";
cin>>ch;
switch(ch)
{
case 1: s.push();
break;
case 2: s.pop();
break;
case 3: s.display();
break;
case 4: exit(0);
default:
return 0;
}
}
}









Direct Download CPP Executable file

                                                                              Download

No comments:

Post a Comment