Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The stack and node classes are below. Stack.h: #ifndef NODEB_STACK_H #define NODEB_STACK_H #include Node.h #include using namespace std; template typename Object> class Stack { private

image text in transcribed

The stack and node classes are below.

Stack.h:

#ifndef NODEB_STACK_H #define NODEB_STACK_H #include "Node.h" #include  using namespace std; templatetypename Object> class Stack { private:  Node* top; public:  Stack() {  top = nullptr; }  ~Stack() {  while (!isEmpty()) { pop(); } } bool isEmpty() const { return (top == nullptr); } void push(Object item) { if (isEmpty()) {  Node* newNode = new Node(item);  top = newNode; } else {  Node* newNode = new Node(item, top);  top = newNode; } } Object pop() { if (isEmpty()) {  return Object(); } else {  Object copy = top->getItem();  Node* topCopy = top;  top = top->getNext();  delete topCopy; // Return the Object  return copy; } } bool exists(Object item) const { Node* curr = top; while (curr != nullptr) {  if (curr->getItem() == item) {  return true; } curr = curr->getNext(); }  return false; } void printStack() const { Node* curr = top; while (curr != nullptr) {  cout getItem() getNext(); } } };  

Node.h:

templatetypename Object> class Node { private: Object item; Node* next; public: Node(Object newItem) { item = newItem; next = nullptr; } Node(Object newItem, Node* nextNode) { item = newItem; next = nextNode; } void setItem(Object newItem) { item = newItem; } Object getItem() const { return item; } void setNext(Node* nextNode) { next = nextNode; } Node* getNext() const { return next; } };
Stack Class Start with the Stack class from lecture. What is the Big-Oh complexity of the methods? Queue Class Create a Queue class that uses the Node class from lecture to create a functioning queue data structure. . Your Queue should be able to push and pop Objects and determine if an Object is in the Queue. What is the complexity of each method? . Your Queue must be able to be used with any data type. Your Nodes must be stored in heap memory. . Your program must not have any memory leaks. Stack Class Start with the Stack class from lecture. What is the Big-Oh complexity of the methods? Queue Class Create a Queue class that uses the Node class from lecture to create a functioning queue data structure. . Your Queue should be able to push and pop Objects and determine if an Object is in the Queue. What is the complexity of each method? . Your Queue must be able to be used with any data type. Your Nodes must be stored in heap memory. . Your program must not have any memory leaks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions