0% found this document useful (0 votes)
19 views4 pages

Aditya KK 4

The document contains a C++ implementation of a stack data structure with methods for pushing, popping, and displaying elements. It includes a main menu for user interaction to perform these stack operations. The stack is dynamically allocated and has a destructor to free memory, although there are issues in the destructor implementation.

Uploaded by

neelmarne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Aditya KK 4

The document contains a C++ implementation of a stack data structure with methods for pushing, popping, and displaying elements. It includes a main menu for user interaction to perform these stack operations. The stack is dynamically allocated and has a destructor to free memory, although there are issues in the destructor implementation.

Uploaded by

neelmarne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Aditya Khaire

Roll:24

#include<iostream>
#define size 10
using namespace std;

// Define Stack class


class stack
{
int *s;
int top; // Index of the top element in the stack

public:
// Constructor to initialize an empty stack
stack()
{
cout<<"\n I am in constructor";
s=new int[size];//creation of dynamic stack
top=-1;
}
void push(int item)
{
top++; // Add element to the top of the stack
and increment

s[top]=item;
}
int pop()
{
int item;
if(!stempty())
{
item=s[top];
top--;
return item;
}
else
cout<<"\n stack is empty";
return -1;
}
int stempty()
{
if(top==-1)
return 1;
else
return 0;

}
void display()
{
if(!stempty())
{
for(int i=0;i<=top;i++)
cout<<" "<<s[i];
}
else
cout<<"\n stack is empty";
}
~stack()
{
for(int i=0;i<size;i++)
{
delete []s;
delete s;
}
}
};

int main()
{
stack obj;
int choice,val;
char ans;
do
{
cout<<"\n\t Main Menu";
cout<<"\n 1.Push \n2.Pop \n3.Display";
cout<<"\n Enter Your choice :";
cin>>choice;
switch(choice)
{
case 1: cout<<"\n Enter the item to be pushed :";
cin>>val;
obj.push(val);
break;
case 2: cout<<"\n popped element :"<<obj.pop();
break;
case 3: obj.display();
break;
}
cout<<"\n Do you want to continue ?";
cin >>ans;
}while(ans=='y'||ans=='Y');
return 0;
}

You might also like