0% found this document useful (0 votes)
68 views2 pages

Stack:: Void Push (Int Val) (If (Top n-1) Else (

This C++ program implements a stack data structure using an array. It defines functions to push, pop, and display elements in the stack. The main function takes user input to select an operation and calls the corresponding function. It allows pushing integers onto the stack until it is full, popping elements off until it is empty, and displaying all elements currently in the stack.
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)
68 views2 pages

Stack:: Void Push (Int Val) (If (Top n-1) Else (

This C++ program implements a stack data structure using an array. It defines functions to push, pop, and display elements in the stack. The main function takes user input to select an operation and calls the corresponding function. It allows pushing integers onto the stack until it is full, popping elements off until it is empty, and displaying all elements currently in the stack.
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/ 2

// Stack:

* ude <iostream>
?Sing namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<“Stack Overflow"<<endl;
else {
top++;
stack[top]~va1;
)
}
void pop() (
if(top<--1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
) else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<end1;
do (
cout<<”Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:”<<endl;
cin>>val;
push(va1);
break:
}
case 2: {
pop();
break;
}
case 3: {
diSPlaY():
break;
)
case 4: {
cout<<'Exit"<<endl;
break; -"'HLI
default: {
cout < < " I n v a l i d C h o i c e " < < e n d l ;
}
}
}while(ch!=4);
return 0;

You might also like