0% found this document useful (0 votes)
47 views

CS221 - Data Structures & Algorithms: Queues

This document discusses queues and their implementation using both linked lists and arrays. It defines a queue as a first-in, first-out (FIFO) data structure where elements are inserted at the rear and deleted from the front. Key queue operations like enqueue and dequeue are presented along with pseudocode to implement them using linked nodes with front and rear pointers or a static-size array with front and rear indexes.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

CS221 - Data Structures & Algorithms: Queues

This document discusses queues and their implementation using both linked lists and arrays. It defines a queue as a first-in, first-out (FIFO) data structure where elements are inserted at the rear and deleted from the front. Key queue operations like enqueue and dequeue are presented along with pseudocode to implement them using linked nodes with front and rear pointers or a static-size array with front and rear indexes.

Uploaded by

Nouman Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CS221 – Data Structures &

Algorithms
Queues
Queue
• An Ordered list in which insertions occur at
one end (rear) and deletions occur at the
other end (front).
• Its operation preserves the entry order of the
elements, a queue is a first-in, first-out (FIFO)
list.
Queue
• In a FIFO data structure, the first element
added to the queue will be the first one to be
removed.
• Whenever an element is added, all elements
that were added before have to be removed
before the new element can be invoked.
Queue Operations
• Enqueue
– Inserts an element at the end of the list, called the
rear.
• Dequeue
– Deletes and returns the element at the start of
list, called the front.
Queue Implementation
• struct Node
• {
– ElementType Element;
– struct Node *Next;
• }
Queue Implementation
• typedef struct Node* PointerToNode;
• typedef PointerToNode Front; //F
• typedef PointerToNode Rear; //R
Queue Implementation
• Enqueue
Queue Implementation
• Enqueue
Node* MyNewNode = CreateNewNode(sizeof(Node))
MyNewNodeElement = A
MyNewNodeNext = RNext
Queue Implementation
• Enqueue
RNext = MyNewNode
Queue Implementation
• Enqueue
R = MyNewNode
Queue Implementation
• Enqueue
Node* MyNewNode = CreateNewNode(sizeof(Node))
MyNewNodeElement = B
MyNewNodeNext = RNext
Queue Implementation
• Enqueue
RNext = MyNewNode
Queue Implementation
• Enqueue
R = MyNewNode
Queue Implementation
• EnqueueAlgo
• {
– Node* MyNewNode = CreateNewNode(sizeof(Node))
– MyNewNodeElement = X
– MyNewNodeNext = RearNext
– RearNext = MyNewNode
– Rear = MyNewNode
• }
Queue Implementation
• void Enqueue(ElementType x, Rear R)
• {
– Node* MyNewNode = malloc(sizeof(struct
Node));
– MyNewNodeElement = x;
– MyNewNodeNext = RNext;
– RNext = MyNewNode;
– R = MyNewNode;
• }
Queue Implementation
• Dequeue Algo
• {
– Node* Temp = Front  Next
– Front  Next = Temp Next
– Return Temp
• }
Queue Implementation
• struct Node* Dequeue(Front F)
• {
– Node* TempNode;
– TempNode = F  Next;
– F  Next = TempNode  Next;
– return TempNode;
• }
Array Implementation of Queue
• We maintain an array Queue[] & the positions
Front and Rear.
• We keep the track of the number of elements in
the queue with Size.
• To enqueue an element x in the queue, increment
the Size and the rear, then set Queue[Rear] = x.
• To dequeue an element , set the return value to
Queue[Front], decrement the Size and then
increment the Front.
Array Implementation of Queue
• struct QueueRecord
• {
– int Capacity;
– int Front = 0;
– int Rear = -1;
– int Size;
– ElementType *Array;
• }
Array Implementation of Queue
• void Enqueue(ElementType x, Queue Q)
• {
– QSize++;
– QRear++;
– QArray[QRear] = x
• }
Array Implementation of Queue
• ElementType Dequeue(Queue Q)
• {
– ElementType temp = QArray[QFront];
– QSize--;
– QFront++;
– return temp;
• }

You might also like