Question

In: Computer Science

write a c++ program to perform the following operations on stack of Integers (Array Implementation of...

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

Solutions

Expert Solution

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.


Related Solutions

write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
Implementing a Stack Write a program that implements a stack of integers, and exercises the stack...
Implementing a Stack Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Write a program in C to perform the following: Generates an array of 10 double random...
Write a program in C to perform the following: Generates an array of 10 double random values between 1.0 and 100.0 – assume these are prices for 10 items that a store sells Generates an array of 10 integer random values between 0 and 200 – assume that these are the number of items/units (first array) sold Generate another array called “itemSale” which would contain the total sale for each item. Calculate the total sale for this store and display...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; else return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results Submit your code and report here.
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a program that implements a stack of integers, and exercises the stack based on commands...
Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws runtime_error("stack is empty")...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT