Stack.h: #ifndef NODEB_STACK_H #define NODEB_STACK_H /* * Stack class * Each Node point to the Node below it * Functionality: push, pop, peek, isEmpty, print
Stack.h:
#ifndef NODEB_STACK_H #define NODEB_STACK_H /* * Stack class * Each Node point to the Node below it * Functionality: push, pop, peek, isEmpty, print */ #include "Node.h" #include using namespace std; templatetypename Object> class Stack { private: // Store the address of the top Node in the Stack Node
Node.h:
#ifndef NODEB_STACK_H #define NODEB_STACK_H /* * Stack class * Each Node point to the Node below it * Functionality: push, pop, peek, isEmpty, print */ #include "Node.h" #include using namespace std; templatetypename Object> class Stack { private: // Store the address of the top Node in the Stack Node* top; public: // Constructor Stack() { // Stack is empty. Top doesn't point at anything. top = nullptr; } // Deconstructor / Destructor ~Stack() { // Pop all the Nodes left in the Stack while (!isEmpty()) { pop(); } } bool isEmpty() const { return (top == nullptr); } void push(Object item) { if (isEmpty()) { // This is the first Node in the Stack // Allocate memory in the heap to store the new Node Node* newNode = new Node(item); // Update top top = newNode; } else { // There is already at least one Node in the Stack Node* newNode = new Node(item, top); // Update top top = newNode; } } Object pop() { if (isEmpty()) { // There is no Node to pop. Return the default Object. // Note: This assumes the Object has a default constructor. return Object(); } else { // There is at least one Node in the Stack. // Store a copy of the Object to be returned Object copy = top->getItem(); // Store a copy of top Node* topCopy = top; // Update top to point at whatever the Node we are about to delete is pointing at top = top->getNext(); // Delete the Node from heap memory // Use the delete keyword with a pointer to the memory you want to deallocate from the heap delete topCopy; // Return the Object return copy; } } bool exists(Object item) const { Node* curr = top; while (curr != nullptr) { // Note: This assumes the Object can use the == operator if (curr->getItem() == item) { // Found the item in the Stack return true; } curr = curr->getNext(); } // Did not find the item in the Stack return false; } void printStack() const { Node* curr = top; while (curr != nullptr) { // Note: This assumes that the Object can use the cout getItem() getNext(); } } }; #endif //NODEB_STACK_H
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance