Question
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,
as a convenient storage container. By building a stack with a vector, the user need not worry about resizing the stack. A sta
// constant version of top() with same internal method code template <typename T> const T& miniStack<T>:: top() const // The
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE

#include <iostream>

#include <vector>

#include <stdexcept>

using namespace std;

template <typename T>

class miniStack

{

public:

//function prototypes

miniStack();

void push(const T& item);

void pop();

T& top();

const T& top() const;

bool empty() const;

int size() const;

private:

vector <T> stackVectorXX;

};

//push tem on stack by inserting it at

//rear opf vector

template <typename T>

void miniStack<T>::push(const T& item)

{

stackVectorXX.push_back(item);

}

//if(empty()) throw underflow_error("miniStack pop() : stack empty")

//pop stack by removing last itemn in vector

template <typename T>

void miniStack<T>::pop()

{

if(empty()) throw underflow_error("miniStack pop() : stack empty");

stackVectorXX.pop_back();

}

//if(empty()) throw underflow_error("miniStack pop() : stack empty")

//return the element at the rar of vector

template <typename T>

T& miniStack<T>::top()

{

if(empty()) throw underflow_error("miniStack top() : stack empty");

return stackVectorXX[stackVectorXX.size()-1];

}

//if(empty()) throw underflow_error("miniStack pop() : stack empty")

//return the element at the rar of vector

template <typename T>

const T& miniStack<T>::top() const

{

if(empty()) throw underflow_error("const miniStack top() : stack empty");

return stackVectorXX[stackVectorXX.size()-1];

}

template <typename T>

bool miniStack<T>::empty() const

{

return stackVectorXX.empty();

}

template <typename T>

int miniStack<T>::size() const

{

return stackVectorXX.size();

}

template <typename T>

miniStack<T>::miniStack()

{

}

int main()

{

miniStack<int> stk;

//testing all functions of miniStack

cout << "Stack push 1";

stk.push(1);

cout << "\nStack push 2";

stk.push(2);

cout << "\nStack pop";

stk.pop();

cout << "\nStack top " << stk.top();

cout << "\nStack size: " << stk.size();

cout << "\nStack is empty?" << (stk.empty() ? " Yes" : " No");

cout << "\nStack pop";

stk.pop();

cout << "\nStack is empty?" << (stk.empty() ? " Yes" : " No");

return 0;

}

OUTPUT

clang++-7 pthread -std=c++17 -o main main.cpp > ./main Stack push 1 Stack push 2 Stack pop Stack top 1 Stack size: 1 Stack is

TIME COMPLEXITY OF OPERATIONS

  • push(): constant amortized time, resizing of vector may happen, which is done in linear time; Best Case O(1) , Worst Case O(n)
  • pop(): constant time; O(1)
  • top(): constant time; O(1)
  • empty(): constant time; O(1)
  • size(): constant time; O(1)
  • pop(): constant time
Add a comment
Know the answer?
Add Answer to:
Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...
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
  • - 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 {...

  • template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() =...

    template <class T> class Stack { public: /** clear * Method to clear out or empty any items on stack, * put stack back to empty state. * Postcondition: Stack is empty. */ virtual void clear() = 0; /** isEmpty * Function to determine whether the stack is empty. Needed * because it is undefined to pop from empty stack. This * function will not change the state of the stack (const). * * @returns bool true if stack is...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • (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...

  • I need help fixing my code.   My output should be the following. Hello, world! : false...

    I need help fixing my code.   My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

  • // 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...

  • 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...

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