0% found this document useful (0 votes)
9 views13 pages

DS 6

The document discusses queues and their implementation using arrays. Queues follow FIFO ordering and elements are added to the rear and removed from the front. An array implementation uses indexes to track the front and rear elements. Operations like enqueue and dequeue are used to add and remove elements from the queue.

Uploaded by

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

DS 6

The document discusses queues and their implementation using arrays. Queues follow FIFO ordering and elements are added to the rear and removed from the front. An array implementation uses indexes to track the front and rear elements. Operations like enqueue and dequeue are used to add and remove elements from the queue.

Uploaded by

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

Data Structures & Algorithms

Syed Ali Raza


Lecture 6
Queue

• Elements added at one end (rear), deleted


from other end (front)
– First In First Out (FIFO)
• Middle elements inaccessible
0 1 2 3 4 5 6 7
17 23 97 44

front = 0 rear = 3
• To insert: put new element in location 4, and set rear to 4
• To delete: take element from location 0, and set front to 1
Queue

front = 0 rear = 3

Initial queue: 17 23 97 44

After insertion: 17 23 97 44 333

After deletion: 23 97 44 333

front = 1 rear = 4

• Notice how the array contents “crawl” to the right as elements are
inserted and deleted
• This will be a problem after a while!
Main queue operations

• enqueue(): inserts an element at the end


of the queue

• dequeue(): removes and returns the


element at the front of the queue
Auxiliary queue operations

• front(): returns the element at the front without


removing it
• size(): returns the number of elements stored
• isEmpty(): indicates whether no elements are
stored
• isFull(): indicates whether all elements are
stored
• Exceptions
– Attempting the execution of dequeue or front on an
empty queue throws an EmptyQueueException
Applications of Queues

• Waiting lines
• Access to shared resources (e.g., printer)
• Scheduling Algorithms
Array based Implementation of Queues

• A queue can be implemented with an


array.
• For example, this queue contains the
integers 4 (at the front) and 6 (at the rear).
[0] [1] [2] [3] [4] [5] ...

4 8 6

An array of
integers to We don't care what's in
implement a this part of the array.
queue of integers
Array based Implementation of Queues

• The easiest implementation also 3 size


keeps track of the number of items
in the queue and the index of the 0 first
first element (at the front of the
queue), the last element (at the
2 last
rear).

[0] [1] [2] [3] [4] [5] ...

4 8 6
Array based Implementation of Queues

• When an element leaves the 2 size


queue, size is decremented, and
first changes, too. 1 first

2 last

[0] [1] [2] [3] [4] [5] ...

4 8 6
Array based Implementation of Queues

• When an element enters the 3 size


queue, size is incremented, and
last changes, too. 1 first

3 last

[0] [1] [2] [3] [4] [5] ...

8 6 2
Array based Implementation of Queues

• Easy to implement 3 size


• But it has a limited capacity with a fixed array
• Or you must use a dynamic array for an 0 first
unbounded capacity
• Special behavior is needed when the rear
2 last
reaches the end of the array.

[0] [1] [2] [3] [4] [5] ...

4 8 6
Array based Implementation of Queues

• Suppose we want to add a new 3 size


element to this queue, where the
last index is [5]: 3 first

5 last

[0] [1] [2] [3] [4] [5]


2 6 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy