DSA Chapter 5
DSA Chapter 5
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.
4
Basic Operations of Queue: peek() & isfull()
peek(): Algorithm: Implementation:
int peek()
begin procedure peek
{
return queue[front]
return queue[front];
end procedure
}
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.
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