0% found this document useful (0 votes)
11 views

L6

Uploaded by

zakwan1292003
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)
11 views

L6

Uploaded by

zakwan1292003
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 and

Algorithms(CSE2001)

MODULE 1
Stack and Queues - Linear Data Structures

1-1
What’s this ?

2
Queue

3
Queue
• A stack is LIFO (Last-In First Out) structure. In
contrast, a queue is a FIFO (First-In First-Out )
structure.
• In a FIFO data structure, the first element
added to the queue will be the first one to be
removed.
• A queue is a linear structure for which items
can be only inserted at one end and removed at
another end.

4
Operations on Queue
• We will dedicate once again six operations on
the Queue.
• You can add a person to the queue (enque),
• Look who is the first person to be serviced (front)
• Rremove the first person (dequeue) and
• Check whether the queue is empty (isEmpty).
• Also there are two more operations to create and
to destroy the queue.

5
Operations
• Queue create()
• Creates an empty queue
• boolean isEmpty(Queue q)
• Tells whether the queue q is empty
• enqueue(Queue q, Item e)
• Place e at the rear of the queue q
• destroy(Queue q)
• destroys queue q

6
Operations
• Item front(Queue q)
• returns the front element in Queue q
without removing it
Precondition: q is not empty
• dequeue(Queue q)
• removes front element from the queue q
Precondition: q is not empty

7
Implementation Using Arrays
• If we use an array to hold queue elements,
dequeue operation at the front (start) of the array
is 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.

8
Snapshot of a Queue

9
enqueue()

10
enqueue() again

11
dequeue()

12
dequeue() again

13
Two more enqueue()

14
What’s Wrong ?

• We have inserts and removal running in


constant time but we created a new
problem.
• We cannot insert new elements even though
there are two places available at the start of
the array.
• Solution: allow the queue to “wrap around”.
Basic idea is to picture the array as a circular
array.

15
Queue array wrap-around

16
enqueue(element)
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}

17
dequeue()
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}

18
isEmpty() and isFull()
int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}

19
isEmpty() and isFull()
int isFull()
{
return noElements == size;
}

int isEmpty()
{
return noElements == 0;
}

20
Simple Queue or Linear Queue
• In Linear Queue, an insertion takes place from one end while
the deletion occurs from another end. The end at which the
insertion takes place is known as the rear end, and the end at
which the deletion takes place is known as front end.
Circular Queue
• In Circular Queue, all the nodes are represented as circular. It is
similar to the linear Queue except that the last element of the
queue is connected to the first element. It is also known as Ring
Buffer, as all the ends are connected to another end.
Priority Queue
• It is a special type of queue in which the elements are arranged
based on the priority. It is a special type of queue data structure
in which every element has a priority associated with it.
Insertion in priority queue takes place based on the arrival,
while deletion in the priority queue occurs based on the
priority. Priority queue is mainly used to implement the CPU
scheduling algorithms.
Deque (or, Double Ended Queue)
• In Deque or Double Ended Queue, insertion and
deletion can be done from both ends of the queue
either from the front or rear. It means that we can
insert and delete elements from both front and rear
ends of the queue.
Deque (or, Double Ended Queue)
• There are two types of deque:
• Input restricted deque

• Output restricted deque


Applications of Queues
• Queue is used when things don’t have to be processed
immediatly, but have to be processed in First In First Out order
like Breadth First Search.
• This property of Queue makes it also useful in following kind of
scenarios.
• When a resource is shared among multiple consumers.
Examples include CPU scheduling, Disk Scheduling.
• When data is transferred asynchronously (data not necessarily
received at same rate as sent) between two processes.
Examples include IO Buffers, pipes, file IO, etc.
Applications of Queues
• Operating systems often maintain a queue of processes that are
ready to execute or that are waiting for a particular event to
occur.
• Computer systems must often provide a “holding area” for
messages between two processes, two programs, or even two
systems. This holding area is usually called a “buffer” and is
often implemented as a queue.
• Call center phone systems will use a queue to hold people in
line until a service representative is free.
Applications of Queues
• Buffers on MP3 players and portable CD players, iPod playlist.
Playlist for jukebox - add songs to the end, play from the front
of the list.
• Round Robin (RR) algorithm is an important scheduling
algorithm. It is used especially for the time-sharing system. The
circular queue is used to implement such algorithms.

You might also like