Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below are some simple C++ questions, with all necessary code. 1.) What is the order of the objects before and after adding them to the

Below are some simple C++ questions, with all necessary code.

1.) What is the order of the objects before and after adding them to the Queue and Stack? When and why does it change?

2.) Using the Node class, will the links point from the front to the back of the Queue or from the back to the front?

3.) Which Nodes in the Queue do you need to keep track of? Is there still a top Node pointer?

4.) How can you be sure there are no memory leaks?

Node.h:

#ifndef NODEB_NODE_H #define NODEB_NODE_H  /** Represents one node to be used in a linked list  Contains data (object) and reference to next in list */  template<typename 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; } }; #endif //NODEB_NODE_H 

Queue.h:

#ifndef NODEB_QUEUE_H #define NODEB_QUEUE_H  /*  * Queue class  * Functionality: push, pop, peek, isEmpty, print, exists  */  #include "Node.h" #include  using namespace std; template<typename Object> class Queue { private: // Store the address of the top Node in the Queue  Node* top; Node* down; public: // Constructor  Queue() { // Queue is empty. Top doesn't point at anything.  top = nullptr; down = nullptr; } // Deconstructor / Destructor  ~Queue() { // Pop all the Nodes left in the Queue  while (!isEmpty()) { pop(); } } bool isEmpty() const { return (top== nullptr); } void push(Object item) { if (isEmpty()) { // This is the first Node in the Queue  // Allocate memory in the heap to store the new Node  Node* newNode = new Node(item); // Update top  top = newNode; down=top; } else { // There is already at least one Node in the Queue  Node* newNode = new Node(item); // Update down  down->setNext(newNode); down = 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 Queue.  // 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 printQueue() const { Node* curr = top; while (curr != nullptr) { // Note: This assumes that the Object can use the << operator  cout << curr->getItem() << endl; curr = curr->getNext(); } } };

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

Spatial Database Systems Design Implementation And Project Management

Authors: Albert K.W. Yeung, G. Brent Hall

1st Edition

1402053932, 978-1402053931

More Books

Students also viewed these Databases questions

Question

Recognize the four core purposes service environments fulfill.

Answered: 1 week ago

Question

Know the three main dimensions of the service environment.

Answered: 1 week ago