0% found this document useful (0 votes)
17 views29 pages

Lec 4-DS Algo

The document covers the concept of queues in data structures, highlighting that a queue operates on a FIFO (First-In First-Out) basis, contrasting with stacks which are LIFO (Last-In First Out). It discusses two primary implementations of queues: using circular arrays and linked lists, detailing the operations of enqueue and dequeue, as well as algorithms for insertion and deletion. Additionally, it addresses the challenges of managing queue elements in an array and introduces the concept of a circular array to optimize space utilization.

Uploaded by

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

Lec 4-DS Algo

The document covers the concept of queues in data structures, highlighting that a queue operates on a FIFO (First-In First-Out) basis, contrasting with stacks which are LIFO (Last-In First Out). It discusses two primary implementations of queues: using circular arrays and linked lists, detailing the operations of enqueue and dequeue, as well as algorithms for insertion and deletion. Additionally, it addresses the challenges of managing queue elements in an array and introduces the concept of a circular array to optimize space utilization.

Uploaded by

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

Data Structures & Algorithm

CS-102

Lecture 4

Queues

Lecturer: Syeda Nazia Ashraf


1
Queues
• 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.
• In other words, the order in which elements enters a queue is
the order in which they leave.
• There are main two ways to implement a queue :
1. Circular queue using array
2. Linked Structures (Pointers)
• Primary queue operations:
• Enqueue: insert an element at the rear of the queue
• Dequeue: remove an element from the front of the queue
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 rear front rear

1 7 5 2 1 7 5 2
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

dequeue()

front rear front rear

7 5 2 1 7 5 2
Implementing Queue
 Using linked List:

front rear front rear

1 7 5 2 1 7 5 2

enqueue(9)

front rear front rear

7 5 2 9 7 5 2 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 rear
1 7 5 2
1 7 5 2
0 1 2 3 4 5 6 7
front rear
0 3
Queue using Array
enqueue(6)

front rear
1 7 5 2 6
1 7 5 2 6
0 1 2 3 4 5 6 7
front rear
0 4
Queue using Array
enqueue(8)

front rear
1 7 5 2 6 8
1 7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
0 5
Queue using Array
dequeue()

front rear
7 5 2 6 8
7 5 2 6 8
0 1 2 3 4 5 6 7
front rear
1 5
Queue using Array
dequeue()

front rear
5 2 6 8
5 2 6 8
0 1 2 3 4 5 6 7
front rear
2 5
Queue using Array
enqueue(9)
enqueue(12)

front rear
5 2 6 8 9 12
5 2 6 8 9 12
0 1 2 3 4 5 6 7
front rear
2 7
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.

0 1
front
front rear
7 2 2
12 5
5 2 6 8 9 12
6 9 2
8 6 3 rear
7
5 4
Queue using Array
enqueue(21)
0 1
front rear 21 front size
7 2 2 8
12 5
5 2 6 8 9 12 21
6 9 2
8 6 3 rear noElements
0 7
5 4
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
Queue using Array
enqueue(7)
0 1
front rear 21 7 front size
7 2 2 8
12 5
5 2 6 8 9 12 21 7
6 9 2
8 6 3 rear noElements
1 8
5 4
int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}
Queue using Array
dequeue()
0 1

front rear 21 7 front size


7 2 4 8
12
6 8 9 12 21 7
6 9
8 6 3 rear noElements
1 6
5 4
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
Following is the algorithm which describes the implementation of Queue using an
Array.

Insertion in Queue:
Algorithm: ENQUEUE(QUEUE, MAXSIZE, FRONT, REAR,COUNT, ITEM)
This algorithm inserts an element ITEM into a circular queue.
1. [QUEUE already filled?]
If COUNT = MAXSIZE then: [ COUNT is number of values in the QUEUE]
Write: OVERFLOW, and Return.
2. [Find new value of REAR.]
If COUNT= 0, then: [Queue initially empty.]
Set FRONT= 0 and REAR = 0
Else: if REAR = MAXSIZE - 1, then:
Set REAR = 0
Else:
Set REAR = REAR+1.
[End of If Structure.]
3. Set QUEUE[REAR] = ITEM. [This insert new element.]
4. COUNT=COUNT+1 [ Increment to Counter. ]
5. Return.
Deletion in Queue:
Algorithm: DEQUEUE(QUEUE, MAXSIZE, FRONT, REAR,COUNT, ITEM)
This procedure deletes an element from a queue and assigns it to the
variable ITEM.
1. [QUEUE already empty?]
If COUNT= 0, then: Write: UNDERFLOW, and Return.
2. Set ITEM = QUEUE[FRONT].
3. Set COUNT = COUNT -1
4. [Find new value of FRONT.]
If COUNT = 0, then: [There was one element and has been deleted ]
Set FRONT= -1, and REAR = -1.
Else if FRONT= MAXSIZE, then: [Circular, so set Front = 0 ]
Set FRONT = 0
Else:
Set FRONT:=FRONT+1.
[End of If structure.]
5. Return ITEM
Following Figure shows that how a queue may be maintained by a circular array with
MAXSIZE = 6 (Six memory locations). Observe that queue always occupies
consecutive locations except when it occupies locations at the beginning and at the
end of the array. If the queue is viewed as a circular array, this means that it still
occupies consecutive locations. Also, as indicated by Fig(k), the queue will be empty
only when Count = 0 or (Front = Rear but not null) and an element is deleted. For
this reason, -1 (null) is assigned to Front and Rear.

You might also like