In: Computer Science
Examples:
The Lab is divided into a set of example and exercise activities related with this topic and students are required to perform all activities and show (and discuss) the output of activities to the instructor.
Example 1: Implement the Stack Push Operation with Overflow condition
Declare an array of size 5 and implement the Push Operation with overflow condition; you can type the following code as a hint:
#include<iostream>
using namespace std ;
#define MAX 5
struct Stack {
int S[MAX];
int top;
};
Stack st;
st.top = -1;
int item;
if(st.top == (MAX-1))
cout<<"Stack Overflow\n" ;
else
{
cout<< "Enter the item to be pushed in stack : " ;
cin>> item ;
st.top++;
st.S[st.top] = item;
}
Complete this implementation as a full program to PUSH as many elements as the user wants (or till the Stack is full).
Example 2: Implement the Stack Pop Operation with Underflow condition
Declare an array of size 5 and implement the Pop operation; you can type the following code as a hint:
int item;
if (st.top == -1) /* stack empty condition */
cout<< "Stack Underflow\n" ;
else
{
item = st.S[st.top];
st.top-- ;
cout<< "Element popped from stack is :" << item ;
}
Complete this implementation as a full program to POP as many elements as the user wants (or till the Stack is empty).
Example 3: Display the contents of Stack
Display the contents of Stack; you can type the following code as a hint:
int i;
if(st.top == -1)
cout<<"Stack is empty\n" ;
else
{
cout<< "Stack elements :\n" ;
for(i = st.top; i >=0; i--)
cout<< st.S[i] ; }
Exercise
Combine all the three examples above to implement one complete program of Stack
CODE -
#include<iostream>
using namespace std ;
#define MAX 5
// DEFINING THE STRUCT STACK
struct Stack {
int S[MAX];
int top;
};
// MAIN FUNCTION
main()
{
Stack st;
st.top = -1;
// LOOP TO RUN UNTIL USER EXITS.
while(true)
{
// DISPLAYING THE STACK OPERATIONS MENU
cout << "\nStack Operations Menu" << endl << endl;
cout << "1. Push \n2. Pop \n3. Display Stack \n4. Exit" << endl;
// TAKING CHOICE AS INPUT FROM USER.
cout << "Enter your choice: ";
int choice;
cin >> choice;
if(choice == 1)
{
int item;
// DISPLAYING "STACK OVERFLOW" MESSAGE IF STACK IS FULL AND USER TRIES TO PUSH ELEMENT TO THE STACK
if(st.top == (MAX-1))
cout<<"\nStack Overflow\n" ;
// TAKING ELEMENT AS INPUT AND PUSHING IT TO THE STACK IF THE STACK IS NOT FULL
else
{
cout<< "\nEnter the item to be pushed in stack : " ;
cin>> item ;
st.top++;
st.S[st.top] = item;
}
}
else if(choice == 2)
{
int item;
// DISPLAYING "STACK UNDERFLOW" MESSAGE IF STACK IS EMPTY AND USER TRIES TO POP ELEMENT FROM THE STACK
if (st.top == -1) /* stack empty condition */
cout<< "\nStack Underflow\n" ;
// POPPING OUT TOP ELEMENT FROM THE STACK IF THE STACK IS NOT EMPTY
else
{
item = st.S[st.top];
st.top-- ;
cout<< "\nElement popped from stack is :" << item << endl;
}
}
else if(choice == 3)
{
int i;
// DISPLAYING "STACK IS EMPTY" MESSAGE IF STACK IS EMPTY AND USER TRIES TO DISPLAY THE STACK
if(st.top == -1)
cout<<"\nStack is empty\n" ;
// DISPLAYING THE STACK ELEMENTS IF STACK IS NOT EMPTY
else
{
cout<< "\nStack elements :\n" ;
for(i = st.top; i >=0; i--)
cout<< st.S[i] << " ";
cout << endl;
}
}
// EXITING THE LOOP AND ULTIMATELY THE PROGRAM IF USER ENTERS 4 AS THEIR CHOICE
else if(choice == 4)
break;
// DISPLAYING ERROR MESSAGE IF USER ENTERS INVALID CHOICE.
else
{
cout << "\nInvalid Choice!" << endl << endl;
}
}
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.