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

Stack of Array

The document contains a C++ program that implements a stack using an array. It provides functions to push, pop, and display elements in the stack, along with a menu-driven interface for user interaction. The program includes error handling for stack overflow and underflow conditions.

Uploaded by

Amnah Issa
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)
6 views

Stack of Array

The document contains a C++ program that implements a stack using an array. It provides functions to push, pop, and display elements in the stack, along with a menu-driven interface for user interaction. The program includes error handling for stack overflow and underflow conditions.

Uploaded by

Amnah Issa
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

Stack of array

#include <iostream>
using 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]=val;
}}
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 element 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"<<endl;
do{
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch){
case 1:{

cout<<"Enter value to be pushed"<<endl;

cin>>val;

push(val);

break;

Case 2: {

Pop();

break;

Case 3: {

display();

break;

Case 4: {

Cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid choice"<<endl;

}}

}while(ch!=4);

retutn 0;

You might also like