Last active
April 8, 2017 11:32
-
-
Save israr-ahmad/c10ba2dbb8ff615833e38761a3557a47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
bool isEmpty(int top); | |
bool isFull(int top,int maxSize); | |
void push(int &top,int stack[],int item,int maxSize); | |
void pop(int &top,int stack[]); | |
void print(int top,int stack[]); | |
int main() | |
{ | |
int top=-1; | |
int maxSize=5; | |
int stack[maxSize]; | |
int item; | |
int choice; | |
cout << "======================================================" <<endl; | |
cout << " \n Menu Driven Program & Stack array \n"; | |
cout << "======================================================" << endl << endl; | |
do | |
{ | |
cout << "|------------------------------------|" <<endl | |
<< "| 1- push a vlaue |" <<endl | |
<< "| 2- pop a value |" <<endl | |
<< "| 3- Exit |" <<endl | |
<< "|------------------------------------|" <<endl; | |
cout << "Enter your choice : "; | |
cin >> choice; | |
switch(choice) | |
{ | |
case 1: | |
if(isFull(top,maxSize)) | |
{ | |
cout << "stack is full: "; | |
} | |
else | |
{ | |
cout << "Enter item to be inserted: "; | |
cin >> item; | |
push(top,stack,item,maxSize); | |
print(top,stack); | |
} | |
break; | |
case 2: | |
if(isEmpty(top)) | |
{ | |
cout << "stack is empty\n "; | |
cout << "stack is empty\n "; | |
cout << "stack is empty\n "; | |
cout << "stack is empty\n "; | |
} | |
else | |
{ | |
pop(top,stack); | |
print(top,stack); | |
} | |
break; | |
} | |
}while (choice != 3 ); | |
return 0; | |
} | |
bool isEmpty(int top){ | |
if (top==-1){ | |
return true; | |
} | |
else | |
return false; | |
} | |
bool isFull(int top,int maxSize){ | |
if (top==maxSize-1){ | |
return true;} | |
else{ | |
return false; | |
} | |
} | |
void push(int &top,int stack[],int value,int maxSize){ | |
if (top==maxSize-1){ | |
cout<<"Stack is full"<<endl; | |
} | |
else{ | |
top=top+1; | |
stack[top]=value; | |
} | |
} | |
void pop(int &top,int stack []){ | |
if (top==-1){ | |
cout<<"underflow"<<endl; | |
} | |
else{ | |
stack[top]=0; | |
top=top-1; | |
} | |
} | |
void print(int top,int stack[]){ | |
for (int i=top;i>=0;i--){ | |
cout<<stack[i]<<endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Share and learn.
If you find any mistake so please tell me.