25
Data Structures and Algorithms Lecture 14 Queues Reading: Chapt. 3 - Weiss Slides Ref. Dr. Sohail Aslam (VU)

Ds lect 14 - queues (i)

Embed Size (px)

Citation preview

Page 1: Ds   lect 14 - queues (i)

Data Structures and

Algorithms

Lecture 14

Queues

Reading: Chapt. 3 - Weiss

Slides Ref. Dr. Sohail Aslam (VU)

Page 2: Ds   lect 14 - queues (i)

Queue

Stores a set of elements in a particular order

A stack is LIFO (Last-In First Out) structure.

In contrast, a queue is a FIFO (First-In First-Out ) structure.

A queue is a linear structure for which items can be only inserted at one end and removed at another end.

It means: the first element inserted is the first one to be removed

Example

The first one in line is the first one to be served

Page 3: Ds   lect 14 - queues (i)

Queues

Queue is an ordered-list in which all the insertions

and deletions are made at two different ends to

maintain the FIFO order.

Queue is implemented by maintaining one pointer to

the front element in the Queue and another pointer

pointing to the rear of the Queue.

Insertions are made at the rear and deletions are

made from the front.

Page 4: Ds   lect 14 - queues (i)

Queue Applications

Real life examples

Waiting in line

Waiting on hold for tech support

Applications related to Computer Science

Threads

Job scheduling (e.g. Round-Robin algorithm for CPU

allocation)

Page 5: Ds   lect 14 - queues (i)

ABA

CBA

DCBA

DCBrear

front

rearfront

rear

front

rear

front

rear

front

First In First Out

Page 6: Ds   lect 14 - queues (i)

front rear Q[0] Q[1] Q[2] Q[3] Comments

-1

-1

-1

-1

0

1

-1

0

1

2

2

2

J1

J1 J2

J1 J2 J3

J2 J3

J3

queue is empty

Job 1 is added

Job 2 is added

Job 3 is added

Job 1 is deleted

Job 2 is deleted

Applications: Job Scheduling

Page 7: Ds   lect 14 - queues (i)

Queue Operations

Enqueue(X) – place X at the rear of the queue.

Dequeue() -- remove the front element and return it.

Front() -- return front element without removing it.

IsEmpty() -- return TRUE if queue is empty, FALSE

otherwise

Page 8: Ds   lect 14 - queues (i)

Implementing Queue

Using linked List: Recall

Insert works in constant time for either end of a

linked list.

Remove works in constant time only.

Seems best that head of the linked list be the front of

the queue so that all removes will be from the front.

Inserts will be at the end of the list.

Page 9: Ds   lect 14 - queues (i)

Implementing Queue

Using linked List:

front

2571 1 7 5 2

frontrear rear

Page 10: Ds   lect 14 - queues (i)

Implementing Queue

Using linked List:

front

2571 1 7 5 2

frontrear rear

front

257 1 7 5 2

frontrear rear

dequeue()

Page 11: Ds   lect 14 - queues (i)

Implementing Queue

Using linked List:

front

2571 1 7 5 2

frontrear rear

front

257 97 5 2

frontrear rear

enqueue(9)

9

Page 12: Ds   lect 14 - queues (i)

Implementing Queue

int dequeue()

{

int x = front->get();

Node* p = front;

front = front->getNext();

delete p;

return x;

}

void enqueue(int x)

{

Node* newNode = new Node();

newNode->set(x);

newNode->setNext(NULL);

rear->setNext(newNode);

rear = newNode;

}

Page 13: Ds   lect 14 - queues (i)

Implementing Queue

int front()

{

return front->get();

}

int isEmpty()

{

return ( front == NULL );

}

Page 14: Ds   lect 14 - queues (i)

Queue using Array

If we use an array to hold queue elements, both

insertions and removal at the front (start) of the array

are expensive.

This is because we may have to shift up to “n”

elements.

For the stack, we needed only one end; for queue

we need both.

To get around this, we will not shift upon removal of

an element.

Page 15: Ds   lect 14 - queues (i)

Queue using Array

front

2571

rear

65 7

0

0 1 32 4

front

1 7 5 2

3

rear

Page 16: Ds   lect 14 - queues (i)

Queue using Array

front

2571

rear

65 7

0

0 1 32 4

front

1 7 5 2

4

rear

enqueue(6)

66

Page 17: Ds   lect 14 - queues (i)

Queue using Array

front

2571

rear

65 7

0

0 1 32 4

front

1 7 5 2

5

rear

enqueue(8)

66

88

Page 18: Ds   lect 14 - queues (i)

Queue using Array

front

257

rear

65 7

1

0 1 32 4

front

7 5 2

5

rear

dequeue()

66

88

Page 19: Ds   lect 14 - queues (i)

Queue using Array

front

25

rear

65 7

2

0 1 32 4

front

5 2

5

rear

dequeue()

66

88

Page 20: Ds   lect 14 - queues (i)

Queue using Array

front

25

rear

65 7

2

0 1 32 4

front

5 2

7

rear

enqueue(9)

enqueue(12)

66

88

99

1212

enqueue(21) ??

Page 21: Ds   lect 14 - queues (i)

Queue using Array

We have inserts and removal running in constant

time but we created a new problem.

Cannot insert new elements even though there are

two places available at the start of the array.

Solution: allow the queue to “wrap around”.

Page 22: Ds   lect 14 - queues (i)

Queue using Array

Basic idea is to picture the array as a circular array.

front

25

rear2

front

7

rear

6 8 9 12

6

5

7

0 1

3

2

4

5

268

9

12

Page 23: Ds   lect 14 - queues (i)

Queue using Array

void enqueue(int x)

{

rear = (rear+1)%size;

array[rear] = x;

noElements = noElements+1;

}

front

25

rear2

front

0

rear

6 8 9 12

6

5

7

0 1

3

2

4

5

268

9

12

enqueue(21)

21

218

size

7

noElements

Page 24: Ds   lect 14 - queues (i)

Queue using Array

int isFull()

{

return noElements == size;

}

int isEmpty()

{

return noElements == 0;

}

front

25

rear2

front

1

rear

6 8 9 12

6

5

7

0 1

3

2

4

5

268

9

12

enqueue(7)

21

218

size

8

noElements

7

7

Page 25: Ds   lect 14 - queues (i)

Queue using Array

int dequeue()

{

int x = array[front];

front = (front+1)%size;

noElements = noElements-1;

return x;

}

front rear4

front

1

rear

6 8 9 12

6

5

7

0 1

3

2

4

689

12

dequeue()

21

218

size

6

noElements

7

7