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

Queues: Ref.: D.S. Malik, Data Structures Using C++

Queues can be implemented using arrays. Elements are added to the rear of the queue and removed from the front. As elements are added and removed, pointers track the front and rear of the queue. If the queue reaches the end of the array, it is made circular by returning to the beginning so addition can continue indefinitely.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Queues: Ref.: D.S. Malik, Data Structures Using C++

Queues can be implemented using arrays. Elements are added to the rear of the queue and removed from the front. As elements are added and removed, pointers track the front and rear of the queue. If the queue reaches the end of the array, it is made circular by returning to the beginning so addition can continue indefinitely.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Queues

Ref.: D.S. Malik, Data Structures Using C++

Queues
add adding an item to the back of a queue delete removing an item from the front of a queue

Implementation of Queues as Arrays


e.g. First create an array; the queue is empty add A to the queue:
[0] [1] [2] A [99]

queueFront = 0 queueRear = 0

Implementation of Queues as Arrays


adding B followed by C gives:

[0] [1] [2] A B C

[99]

queueFront = 0 queueRear = 2

Implementation of Queues as Arrays


deleting an element gives:

[0] [1] [2] A B C

[99]

queueFront = 1 queueRear = 2

Implementation of Queues as Arrays


Student exercise: Start from an empty queue. What does the queue look like after: add A, add B, add C delete, add D, delete, add E delete, add F, delete, add G delete, add H, delete, add I delete, add J

Implementation of Queues as Arrays


Answer:

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] A B C D E F G H I J

[99]

queueFront = 7 queueRear = 9

Implementation of Queues as Arrays


If we keep deleting and adding like this then we will eventually reach the end of the array:
[0] [1] [2] A B C [97] [98] [99] x x x

queueFront = 97 queueRear = 99 We need to program the queue so that it can continue to operate.

Implementation of Queues as Arrays


Conceptually, we could think of it as a circular queue.

[99]

[0]

Implementation of Queues as Arrays


e.g.
[0] [1] [2] [97] [98] [99] X Y

queueFront = 98

queueRear = 99

Student exercise: Add Z. Redraw the queue and give the values of the variables.

Implementation of Queues as Arrays


Answer:
[0] [1] [2] Z [97] [98] [99] X Y

queueFront = 98

queueRear = 0

You might also like