Question

In: Computer Science

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

Solutions

Expert Solution

Answer:-

template <class T>

class Stack {

private:

struct Node {

T data;

Node* next;

};

Node* top;

public:

Stack() : top(nullptr){}

Stack(Stack const& value);

Stack<T>(Stack<T>&& move) noexcept;

Stack<T>& operator=(Stack&& move) noexcept;

~Stack();

Stack& operator=(Stack const& rhs);

friend std::ostream& operator<<(std::ostream& str, Stack<T> const& data) {

data.show(str);

return str;

}

bool isEmpty();

int getSize();

void push(const T& theData);

void pop();

void show(std::ostream &str) const;

};

template <class T>

Stack<T>::Stack(Stack const& value) : top(nullptr) {

for(Node* loop = value->data; loop != nullptr; loop = loop->next) {

push(loop->data);

}

}

template <class T>

Stack<T>::Stack(Stack<T>&& move) noexcept : top(nullptr) {

move.swap(*this);

}

template <class T>

Stack<T>& Stack<T>::operator=(Stack<T> &&move) noexcept {

move.swap(*this);

return *this;

}

template <class T>

Stack<T>::~Stack() {

while(top != nullptr) {

pop();

}

}

template <class T>

Stack<T>& Stack<T>::operator=(Stack const& rhs) {

Stack copy(rhs);

swap(copy);

return *this;

}

template <class T>

bool Stack<T>::isEmpty() {

if(top == nullptr) {

return true;

}

else {

return false;

}

}

template <class T>

int Stack<T>::getSize() {

int size = 0;

Node* current = top;

while(current != nullptr) {

size++;

current = current->next;

}

return size;

}

template <class T>

void Stack<T>::push(const T &theData) {

Node* newNode = new Node;

newNode->data = theData;

newNode->next = nullptr;

if(top != nullptr) {

newNode->next = top;

}

top = newNode;

}

template <class T>

void Stack<T>::pop() {

Node* temp;

if(top == nullptr) {

throw std::invalid_argument("Trying to pop from empty list....");

}

temp = top;

top = top->next;

delete temp;

}

template <class T>

void Stack<T>::show(std::ostream &str) const {

for(Node* loop = top; loop != nullptr; loop = loop->next) {

str << loop->data << "\t";

}

str << "\n";


Related Solutions

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...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of...
Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure. Related codes: public interface Stack<E> { int size( ); boolean isEmpty( ); void push(E e); E top( ); E pop( ); } public class LinkedStack<E> implements Stack<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>( );    public LinkedStack( ) { }    public int size( ) { return...
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...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called...
Please complete absolutely follow the requirements. Thanks! Implement a stack ADT by writing a class called Stack. Use a static array to hold stack elements. Instantiate the Stack class in the main function and provide a user loop and a menu so that all the Stack class member-functions, push, pop, etc., are available so that the user can thoroughly exercise the member-functions of the Stack class. Also, implement a ReversePrint() for the stack. My StackProject, whose exposition I have given...
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...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in...
This is C++ programming. Use separate compilation to implement a polynomial ADT that manipulates polynomials in a single variable x (e.g., p = 4 x^5 + 7 x^3 – x^2 + 9 ). For this problem, consider only polynomials whose exponents are non-negative integers. You are required to identify a proper data representation schema to store such polynomials and hide such data from external users of this ADT. Additionally, your ADT will at least include the following member functions: One...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
The rapidly changing world means there is no perfect form of management." Through the use of...
The rapidly changing world means there is no perfect form of management." Through the use of relevant theory and real-life company examples, explain your argument on the sentence provided.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT