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.          ...
- 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;       ...
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()...
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete....
c++ data structures linked list delete node bool deleteNode(int); pass this method an id to delete. Return true or false to indicate success or failure. Delete the memory the node was using The algorithm for deleting a Node to a Linked List (general case): ● Begin at the head. ● Compare the id to the current node. ● If search id > current id, stop. ● Detach the current Node ○ current->previous->next = current->next ○ current->next->previous = current->previous ● Deallocate...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...