-1

i'm typing my code for my assignment and i'm required to create 2 queues in the simulation file which are the customer queue and server queue which are DYNAMIC QUEUE as required in the question

i confused that whether i need to create a new queuetype in the simulation header file or i can just use the same header file but i worry that the value between 2 queues will crashed (something like this) if i use the same header file for 2 queue

Question: Can I use the same queue ADT file for 2 different queues? this is my code for the queue ADT header file

#ifndef H_queueAsLinkedList
#define H_queueAsLinkedList

#include <iostream>
#include <cassert>

#include "queueADT_2.h"

using namespace std;

//Definition of the node
template <class T>
struct nodeType
{
     T info;
     nodeType<T> *link;
};

template <class T>
class queueAsLinkedList: public queueADT<T>
{
public:
     const queueAsLinkedList<T>& operator=(const queueAsLinkedList<T>&);
     //Overload the assignment operator.
     
     bool isEmpty() const;
     //Function to determine whether the queue is empty.
     //Postcondition: Returns true if the queue is empty,
     //               otherwise returns false.
     
     bool isFull() const;
     //Function to determine whether the queue is full.
     //Postcondition: Returns true if the queue is full,
     //               otherwise returns false.
    
     void initializeQueue();
     //Function to initialize the queue to an empty state.
     //Postcondition: queueFront = nullptr; queueRear = nullptr
     
     T qFront() const;
     //Function to return the first element of the queue.
     //Precondition: The queue exists and is not empty.
     //Postcondition: If the queue is empty, the program
     //               terminates; otherwise, the first
     //               element of the queue is returned.
     
     T qRear() const;
     //Function to return the last element of the queue.
     //Precondition: The queue exists and is not empty.
     //Postcondition: If the queue is empty, the program
     //               terminates; otherwise, the last
     //               element of the queue is returned.
     
     void enQueue(const T& queueElement);
     //Function to add queueElement to the queue.
     //Precondition: The queue exists and is not full.
     //Postcondition: The queue is changed and queueElement
     //               is added to the queue.
     
     void deQueue();
     //Function to remove the first element of the queue.
     //Precondition: The queue exists and is not empty.
     //Postcondition: The queue is changed and the first
     //               yelement is removed from the queue.
     
     queueAsLinkedList();
     //Default constructor
     
     queueAsLinkedList(const queueAsLinkedList<T>& );
     //Copy constructor
     
     ~queueAsLinkedList();
     //Destructor
     
private:
     nodeType<T> *queueFront; //pointer to the front of the queue
     nodeType<T> *queueRear; //pointer to the rear of the queue

};

//********************implementation**************************

template <class T>
bool queueAsLinkedList<T>::isEmpty() const
{
    return (queueFront == nullptr);
} //end isEmptyQueue

template <class T>
bool queueAsLinkedList<T>::isFull() const
{
    return false;
} //end isFullQueue

template <class T>
void queueAsLinkedList<T>::initializeQueue()
{
     nodeType<T> *temp;
     while (queueFront!= nullptr) //while there are elements
     //left in the queue
     {
         temp = queueFront; //set temp to point to the current node
         queueFront = queueFront->link; //advance first to the next node
         delete temp; //deallocate memory occupied by temp
     }
     queueRear = nullptr; //set rear to nullptr
} //end initializeQueue

template <class T>
void queueAsLinkedList<T>::enQueue(const T& newElement)
{
     nodeType<T> *newNode;
     newNode = new nodeType<T>; //create the node
     newNode->info = newElement; //store the info
     newNode->link = nullptr; //initialize the link
     //field to nullptr
     if (queueFront == nullptr) //if initially the queue is empty
         {
             queueFront = newNode;
             queueRear = newNode;
         }
     else //add newNode at the end
         {
             queueRear->link = newNode;
             queueRear = queueRear->link;
         }
}//end enQueue

template <class T>
void queueAsLinkedList<T>::deQueue()
{
     nodeType<T> *temp;
     if (!isEmpty())
     {
        temp = queueFront; //make temp point to the first node
        queueFront = queueFront->link; //advance queueFront
        delete temp; //delete the first node
        if (queueFront == nullptr) //if after deletion the queue is empty
            queueRear = nullptr; //set queueRear to nullptr
     }
     else
        cout << "Cannot remove from an empty queue" << endl;
}//end deQueue

