0% found this document useful (0 votes)
11 views16 pages

DSA Chapter 5

Chapter 5 discusses queues as a linear data structure that follows the First In First Out (FIFO) principle, allowing insertion at one end (REAR) and deletion at the other (FRONT). It outlines basic operations such as enqueue, dequeue, isEmpty, isFull, and peek, along with their algorithms and implementations. The chapter also covers types of queues, ways to implement them, and various applications in computing and data management.

Uploaded by

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

DSA Chapter 5

Chapter 5 discusses queues as a linear data structure that follows the First In First Out (FIFO) principle, allowing insertion at one end (REAR) and deletion at the other (FRONT). It outlines basic operations such as enqueue, dequeue, isEmpty, isFull, and peek, along with their algorithms and implementations. The chapter also covers types of queues, ways to implement them, and various applications in computing and data management.

Uploaded by

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

Chapter 5

Queue and its Application

1
Introduction
• Queue is a linear data structure which enables insert
operations to be performed at one end called REAR
and delete operations to be performed at another
end called FRONT.
• Queue follows the First In First Out (FIFO) rule - i.e.,
the data item stored first will be accessed first.

• For example:
• people waiting in line for a FRONT
rail ticket form a queue.
REAR 2
Introduction
• FIFO Principle of Queue:
– A Queue is like a line waiting to purchase tickets, where
the first person in line is the first person served. (i.e., First
come first serve).
– Position of the entry in a queue ready to be served, that
is, the first entry that will be removed from the queue, is
called the front of the queue (sometimes, head of the
queue), similarly, the position of the last entry in the
queue, that is, the one most recently added, is called the
rear (or the tail) of the queue.

3
Basic Operations of Queue
• Enqueue(): Add an element to the end of the queue.

• Dequeue(): Remove an element from the front of


the queue.

• IsEmpty(): Check if the queue is empty.

• IsFull(): Check if the queue is full.

• Peek(): Get the value of the front of the queue


without removing it.

4
Basic Operations of Queue: peek() & isfull()
peek(): Algorithm: Implementation:
int peek()
begin procedure peek
{
return queue[front]
return queue[front];
end procedure
}

isfull(): Algorithm: Implementation:

begin procedure isfull bool isfull()


if rear equals to MAXSIZE {
return true if(rear == MAXSIZE - 1)
else return true;
return false else
endif return false;
end procedure }
5
Basic Operations of Queue: isempty()
Algorithm:
begin procedure isempty
if front is less than MIN OR front is greater than rear
return true
else
return false
endif
end procedure
Implementation:
bool isempty() If the value of front is less
{ than MIN or 0, it tells that
if(front < 0 || front > rear) the queue is not yet
initialized, hence empty.
return true;
else
return false;
} 6
Working of Queue
▪ Queue operations work as follows:
• Two pointers, FRONT and REAR, are required
• FRONT track the first element of the queue
• REAR track the last element of the queue
• Initially, set value of FRONT and REAR to -1

7
Basic Operations of Queue: enqueue()
• The following steps should be taken to enqueue
(insert) data into a queue:
1. Check if the queue is full or not
2. If the queue is full, produce overflow error and exit.
3. If the queue is not full, increment rear pointer to point
the next empty space.
4. Add data element to the queue location, where the rear
is pointing.
5. return success.

8
Basic Operations of Queue: enqueue()
Algorithm Implementation
begin procedure enqueue(data) int enqueue(int data)
if queue is full {
return overflow if(isfull())
endif return 0;
rear ← rear + 1 rear = rear + 1;
queue[rear] ← data queue[rear] = data;
return true return 1;
end procedure }

9
Basic Operations of Queue: dequeue()
• The following steps are taken to perform dequeue
operation:
1. Check if the queue is empty or not.
2. If the queue is empty, produce underflow error and exit.
3. If the queue is not empty, access the data where front is
pointing.
4. Increment front pointer to point to the next available
data element.
5. Return success.

10
Basic Operations of Queue: dequeue()
Algorithm Implementation
begin procedure dequeue int dequeue()
if queue is empty {
return underflow if(isempty())
endif return 0;
data = queue[front] int data = queue[front];
front ← front + 1 front = front + 1;
return true return data;
end procedure }

11
Types of Queue
In Linear Queue, an insertion
takes place from one end while
the deletion occurs from another
end. It is a special type of queue
data structure in which
every element has a
priority associated with it.

It is similar to the linear


Queue except that the last
element of the queue is
connected to the first element.
In Double Ended Queue, insertion and
deletion can be done from both ends of
the queue either from the front or rear.

12
Ways to implement the queue
• There are two ways of implementing the Queue:
Array and Linked list.
Using Array
void enqueue(int queue[], int item) int dequeue (int queue[], int item)
{ {
if (isfull()) if (isempty())
{ {
cout<<"overflow"; cout<<"underflow";
} }
else else
{ {
rear = rear + 1; item = queue[front];
queue[rear]=item; front = front + 1;
} return item;
} }
}
13
Ways to implement the queue: using Linked list
void enqueue(struct node *ptr, int item) { void dequeue (struct node *ptr)
ptr = (struct node *) malloc (sizeof(struct node)); {
if(ptr == NULL) { if(front == NULL)
cout<<"\nOVERFLOW\n"; {
return; cout<<"\nUNDERFLOW\n";
} return;
else { }
ptr -> data = item; else
if(front == NULL) { {
front = ptr; ptr = front;
rear = ptr; front = front -> next;
front -> next = NULL; delete ptr;
rear -> next = NULL; }
} }
else {
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
} 14
Applications of Queue
• In CPU scheduling and Disk Scheduling.
• In asynchronous transfer of data (where data is not
being transferred at the same rate between two
processes) for eg. pipes, file IO, sockets.
• In operating systems for handling interrupts.
• As buffers in most of the applications like MP3 media
player, CD player, etc.
• To maintain the play list in media players in order to
add and remove the songs from the play-list.
• Center phone systems use Queues to hold people
calling them in order.
15
Thank You

Question?

16

You might also like