In: Computer Science
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX)
(i) Push an Element on to stack
(ii) Pop an Element from stack
(iii) Demonstrate how stack can be used to check Palindrome
(iv) Display the status of stack
(v) Exit
Hi,
Please find the answer below:
-------------------------------
C++ Program:
#include<iostream>
#define MAX 10 // Stack with maximum size
using namespace std;
void push(int stack[],int item);
int pop(int stack[]);
void palindromeCheck(int stack[]);
void displayStack(int stack[]);
//variables used
int stackOfIntegers[MAX];
int item;
int choice;
int top=-1;
int stackCount=0;
/************************************
* Stack push operation
*************************************/
void push(int stack[],int item)
{
if(top==(MAX-1))
cout << "ERROR
:Stack Overflow" << endl;
else
{
top=top+1;
stack[top]=item;
stackCount++;
}
}
/************************************
* Stack pop operation
*************************************/
int pop(int stack[])
{
int itemTop;
if(top==-1)
cout << "ERROR :
Stack Underflow" << endl ;
else
{
itemTop=stack[top];
top=top-1;
stackCount--;
cout << "Pop
element: " << itemTop << endl ;
}
return itemTop;
}
/************************************
* Check Stack is Palindrome or Not
*************************************/
void palindromeCheck(int stack[])
{
int count=0;
int palinCount=stackCount;
if(top==-1)
cout << "Stack is
empty" << endl;
else
{
for(int i=0;
i<palinCount; i++)
{
if(stack[i]==pop(stack))
count=count+1;
}
if(palinCount==count)
cout << "TRUE: Stack contents are Palindrome" << endl
;
else
cout << "FALSE: Stack contents are not Palindrome" <<
endl;
}
}
/************************************
* display Stack
*************************************/
void displayStack(int stack[])
{
if(top==-1)
cout << "Stack is
Empty" << endl;
else
cout << "Stack:"
<< endl;
for(int i=top; i>=0; i--)
{
cout << stack[i]
<< endl;
}
}
/************************************
* Main
*************************************/
int main()
{
do
{
cout
<<"-----MENU-----\n";
cout <<"1. Push an
Element on to stack.\n";
cout <<"2. Pop an
Element from stack.\n";
cout <<"3. Demo
Stack Usage to check Palindrome.Stack will be empty!\n";
cout <<"4. Display
the status of stack.\n";
cout <<"5.
Exit\n";
cout
<<"--------------\n";
cout <<"Enter your
Choice:";
cin >>
choice;
switch(choice)
{
case 1:
cout << "Enter an element:";
cin >> item;
push(stackOfIntegers,item);
break;
case 2:
item=pop(stackOfIntegers);
break;
case 3:
palindromeCheck(stackOfIntegers);
break;
case 4:
displayStack(stackOfIntegers);
break;
case 5:
exit(0);
default:
cout << "Invalid Choice";
break;
}
}
while(choice!=5);
return 0;
}
Program Output:
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:1
Enter an element:7
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:1
Enter an element:6
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:1
Enter an element:7
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:1
Enter an element:6
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:4
Stack:
6
7
6
7
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:2
Pop element: 6
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:4
Stack:
7
6
7
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:4
Stack:
7
6
7
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:3
Pop element: 7
Pop element: 6
Pop element: 7
TRUE: Stack contents are Palindrome
-----MENU-----
1. Push an Element on to stack.
2. Pop an Element from stack.
3. Demo Stack Usage to check Palindrome.Stack will be empty!
4. Display the status of stack.
5. Exit
--------------
Enter your Choice:5
Process returned 0 (0x0) execution time : 49.139
s
Press any key to continue.
Screenshot:
-------------------------------
Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.