0% found this document useful (0 votes)
14 views6 pages

Stack

The document provides a C++ implementation of a stack using object-oriented programming principles. It includes methods for pushing, popping, peeping, changing, and displaying stack elements, along with a simple user interface for interaction. The stack has a maximum size of 5 and handles overflow and underflow conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Stack

The document provides a C++ implementation of a stack using object-oriented programming principles. It includes methods for pushing, popping, peeping, changing, and displaying stack elements, along with a simple user interface for interaction. The stack has a maximum size of 5 and handles overflow and underflow conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.OPERATION OF STACK IN C++ USING OOP CONTENT.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
class stack
{
int n,top;
int s[20];
public:
stack()
{
n=5;
top=-1;
}
stack(int no)
{
n=no;
top=-1;
}
void push(int data)
{
if(top==n-1)
{
cout<<endl<<"STACK OVERFLOW";
return;
}
top++;
s[top]=data;
}
int pop()
{
int data;
if(top==-1)
{
cout<<endl<<"STACK UNDERFLOW";
return 0;
}
data=s[top];
top--;
return data;
}
int peep()
{
int i,data;
cout<<endl<<"WHICH ELEMENT YOU WANT :";
cin>>>i;
if((top-i+1)<0)
{
cout<<endl<<"STACK UNDERFLOW IN PEEP";
return 0;
}
data=s[top-i+1];
return data;
}
void change(int data)
{
int i;
cout<<endl<<"which element you want to change :";
cin>>i;
if((top-i+1)<0)
{
cout<<endl<<"stack underflow on change";
}
s[top=i+1]=data;
}
void display()
{
if(top==-1)
cout<<endl<<"STACK IS EMPTY";
else
{
cout<<endl<<"THE CONTENTS OF STACK IS :";
for(int i=top;i>0;i--)
cout<<"\n\t"<<s[i];
}
}
};
void main()
{
int e,ch;
stack s;
do
{
clrscr();
cout<<"\n\t\t:-STACK IMPLEMENTATION:-";
cout<<"\n\t1.push";
cout<<"\n\t2.pop";
cout<<"\n\t3.display";
cout<<"\n\t4.peep";
cout<<"\n\t5.change";
cout<<"\n\t6.exit";
cout<<"\n\tenter choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nENTER ELEMENT: ";
flushall();
cin>>e;
s.push(e);
break;
case 2:
e=s.pop();
if(e!=0)
cout<<"THE DELETED ELEMENT IS:";
break;
case 3:
s.display();
break;
case 4:
e=s.peep();
if(e!=0)
cout<<"\nTHE SELECTED ELEMENT IS:";
break;
case 5:
cout<<"\nENTER NEW ELEMENT :";
flushall();
cin>>e;
s.change(e);
break;
case 6:
exit(0);
default:
cout<<"\nwrong choice";
}
getch();
}while(ch!=6);
}
OUTPUT:-

You might also like