[go: up one dir, main page]

0% found this document useful (0 votes)
67 views25 pages

Queus

The document discusses queues as a data structure. It covers representing queues using sequential organization, common queue operations like enqueue and dequeue, applications of queues like job scheduling, and different types of queues like circular queues and double-ended queues. Key topics include the abstract data type of a queue, implementations using arrays and linked lists, and how operating systems use queues to schedule and track computer jobs.

Uploaded by

Nisha Jaiswal
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)
67 views25 pages

Queus

The document discusses queues as a data structure. It covers representing queues using sequential organization, common queue operations like enqueue and dequeue, applications of queues like job scheduling, and different types of queues like circular queues and double-ended queues. Key topics include the abstract data type of a queue, implementations using arrays and linked lists, and how operating systems use queues to schedule and track computer jobs.

Uploaded by

Nisha Jaiswal
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/ 25

Fundamentals of Data Structures

S. Y. B. Tech CSE Trimester – III

SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY


Queue
Topics to be
Covered
❑ Queue as an Abstract Data Type

❑ Representation of Queue Using Sequential Organization

❑ Applications of Queue

DATA STRUCTURE -I UNIT-IV 2


Unit : V

Queue

DATA STRUCTURE -I UNIT-IV 3


Queue
Queue as a Data Structure
Queue
Queue is an ordered list (linear data structure) in which
insertions(Enqueue) are done at rear end and deletions(dequeue)
are done at the front end of the Queue.
ADT of Queue

CREATEQ(Q) which creates Q as an empty queue;


ADDQ(i,Q) which adds the element i to the rear of a queue and
returns the new queue;
DELETEQ(Q) which removes the front element from the queue
Q and returns the resulting queue;
FRONT(Q) which returns the front element of Q;
ISEMTQ(Q) which returns true if Q is empty else false.

DATA STRUCTURE -I UNIT-IV 6


ADT of Queue (Cont’)
A complete specification of 6 for all Q queue, i item let

this data structure is ISEMTQ(CREATEQ) :: = true


ISEMTQ(ADDQ(i,Q)) :: = false
structure QUEUE (item)
DELETEQ(CREATEQ) :: = error
1 declare CREATEQ( )
DELETEQ(ADDQ(i,Q)):: =
queue
if ISEMTQ(Q) then CREATEQ
2 ADDQ(item,queue) ฀queue else ADDQ(i,DELETEQ(Q))
3 DELETEQ(queue) ฀queue FRONT(CREATEQ) :: = error

4 FRONT(queue) ฀item FRONT(ADDQ(i,Q)) :: =


if ISEMTQ(Q) then i else
5 ISEMTQ(queue) ฀boolean; FRONT(Q)
16 end
17 end
DATA STRUCTURE -I QUEUE
UNIT-IV 7
Applications of Queue
Queues, like stacks, also arise quite naturally in the computer solution of many
problems.
The most common occurrence of a queue in computer applications is for the
scheduling of jobs.
In batch processing the jobs are ''queued-up'' as they are read-in and executed,
one after another in the order they were received.
Operations on Queue
Adding an element at the rear of the Queue
Delete the front element from the queue
PeepRear returns the rear element of the queue
PeepFront returns the front element of the queue
isFull returns if queue is full
isEmpty returns if queue is empty
Adding a element
Algorithm AddQ(q[],elem) int isfull()
{ { if(rear==size-1)

if(isfull()) return 1

print “Queue is full ” else


return 0
else
}
{ rear=rear+1
q[rear]=elem
}
}
Deleting an element from Queue
Algorithm DelQ(q[]) int isempty()
{ { if(rear==front)
if(isempty())
return 1
return -1
else
else
return 0
{ front=front+1
}
elem=q[front]
return elem
}
}
Queue operations
AddQ(10)
AddQ(18)
E=delQ()
AddQ(20)
E=delQ()
E=delQ()
E=delQ()
Circular Queue
A more efficient queue representation is obtained by regarding the array
Q(1:n) as circular. It now becomes more convenient to declare the array as
Q(0:n - 1).
When rear = n - 1, the next element is entered at Q(0) in case that spot is
free. Using the same conventions as before, front will always point one
position counterclockwise from the first element in the queue.
Circular Queue
Circular Queue
Initially front=rear=0
Algorithm AddCQ(elem)
{ //insert items in the CQ stored in Q[0..n-1]
//rear points to the last item & front is one
//position counter clockwise from the first
if (rear +1 ) %n== front
print “queue full”
else
{ rear=(rear+1) %n
Q[rear]=elem
}
}
Circular Queue
Initially front=rear=0
Algorithm DelCQ(elem)
{ //removes the front element of the queue
if front==rear
print “queue empty”
else
{ front=(front+1) %n
elem=Q[front]
return elem
}
}
Linked Queue and Operations

front rear

Queue implemented using Singly linked list


Double Ended Queue(Deque)
● A double-ended queue (deque) is an abstract data type that

generalizes a queue, for which elements can be added to or


removed from either the front or rear.
● Hybrid linear structure provides all the capabilities of stacks

and queues in a single data structure.

18
Types of deque
● Input-restricted deque
Deletion can be made from both ends , but Insertion can be made
at one end only.
● Output-restricted deque
Insertion can be made at both ends , but Deletion can be made
from one end only.

19
deque Operations
● pushRear() - Insert element at back

● pushFront() - Insert element at front


● popRear() - Remove last element
● popFront() - Remove first element

● isEmpty() – Checks whether the queue is empty or not.

20
deque Example
Operation Deque content
pushFront(‘a’) [‘a’]
pushFront(‘b’) [‘b’ , ‘a’]
pushRear(‘c’) [‘b’ , ‘a’ , ‘c’]

popFront() [‘a’ , ‘c’]


popRear() [‘a’]

21
Queue Applications: Job Scheduling

❖Job scheduling is the process of allocating system resources to many


different tasks by an operating system (OS).
❖The system handles prioritized job queues that are awaiting CPU time
and it should determine which job to be taken from which queue and the
amount of time to be allocated for the job.
❖This type of scheduling makes sure that all jobs are carried out fairly
and on time.
❖Job scheduling is performed using job schedulers. Job schedulers are
programs that enable scheduling and, at times, track computer "batch"
jobs, or units of work
The Operating System maintains the following important process
scheduling queues −
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing
in main memory, ready and waiting to execute. A new process is
always put in this queue.
Device queues − The processes which are blocked due to
unavailability of an I/O device constitute this queue.

DATA STRUCTURE -I UNIT-IV 23


FAQS
1. Write an ADT for Queue
2. What are the primitive operations of
Queue.
3. Explain with example Queue
applications.

DATA STRUCTURE -I UNIT-


IV
24
Takeaways
❑ Queue is First in First Out(FIFO) Data Structure

❑ Primitive operations on Queue are enqueue, dequeue,isEmpty and isFull

❑ Queue can be represented by using Array as well as linked list.

❑ Queue is commonly used in Job Sequencing by OS.

DATA STRUCTURE -I UNIT-


IV
25

You might also like