Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Code For Pushing and Poping Data Into A Stack

The document discusses a C++ program to implement a stack with push and pop functions. The program allows the user to choose pushing or popping integers from a stack of size 10 or displaying the stack. It includes function definitions for push, pop and display and a main function to run the stack operations based on user input.

Uploaded by

Tasneem Kubra
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)
10 views

Code For Pushing and Poping Data Into A Stack

The document discusses a C++ program to implement a stack with push and pop functions. The program allows the user to choose pushing or popping integers from a stack of size 10 or displaying the stack. It includes function definitions for push, pop and display and a main function to run the stack operations based on user input.

Uploaded by

Tasneem Kubra
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/ 3

Write a program in C++ to push and pop the integer data elements on the stack.

The stack size must be 10. The user must be given choice:
1. To push the number on stack,
2. To Pop the number from stack,
3. To display the data lying on stack.

#include<iostream>
using namespace std;
void star();
class stack{
private:
int top;
int arr[10];
public:
stack()
{
top=-1;
}
void push(int);
int pop();
void display();
};
void stack::push(int v)
{
if(top==9)
cout<<"Stack is full.\n";
else
{
arr[++top]=v;
cout<<endl<<v<<" has been placed.\n";
}
}
int stack::pop()
{
if(top==-1)
{
cout<<"Stack is empty.\n";
return NULL;
}
else
{
return arr[top--];
}
}
void stack::display()
{
if(top==-1)
{
cout<<"Stack has no data.\n";
}
else
{
cout<<"The stack has following data.\n\n";
for(int i=top;i>=0;i--)
{
cout<<"\t"<<arr[i]<<"\n";
}
}
}
int main()
{
star();
cout<<"Program objective : This program performs \n\tPushing \n\tPoping
\n\tDisplaying \nat a stcak acooring to user input.\n";
star();
stack s1;
cout<<"\n\n-------------------------STACK IMPLEMENTATION---------------------\n\n";
cout<<"______________Enter 1 To push\n";
cout<<"______________Enter 2 To Pop\n";
cout<<"______________Enter 3 To display\n";
cout<<"______________Enter 0 To exit\n\n";
cout<<"Enter choice : ";
int chk;
cin>>chk;
cout<<endl;
while(chk!=0)
{
if(chk==1)
{
cout<<"---------------Pushing the number--------------\n\n";
cout<<"Enter the number to push in stack : ";
int num;
cin>>num;
s1.push(num);
}
else if(chk==2)
{
cout<<"---------------Poping the number--------------\n\n";
cout<<s1.pop()<<" has been poped.\n";
}
else if(chk==3)
{
cout<<"---------------Displaying the numbers--------------\n\n";
s1.display();
}
else
cout<<"\ainvalid choice.\n";
cout<<"\n______________Enter 1 To push\n";
cout<<"______________Enter 2 To Pop\n";
cout<<"______________Enter 3 To display\n";
cout<<"______________Enter 0 To exit\n\n";
cout<<"Enter choice : ";
cin>>chk;
cout<<endl;
}
}
void star()
{
cout<<endl;
for(int i=1; i<37;i++)
{
cout<<"**";
}
cout<<endl;
}

You might also like