Question

In: Computer Science

Implement a generic MyStack class using templates. A stack allows elements to be added and removed...

Implement a generic MyStack class using templates. A stack allows elements to be added and removed in a last-in, first-out (LIFO) manner. Stacks have an operation called push to place elements at the top of the stack, and another operation called pop to remove and return the element at the top of the stack. The only element on the stack that may be referenced is the one on the top. This means that if two elements are pushed onto the stack, the top most element must be removed from the stack before the first pushed element can be referenced. Your MyStack class must implement the following member functions (see below) in a file named MyStack.hpp.

(a) A member function push that takes the element to be pushed onto the stack and returns nothing.

(b) A member function pop that takes no arguments and returns the top element.

(c) A member function top that takes no arguments and returns the top element without removing it from the stack.

(d) A member function empty that takes no arguments and returns true if the stack is empty and false otherwise.

(e) A member function size that takes no arguments and returns the number of elements on the stack.

You are free to choose between a singly linked list (SLinkedList) and a vector as the internal data structure of your MyStack class. You can use the following code as a test driver for your implementation.

#include

#include

#include"MyStack.hpp"

using namespace s t d ;

int main ( ) { MyStack i n t s t a c k ;

MyStack s t r s t a c k ;

i n t s t a c k . push ( 2 ) ;

i n t s t a c k . push ( 7 ) ;

i n t s t a c k . push ( 1 ) ;

i n t s t a c k . push ( 8 ) ;

i n t s t a c k . push ( 2 ) ;

s t r s t a c k . push ( " Hermione_Granger " ) ;

s t r s t a c k . push ( " Ron_Weasley " ) ;

s t r s t a c k . push ( " Fleur_Delacour " ) ;

s t r s t a c k . push ( " Draco_Malfoy " ) ;

s t r s t a c k . push ( " Cedric_Diggory " ) ;

c ou t << "stack size: " << s t r s t a c k . s i z e ( ) << e n dl ;

c ou t << "popped: " << s t r s t a c k . pop ( ) << e n dl ;

c ou t << "peek: " << s t r s t a c k . top ( ) << e n dl ;

c ou t << "popped: " << s t r s t a c k . pop ( ) << e n dl ;

c ou t << "stack size: " << s t r s t a c k . s i z e ( ) << e n dl ;

c ou t << "stack empty: " << ( i n t s t a c k . empty ( ) ? "Y" : "N" ) << e n dl ;

while ( ! i n t s t a c k . empty ( ) ) {

c ou t << i n t s t a c k . pop ( ) << " " ;

} c ou t << e n dl ;

c ou t << "stack empty: " << ( i n t s t a c k . empty ( ) ? "Y" : "N" ) << e n dl ;

return 0 ;

}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

//MyStack.hpp file

#ifndef MyStack_H

#define MyStack_H

#include<vector>

template <typename T>

class MyStack{

                private:

                //using a vector as underlying data structure

                std::vector<T> stk;

                public:

                //adds an element to the top (back)

                void push(T element);

                //removes and returns the top element

                T pop();

                //returns the top element, without removing

                T top() const;

                //returns true if stack is empty

                bool empty() const;

                //returns the current number of elements

                int size() const;

};

//implementation of all methods

template <typename T>

void MyStack<T>::push(T element){

                //simply adding to the back

                stk.push_back(element);

}

template <typename T>

T MyStack<T>::pop(){

                //getting last element

                T element=stk[stk.size()-1];

                //removing it

                stk.pop_back();

                //returning it

                return element;

}

template <typename T>

T MyStack<T>::top() const{

                //getting and returning last element

                T element=stk[stk.size()-1];

                return element;

}

template <typename T>

bool MyStack<T>::empty() const{

                return stk.empty();

}

template <typename T>

int MyStack<T>::size() const{

                return stk.size();

}

#endif

/*OUTPUT using your test program*/

stack size: 5

popped: Cedric_Diggory

peek: Draco_Malfoy

popped: Draco_Malfoy

stack size: 3

stack empty: N

2 8 1 7 2

stack empty: Y


Related Solutions

Implement the stack ADT in a fully generic manner (through the use of templates) by means...
Implement the stack ADT in a fully generic manner (through the use of templates) by means of a singly linked list. (Give your implementation “from scratch,” without the use of any classes from the Standard Template Library ).
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT