Question

Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...

Data Structures using C++

Consider the following class

#ifndef LINKEDQUEUETYPE_H

#define LINKEDQUEUETYPE_H

#include <iostream>

#include <new>   

#include <cstdlib>

#include "QueueADT.h"

using namespace std;

// Definition of the node

template <class ItemType>

struct NodeType

{

       ItemType info;

       NodeType<ItemType> *next;

};

template <class ItemType>

class LinkedQueueType: public QueueADT<ItemType>

{

public:

       // Constructor

       LinkedQueueType();

          // Default constructor.

          // Post: An empty queue has been created. queueFront = NULL;

          //       queueBack = NULL;

       LinkedQueueType(const LinkedQueueType<ItemType>& otherQueue);

          // Copy constructor for deep copy of the linked implementation

          // of queues.

       // Destructor

       ~LinkedQueueType();

          // Delete all the queue items.

          // Post: All the items of the queue are removed.

       // Action responsibilities

       void resetQueue();

          // Reset the queue to an empty queue.

          // Post: Queue is empty. queueFront = NULL; queueBack = NULL;

       void add(const ItemType& newItem);

          // Function to add newItem to the queue.

          // Pre: The queue exists and is not full.

          // Post: The queue is changed and newItem is added to the

          //       back of the queue.

       void remove();

          // Function to remove the first element of the queue.

          // Pre: The queue exists and is not empty.

          // Post: The queue is changed and the first (i.e., front) item is

          //       removed from the queue.

       // Knowledge responsibilities

       bool isEmpty() const;

          // Post: Returns true if queue is empty; false otherwise.

       bool isFull() const;

          // Post: Returns true if there is room for another item;

          //       false otherwise.

       ItemType front() const;

          // Function to return the first element of the queue.

        // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the first element of the queue is returned.

       ItemType back() const;

          // Function to return the last element of the queue.

          // Pre: The queue exists and is not empty.

          // Post: If the queue is empty, the program terminates; otherwise,

          //       the last element of the queue is returned.

       // Operator overloading

       const LinkedQueueType<ItemType>& operator=

                     (const LinkedQueueType<ItemType>& otherQueue);

          // Overload the assignment operator.

protected:

       NodeType<ItemType> *queueFront; // pointer to the front of the queue

                                       // (pointing to first node)

       NodeType<ItemType> *queueBack; // pointer to the back of the queue

                                       // (pointing to last node)

private:

       void copyQueue(const LinkedQueueType<ItemType>& otherQueue);

          // Function to make a copy of otherQueue.

          // A deep copy of otherQueue is created and assigned to this queue.

};

//**************************************************************

template <class ItemType>

LinkedQueueType<ItemType>::LinkedQueueType()

{

       queueFront = NULL;

       queueBack = NULL;

}

//**************************************************************

template <class ItemType>

LinkedQueueType<ItemType>::LinkedQueueType

                (const LinkedQueueType<ItemType>& otherQueue)

{

       queueFront = NULL;

       queueBack = NULL;

       copyQueue(otherQueue);

}

//**************************************************************

template <class ItemType>

LinkedQueueType<ItemType>::~LinkedQueueType()

{

       resetQueue();

}

//**************************************************************

template <class ItemType>

void LinkedQueueType<ItemType>::resetQueue()

{

       NodeType<ItemType> *temp; // pointer to delete the node

      

       while ( queueFront != NULL ) // while there are items in queue

       {

              temp = queueFront; // set temp to point to the current node

              queueFront = queueFront->next; // advance queueFront to the next

                                             // node

              delete temp; // deallocate memeory occupied by temp

       }

       queueBack = NULL;

}

//**************************************************************

template <class ItemType>

void LinkedQueueType<ItemType>::add(const ItemType& newItem)

{

       NodeType<ItemType> *newNode; // pointer to create the new node

       try

       {

              newNode = new NodeType<ItemType>; // create the node

              newNode->info = newItem; // store newItem in the node

              newNode->next = NULL;

              if (queueFront == NULL) // if initially the queue is empty

              {

                     queueFront = newNode;

                     queueBack = newNode;

              }

              else

              {

                     queueBack->next = newNode;

                     queueBack= queueBack->next;

              }

       }

       catch(bad_alloc)

       {

              cout << "ERROR: Cannot allocate memory!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

void LinkedQueueType<ItemType>::remove()

{

       NodeType<ItemType> *temp; // pointer to deallocate memory

       if ( !isEmpty() )

       {

              temp = queueFront; // set temp to point to the first node

              queueFront = queueFront->next; // advance queueFront to the

                                            // next node

              delete temp; // delete the first node

              if ( queueFront == NULL ) // if after deletion the queue is empty

                     queueBack = NULL; // set queueBack to NULL

       }

       else

              cout << "Cannot remove from an empty queue." << endl;

}

//**************************************************************

template <class ItemType>

bool LinkedQueueType<ItemType>::isEmpty() const

{

       return ( queueFront == NULL );

}

//**************************************************************

template <class ItemType>

bool LinkedQueueType<ItemType>::isFull() const

{

       NodeType<ItemType> *newNode; // a testing node to see if there

                                    // is more room

       try

       {

              newNode = new NodeType<ItemType>;

              delete newNode;

              return false; //more room available, not full

       }// end try

       catch (bad_alloc)

       {

              return true; // no more room available

       }

}

//**************************************************************

template <class ItemType>

ItemType LinkedQueueType<ItemType>::front() const

{

       if ( !isEmpty() )

              return queueFront->info; // return the first item

       else // if queue is empty, terminate the program

       {

              cout << "ERROR: Cannot return first item from an empty queue!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

ItemType LinkedQueueType<ItemType>::back() const

{

       if ( !isEmpty() )

              return queueBack->info; // return the last item

       else // if queue is empty, terminate the program

       {

              cout << "ERROR: Cannot return last item from an empty queue!\n";

              exit(EXIT_FAILURE);

       }

}

//**************************************************************

template <class ItemType>

const LinkedQueueType<ItemType>& LinkedQueueType<ItemType>::operator=

                    (const LinkedQueueType<ItemType>& otherQueue)

{

       if ( this != &otherQueue ) // avoid self-copying

              copyQueue(otherQueue);

       return *this;

}

//**************************************************************

template <class ItemType>

void LinkedQueueType<ItemType>::copyQueue

                     (const LinkedQueueType<ItemType>& otherQueue)

{

       NodeType<ItemType> *newNode, *current;

       if ( queueFront != NULL ) // if queue is nonempty, make it empty

              resetQueue();

       if ( otherQueue.queueFront != NULL ) // if otherQueue is nonempty

       {

              try

              {

                     current = otherQueue.queueFront; // set current to point to

                                                      // the queue to be copied

                     // copy the queueFront item of the queue

                     queueFront = new NodeType<ItemType>; // create the node

                     queueFront->info = current->info; // copy the info

                     queueFront->next = NULL; // set next to NULL

                     queueBack = queueFront; // set queueBack to point to

                                             // the node

                     current = current->next; // set current to point

                                             // to the next node

                     // copy the remaining of the queue

                     while ( current != NULL)

                     {

                           newNode = new NodeType<ItemType>;

                           newNode->info = current->info;

                           newNode->next = NULL;

                           queueBack->next = newNode;

                           queueBack = newNode;

                           current = current->next;

                     }//end while

              }// end try

              catch (bad_alloc)

              {

                     cout << "ERROR: Cannot allocate memory!\n";

                     exit(EXIT_FAILURE);

              }

       }//end else

}

#endif

What is the effect of the following statements? If a statement is invalid, explain why it is invalid.

(a) LinkedQueueType<int> numQueue;

(b) LinkedQueueType<int> numQueue2(numQueue);

Homework Answers

Answer #1

LinkedQueueType class has two constructor:

   // Constructor
LinkedQueueType();
    // Default constructor.
// Post: An empty queue has been created. queueFront = NULL;


LinkedQueueType(const LinkedQueueType<ItemType>& otherQueue);
// Copy constructor for deep copy of the linked implementation
// of queues.


(a) LinkedQueueType<int> numQueue;

   THis sttement create an Object of class LinkedQueueType using default constructor.
   Since LinkedQueueType class has default constructor so this is valid statement


(b) LinkedQueueType<int> numQueue2(numQueue);

THis sttement create an Object of class LinkedQueueType using copy constructor.
   Since LinkedQueueType class has copy constructor so this is valid statement.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
Used the next seudoucode pseudocode and implement in the next code below run in c ++...
Used the next seudoucode pseudocode and implement in the next code below run in c ++ this code What is the output of the following pseudocode, where num1 , num2 , and num3 are integer variables? num1 = 5 num2 = 1 num3 = 4 aQueue.enqueue(num2) aQueue.enqueue(num3) aQueue.dequeue() aQueue.enqueue(num1 - num2) num1 = aQueue.peek() aQueue.dequeue() num2 = aQueue.peek() aQueue.dequeue() cout << num2 << " " << num1 << " " << num3 << endl ____________________________________________________________________________________ a// Created by Frank M....
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL;...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...