template <class T>
T queueAsLinkedList<T>::qFront() const
{
     assert(queueFront != nullptr);
     return queueFront->info;
} //end front

template <class T>
T queueAsLinkedList<T>::qRear() const
{
     assert(queueRear!= nullptr);
     return queueRear->info;
} //end back

template <class T>
queueAsLinkedList<T>::queueAsLinkedList()
{
     queueFront = nullptr; //set front to nullptr
     queueRear = nullptr; //set rear to nullptr
} //end default constructor

template <class T>
queueAsLinkedList<T>::~queueAsLinkedList()
{
    initializeQueue();
}// end Destructor

template <class T>
const queueAsLinkedList<T>& queueAsLinkedList<T>::operator=
                       (const queueAsLinkedList<T>& otherQueue)
{
    cout << "Write the definition of the function "
         << "to overload the assignment operator." << endl;
} //end assignment operator

template <class T>
queueAsLinkedList<T>::queueAsLinkedList(const queueAsLinkedList<T>& otherQueue)
{
    cout << "Write the definition of the copy constructor."
         << endl;
} //end copy constructor
#endif

sorry for my bad English, i can explain again if my description is confusing

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Fourwoods
  • 3
  • 2
  • The header just defines the type – not how many instances you can create of (though the node class is an implementation detail and thus should be a nested class of the list, but that's another matter). So yes, you can – just declare your lists as you would do with ordinary lists. – Aconcagua Feb 04 '22 at 13:20
  • Side note: There are special cases where this doesn't apply, e.g. a class implementing the singleton pattern – but that's again another matter... – Aconcagua Feb 04 '22 at 13:22
  • i see... thank you so much. it helps me a lot! – Fourwoods Feb 04 '22 at 13:32
  • Whoever provided you this header file *absolutely* should read about [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – especially in a header file! It's one's own matter – partially at least, consider those having to maintain them later on – if one want's to spoil the global namespace in one's own *sources*, but with *headers* one does so for *everyone* using them! – Aconcagua Feb 04 '22 at 13:35

2 Answers2

0

queueAsLinkedList does not have a static member.

The only thing that instances with a same class can share is static member.

Member variables are created each of Instance of class.

So you do not have to worry about crashes.

Mona04_
  • 56
  • 5
0

This declaration of a queue

template <class T>
class queueAsLinkedList: public queueADT<T>

does not make a sense.

The structure nodeType

struct nodeType
{
     T info;
     nodeType<T> *link;
};

should be a private or protected data member of the class template queueAsLinkedList.

For example

template <class T>
class queueAsLinkedList
{
private:
    struct nodeType
    {
        T info;
        nodeType *link;
    } *queueFront = nullptr, *queueRear = nullptr;
//...

You can use the same queue declaration to create two or more different objects of the queue type.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • actually there is a another file call queueADT.h header file that i don't included at here.. but this helps me a lot!! thank you so much!! – Fourwoods Feb 04 '22 at 13:30
  • Why shouldn't that declaration *not* make sense??? `queueAsLinkedList` publicly inherits from `queueADT` which itself is a template as well and instantiated with `T`. – Aconcagua Feb 04 '22 at 13:31
  • @Aconcagua Because the queue is not the same as nodeType. What interface is inherited by the queue from nodeType?! – Vlad from Moscow Feb 04 '22 at 13:33
  • @Aconcagua He is a beginner that uses the knowledge at current moment. So there is nothing bad for a beginner to use the using directive. All books on C++ for beginners use this directive. – Vlad from Moscow Feb 04 '22 at 13:37
  • The node type has nothing to do with the queue – the queue uses it internally, that's it. *But* I fully agree on that the former, as actually an implementation detail, should be a nested class of the latter... – Aconcagua Feb 04 '22 at 13:38
  • i see... i will try to look at it because i'm still new for this chapter. what i did is just follow the textbook (cry X( ), but your suggestion is helpful for me! – Fourwoods Feb 04 '22 at 13:38