







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of queues as a first-in-first-out (fifo) data structure and presents two possible implementations: a simple linked list and a circular array. The design of an abstract data type (adt) for queues, including its attributes and operations, as well as the code implementation for each method in c++. The comparison of the two implementations in terms of programming difficulty, memory requirements, execution time, and other factors is also provided.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








typedef desired-type-of-queue-item QueueItemType; class Queue { public: Queue(); Queue(const Queue& Q); ~Queue(); bool isEmpty() const; void enqueue(QueueItemType NewItem) throw(QueueException); void dequeue() throw(QueueException); void dequeue(QueueItemType& queueFront) throw(QueueException); void getFront(QueueItemType& queueFront) const throw(QueueException); private: struct QueueNode { QueueItemType item; QueueNode* next; }; QueueNode* frontPtr; QueueNode* backPtr; };
void Queue::enqueue(QueueItemType newItem) throw(QueueException) { QueueNode* newPtr = new QueueNode; if (newPtr == NULL) throw QueueException( "QueueException: enqueue cannot allocate memory"); else { newPtr->item = NewItem; if (isEmpty()) frontPtr = newPtr; else backPtr->next = newPtr; backPtr = newPtr; // new node is at back } } void Queue::getFront(QueueItemType& queueFront) const throw(QueueException) { if (isEmpty()) throw QueueException( "QueueException: empty queue, cannot get front") else QueueFront = frontPtr->item; } } void Queue::dequeue() throw(QueueException) { if (isEmpty()) throw QueueException( "QueueException: empty queue, cannot dequeue"); else { QueueNode* tempPtr = frontPtr; if (frontPtr == backPtr) // one node in queue frontPtr = NULL; backPtr = NULL: else frontPtr = frontPtr->next; tempPtr->next = NULL; // safeguard delete tempPtr; } }
const int MAX_QUEUE = maximum-size-of-queue ; typedef desired-type-of-queue-item QueueItemType; class Queue { public: Queue(); Queue(const Queue&); bool isEmpty() const; void enqueue(QueueItemType newItem) throw(QueueException); void dequeue() throw(QueueException); void dequeue(QueueItemType& queueFront) throw(QueueException); void getFront(QueueItemType& queueFront) const throw(QueueException); private: QueueItemType items[MAX_QUEUE]; int front; //Init to 0 int back; //Init to MAX_QUEUE - 1 int count; //Init to 0 };
void Queue::enqueue(QueueItemType newItem) throw(QueueException) { if (count == MAX_QUEUE) throw QueueException( "QueueException: queue full on enqueue"); else { back = (back + 1) % MAX_QUEUE; items[back] = newItem; ++count; } } void Queue::getFront(QueueItemType& queueFront) const throw(QueueException) { if (isEmpty()) throw QueueException( "QueueException: empty queue, cannot get front"); else { queueFront = items[front]; } void Queue::dequeue() throw(QueueException) { if (isEmpty()) throw QueueException( "QueueException: empty queue, cannot dequeue"); else { front = (front + 1) % MAX_QUEUE; --count; } } void Queue::dequeue(QueueItemType& queueFront) throw(QueueException) { if (isEmpty()) throw QueueException( "QueueException: empty queue, cannot dequeue"); else { queueFront = items[front]; front = (front + 1) % MAX_QUEUE; --count;
Programming Difficulty Memory Requirements Execution Time Other Simple Linked List w/ Front and Back Pointers Circular Array
Making the Linked List ADT Generic using a Template (see pages 409-412 in the Textbook) #include
Using the Generic (Template) List ADT int main() { List