Queues: A FIFO Data Structure with Linked List and Circular Array Implementations, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/18/2009

koofers-user-fp9
koofers-user-fp9 🇺🇸

8 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 7
QUEUES
Defn: A queue is a data structure in which the item first in is
the item first out (FIFO).
Examples:
check-out line at a grocery store
drive-thru window at Taco Bell
printer queue
We want to design an ADT for Queues.
The data structure must provide the following attributes:
length
front
back
contents
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Queues: A FIFO Data Structure with Linked List and Circular Array Implementations and more Study notes Computer Science in PDF only on Docsity!

CHAPTER 7

QUEUES

Defn: A queue is a data structure in which the item first in is

the item first out (FIFO).

Examples:

 check-out line at a grocery store

 drive-thru window at Taco Bell

 printer queue

We want to design an ADT for Queues.

The data structure must provide the following attributes:

 length

 front

 back

 contents

The following operations must be provided in the ADT:

 enqueue : add to the back of the queue

 dequeue : remove from the front of the queue

 getFront : get information stored in the first item of the

queue

 isEmpty : check to see if the queue is empty

 Initialize : create an empty queue

We will look at two possible implementations of Queue ADTs:

 Simple linked list

 Circular array

Specification for the Linked Queue

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; };

C++ Code:

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; } }

Circular Array Implementation of Queues

Problem: With simple arrays, the contents of the queue must

be shifted to the front upon each deletion.

Solution: Don’t slide things to the front, use a circular array

that allows the front to move.

Now we need to know where the front is located in the array:

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 };

C++ Code:

 The next open spot is (back +1) mod MAX_QUEUE

 Changing the index range to 0..(MAX_QUEUE - 1) helps with the mod

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;

Comparison of ADT Implementation for Queues

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 #include "ListException.h" #include "ListIndexOutOfRangeException.h" template class List; template class ListNode { friend class List; private: ListNode() : next(NULL) {}; ListNode(const T& nodeItem, ListNode* ptr) : item(nodeItem), next(ptr) {}; T item; ListNode* next; }; template class List { public: // constructors and destructor List(); // default constructor List(const List& aList); // copy constructor ~List(); // deletes all nodes in list // list operations List& operator=(const List& aList); bool isEmpty() const; int getLength() const; void insert(int index, T newItem) throw(ListIndexOutOfRangeException, ListException); void remove(int index) throw(ListIndexOutOfRangeException); void retrieve(int index, T& dataItem) const throw(ListIndexOutOfRangeException); private: int size; // number of items in list ListNode* head; // pointer to linked list of items ListNode* find(int index) const; // returns a pointer to the // position-th node in the // linked list };

Using the Generic (Template) List ADT int main() { List floatList; List charList; floatList.insert(1, 1.1); floatList.insert(2, 2.2); charList.insert(1, 'a'); charList.insert(2, 'b'); // … }