Ds lect 14 - queues (i)

Post on 11-Jul-2015

50 views 5 download

Transcript of Ds lect 14 - queues (i)

Data Structures and

Algorithms

Lecture 14

Queues

Reading: Chapt. 3 - Weiss

Slides Ref. Dr. Sohail Aslam (VU)

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

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.

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)

ABA

CBA

DCBA

DCBrear

front

rearfront

rear

front

rear

front

rear

front

First In First Out

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

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

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.

Implementing Queue

Using linked List:

front

2571 1 7 5 2

frontrear rear

Implementing Queue

Using linked List:

front

2571 1 7 5 2

frontrear rear

front

257 1 7 5 2

frontrear rear

dequeue()

Implementing Queue

Using linked List:

front

2571 1 7 5 2

frontrear rear

front

257 97 5 2

frontrear rear

enqueue(9)

9

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;

}

Implementing Queue

int front()

{

return front->get();

}

int isEmpty()

{

return ( front == NULL );

}

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.

Queue using Array

front

2571

rear

65 7

0

0 1 32 4

front

1 7 5 2

3

rear

Queue using Array

front

2571

rear

65 7

0

0 1 32 4

front

1 7 5 2

4

rear

enqueue(6)

66

Queue using Array

front

2571

rear

65 7

0

0 1 32 4

front

1 7 5 2

5

rear

enqueue(8)

66

88

Queue using Array

front

257

rear

65 7

1

0 1 32 4

front

7 5 2

5

rear

dequeue()

66

88

Queue using Array

front

25

rear

65 7

2

0 1 32 4

front

5 2

5

rear

dequeue()

66

88

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) ??

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”.

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

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

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

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