Question

In: Computer Science

Examples: The Lab is divided into a set of example and exercise activities related with this...

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

Solutions

Expert Solution

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.


Related Solutions

Discuss how technology is used and related to boundary-less in police activities. Include an example in...
Discuss how technology is used and related to boundary-less in police activities. Include an example in your response. please provide a source
Oxfam activities and examples ?
Oxfam activities and examples ?
Skill Identification Exercise: Job-Related Examples Review the list of job-related skills below, Think about times or...
Skill Identification Exercise: Job-Related Examples Review the list of job-related skills below, Think about times or situations when you have used each skill, and note that example in the right-handcolumn Skill Example (job, academic and/or volunteer experience) The ability to solve problems (related to your major or concentration if possible) Strong hands-on mechanical skills; can take things apart and put them back together Strong analytical aptitude and attention to detail The ability to apply sound professional judgment The ability to...
Pre-Lab Exercise: 1. What is a primary standard substance? Give an example. 2. What is meant...
Pre-Lab Exercise: 1. What is a primary standard substance? Give an example. 2. What is meant by the term, equivalence point, during an acid-base titration? 3. What is an indicator? What role does it play in an acid-base titration? 4. Why is it necessary to standardize a NaOH solution? 5. Find the molar concentration of a NaOH solution if 0.3999 g of it is dissolved in water to yield 100.0 mL solution. Watch your sig. fig. 6. Consider the following...
Lab Practical #1 On-line Determination of Bacteria. In this lab exercise you will be given the...
Lab Practical #1 On-line Determination of Bacteria. In this lab exercise you will be given the bacteria and you will need to explain what it would look like on the different medias (NG is suitable for No growth). BA=blood agar, CNA = Columbia nutrient agar, MA=MacConkey agar, MSA=mannitol salt agar, HE=Hektoen enteric Agar Staphylococcus Aureus: Gram stain __________________. Growth on Media: BA _________ CNA__________ MA__________MSA_________HE__________ One place where the bacteria causes infection ________. Streptococcus pneumoniae: Gram stain __________________. Growth on...
Chapter 2 Exercise is divided in to 2 sections A and B. Data for this assignment...
Chapter 2 Exercise is divided in to 2 sections A and B. Data for this assignment is under Data files in module Ex2-30-e8.xls (for A) and Ex2-34-e8.xls (for B). See data under modules. A) The following data give the weekly amounts spent on groceries for a sample of households. $271 $363 $159 $ 76 $227 $337 $295 $319 $250 279 205 279 266 199 177 162 232 303 192 181 321 309 246 278 50 41 335 116 100 151...
1. Differentiate between investing activities, operating activities and financing activities. Please provide examples of each in...
1. Differentiate between investing activities, operating activities and financing activities. Please provide examples of each in your answer. 2. Discuss and explain the difference between a vertical analysis and a horizontal analysis. Which method do you feel is better?
Differentiate between investing activities, operating activities and financing activities. Please provide examples of each in your...
Differentiate between investing activities, operating activities and financing activities. Please provide examples of each in your discussion.
explain set operations and the laws of set theory with examples
explain set operations and the laws of set theory with examples
Objectives:         The objectives of this lab exercise are to: Demonstrate an understanding of the concept...
Objectives:         The objectives of this lab exercise are to: Demonstrate an understanding of the concept of operator precedence in Python expressions and statements. Take simple problem descriptions and produce small but complete Python programs that solve these problems. Give you some practice in using simple Python interactive input and output capabilities. Give you some practice in the simplest Python decision statement (if). Give you some practice in the simplest Python loop statement (while). Specifications: This is a four-part exercise....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT