0% found this document useful (0 votes)
17 views

Aim: WAP To Perform Push, Pop, and Traverse in A Stack of Integers

The document describes a C++ program to implement a stack of integers using functions like push(), pop(), and display(). The push() function inserts an element into the stack, pop() removes an element, and display() prints the current elements. The main() function prompts the user to insert or delete elements, calling the appropriate functions, and prints the stack after each operation. It checks for overflow and underflow conditions.

Uploaded by

name
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)
17 views

Aim: WAP To Perform Push, Pop, and Traverse in A Stack of Integers

The document describes a C++ program to implement a stack of integers using functions like push(), pop(), and display(). The push() function inserts an element into the stack, pop() removes an element, and display() prints the current elements. The main() function prompts the user to insert or delete elements, calling the appropriate functions, and prints the stack after each operation. It checks for overflow and underflow conditions.

Uploaded by

name
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/ 3

Aim: WAP to perform push, pop, and traverse in a stack of

integers.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

int pop(int [], int &);


int push(int [], int &, int);
void display(int [], int);
const int SIZE = 50;

void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key to
exit..\n";
getch();
exit(1);
}
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
cout<<"Now the deletion of elements starts..\n";
ch='y';
while(ch=='y' || ch=='Y')
{
res = pop(stack, top);
if(res==-1)
{
cout<<"\nUnderflow..!!..Aborting..!!..Press a key to
exit..\n";
getch();
exit(2);
}
else
{
cout<<"\nElement deleted is: "<<res<<endl;
cout<<"\nThe Stack now is:\n";
display(stack, top);
}
cout<<"Want to delete more ? (y/n).. ";
cin>>ch;
}
getch();
}

int push(int stack[], int &top, int elem)


{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}

int pop(int stack[], int &top)


{
int ret;
if(top==-1)
{
return -1;
}
else
{
ret=stack[top];
top--;
}
return ret;
}

void display(int stack[], int top)


{
if(top==-1)
{
return;
}
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}
Output:

You might also like