Question

Stack.h

#ifndef STACK H #define STACK H /1 your name // Stack.h // date // description #include «vector» using namespace std; template <typename T> class Stack vector<T> container; public: Stack (): container) void push (T x) container.push back(x); void pop) container.pop back); T top) t return container.back); bool empty() t return container.emptyO; b: #endif

Stack_test.cpp

What would happen if we add

s2.pop();

or

cout << s2.top();

to the end of the program (when s2 is empty?)

Fix Stack.h so that these problems would not occur.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If we add

s2.pop()

or

cout<<s2.top();

Then the program would crash as there would be a run time error. It is because the stack is already empty and we are trying to remove an element from an empty stack. So, it will cause a runtime error resulting into a crash of the program.

We can fix this by adding a check condition in the pop() and top() funciton.

So, the correct code is

------------------------Stack.h----------------------

#ifndef STACK_H

#define STACK_H

#include<iostream>

#include<vector>

using namespace std;

template<typename T>

class Stack

{

    vector<T> container;

   

    public:

       

        Stack(): container() {}

       

        void push(T x)

        {

            this->container.push_back(x);

        }

       

        void pop()

        {

            // if the container is not empty

            if( !this->empty() )

                this->container.pop_back();

        }

       

        T top()

        {

            // if the container is not empty

            if( !this->empty() )

                return this->container.back();

            // if the container is empty

            else

                return NULL;

        }

       

        bool empty()

       {

            return this->container.empty();

        }

};

#endif


----------------------main.cpp-------------------

#include<iostream>

#include<string>

#include "Stack.h"

int main()

{

    Stack<int> s1;

   

    s1.push(4);

    s1.push(3);

    s1.push(2);

    s1.push(1);

   

    while( !s1.empty() )

    {

        cout<<s1.top()<<endl;

        s1.pop();

    }

   

    Stack<string> s2;

   

    s2.push("Yoda said");

    s2.push("something");

    s2.push("to write");

   

    while( !s2.empty() )

    {

        cout<<s2.top();

        s2.pop();

    }

  

    cout<<endl;

   

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Stack.h Stack_test.cpp What would happen if we add s2.pop(); or cout << s2.top(); to the end...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please Answer this question using the language of C++. I provide you with the picture of figure 1...

    Please Answer this question using the language of C++. I provide you with the picture of figure 18_02. Thank you. I 7/ Fig. 18.2: Stack.h 2 // Stack class template. #ifndef #de fine 3 STACK-H STACK-H 5 #include 7 template 8 class Stack ( 9 public: 10 I const T& top) 12 13 l/ return the top element of the Stack return stack.frontO; // push an element onto the Stack void push(const T& pushValue) 15 stack push front (pushValue); 17...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • Help with c++ program. The following code takes an infix expression and converts it to postfix....

    Help with c++ program. The following code takes an infix expression and converts it to postfix. When I compile the code it returns "Segmentation fault". However I don't know what causes this. #include #include #include using namespace std; template class Stack { public: Stack();//creates the stack bool isempty(); // returns true if the stack is empty T gettop();//returns the front of the list void push(T entry);//add entry to the top of the stack void pop();//remove the top of the stack...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT