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

queue notes UNIT-V

Queue imp notes

Uploaded by

Himesh Kochale
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)
15 views

queue notes UNIT-V

Queue imp notes

Uploaded by

Himesh Kochale
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/ 10

Unit-III QUEUE Marks-20

Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)


4.1 Introduction of Queue
Q.Define queue
Q.Explain the front and rear for queue
Q.Define the term FRONT and REAR of the queue.?
Q.Explain the terms front and rear with relevance to queue?

4.1 Introduction of Queue or (Linear Queue)


Queue is (FIFO)First in First out.
Where item are inserted at one end(Rear) and
item are deleted from other end(Front).

4.1.1 Array Representation of Queues


Q.Explain how two pointer front end and rear related to queue with diagram?
Q.Sketch representation of queue as an array ?
Q.Define following term :empty Queue

Fig: Empty Queue after initialization


During initialization of queue , its front and rear are set to -1

UNIT-III Page 1
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)

Above queue is called as empty queue


Following observations from above operations
1)If the queue is empty then front= -1 and Rear = -1
2)If the queue is full then rear =MAX-1
Where MAX is size of Array.
3)If Rear =Front then queue contain only one element.
4)If Rear > Front then queue is non-empty.

4.2 Queue as an ADT (Abstract Data type )

typedef struct queue


{
int data[size];
int F, R ;
} queue;

 Size is a constant, maximum number of elements that are stored.


 One dimensional array can be used to hold elements of Queue.
 F and R indicate that F is Front and R is Rear.

UNIT-III Page 2
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
4.2.1 Operation on Queue (implemented by using Array)
OR ‘C’ Function for primitive operations on Queue
Q.Explain any four terms in queue?
Q.Write a procedure to insert an element into queue and to delete an element from a queue.
Q.Write the procedure for inserting an element in queue.
Q.Explain queues implementations using linked list?
Q. Enlist any four operations of queue.
Q.List the operations to be performed at front and rear and.

4.2.1 ‘C’ Function for primitive operations on Queue.


1) ‘C’ Function initialize()
void initialize(queue *q)
{
q F= -1;
q R= -1;
}
Initializes a queue by setting the value of Front and Rear to -1.
Element of the structure queue can be accessed through the pointer q.
[qF , qR , qdata[]

2) ‘C’ Function Queue is empty


int empty(queue *q)
{
If(qR= = -1)
return (1); // Queue is empty
return (0); // Queue is not empty
}
Above function check whether Queue is empty or not.
if it return 1 then Queue is empty or
if it return 0 then Queue is not empty.

3) ‘C’ Function Queue is Full


int full(queue *q)
{
if (qR= = MAX-1)
return(1);
return(0);
}
Above function check whether queue is full or not.
if it return 1 then queue is full or
if it return 0 then queue is not full.

Algorithm for insertion in a queue [C’ Function for insertion in queue.]


1)Inserting for an empty queue
a)R=F=0;
b)data[R]=x //x is the element to be inserted.
2)Inserting in non-empty queue
R=R+1;
Data[R]=x

UNIT-III Page 3
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
Rear is increased by 1 and the element x is stored at the rear end of the queue.
4) ‘C’ Function for insertion in queue.
enqueue function
Void enqueue(queue *q,int x)
{
if (qR = = -1)
{
qR= qF=0;
qdata[qR]=x;
}
else
{
qR=qR+1;
qdata[qR]=x;
}
}

Algorithm for Deletion from a queue. ‘C’ Function for Deletion from queue.
a)Deletion of the last element or only element(R=F)
1) x =data[F]
2)R=F= -1;
3)return (x);
b)Deletion of the element when the queue length >1
a) x = data[F]
b)F= F+1
c)return(x);

5) ‘C’ Function for Deletion from queue.


Int dequeue (queue *q)
{
int x;
x= qdata[qF]
if(qR = = qF)
{
qR=-1;
qF= -1;
}
else
qF=qF+1;
Return(x);
}

Q.Write a menu driven program in c languages for queue having menus:store,retrive and display
Queue.
Q.Write a C programming to implement queue with insert and delete operations.
Q.Write a menu driven program to insert and delete element in Queue and display Queue
Q.Write a program to insert element in queue.
Q.Write a code delete an element in queue.

UNIT-III Page 4
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
4.3 Types of Queue
There are three types of queue
1)Linear Queue refer 4.1 Introduction of Queue
2)Circular Queue
3)Priority Queue

Problem of using Linear Queue


Q.What is potential problem occur in linear queue while using?
Q.What is disadvantage of Linear Queue?
Q.What is main issue occur in linear queue while using?
Problem of using Linear Queue

Fig: after insertion of the five element in array (queue) as shown in fig.

Rear=4
Queue is full
Front=0

Fig: after deletion of the two element in array (queue) as shown in fig.
Rear=4
Queue is full

Front=2
Queue is full as there is no empty space ahead of rear. we can not insert the next element
even if there is a space in front of queue.

2)Circular Queue
Q.Write suitable diagram explain the principle of circular queue.?
Q.Draw and explain circular queue in details.
Q.Define circular queue.?also explain advantages of circular queue over linear queue with example.
Q.Explain the concept of circular queue with example.
Q.What is circular queue?
Q.Describe circular queue with an example?
Q.Define circular queue.
Q.Describe circular queue .give its advantages ?

UNIT-III Page 5
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
2)Circular Queue:
 Circular queue is a linear data structure in which last node is connected back to first node to form
circle.
 It follows FIFO Principle.
 Elements are inserted at rear end and deleted from front end.
 It is also known as “Ring Buffer”.
 Only one pointer is required to be maintained.

If we have Array size is 5 i.e MAX is 5 then we have implementation for circular queue.
i=(i+1)% MAX
Where i will start from 0 its first index in array i.e 0
i (i+1)%MAX
0 (0+1)%5=1
1 (1+1)%5=2
2 (2+1)%5=3

UNIT-III Page 6
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
3 (3+1)%5=4
4 (4+1)%5=0

When we move around an array of size five following sequence for location should be generated.
0123401234…………

Data type for Queue in circular queue or circular array ADT (Abstract Data type )
typedef struct queue
{
int data[size];
int F, R ;
} queue;

size is a constant ,maximum number of elements that are stored.


One dimensional array can be used to hold elements of Queue.
F and R indicate that F is Front and R is Rear.
Various operation on the queue in a circular array
1) ‘C’ Function initialize()
void initialize(queue *q)
{
q F= -1;
q R= -1;
}
Initializes a queue by setting the value of Front and Rear to -1.
Element of the structure queue can be accessed through the pointer q.
[qF , qR , qdata[]
2) ‘C’ Function Queue is empty
int empty(queue *q)
{
If(qR= = -1)
return (1); // Queue is empty
return (0); // Queue is not empty
}
Above function check whether Queue is empty or not.
if it return 1 then Queue is empty or
if it return 0 then Queue is not empty.

3) ‘C’ Function Queue is Full


int full(queue *q)
{
if (qR+1) %MAX = = qF
return(1);
return(0);
}
Front will be found immediately ahead of rear in queue that is full.
if it return 1 then queue is full or
if it return 0 then queue is not full.

UNIT-III Page 7
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
4) ‘C’ Function for insertion in queue.
enqueue function
Void enqueue(queue *q,int x)
{
if (empty(q)) /* empty queue */
{
qR=qF==0;
qdata[qR]=x;
}
else
{
qR=(qR+1)%MAX;
qdata[qR]=x;
}
}

5) ‘C’ Function for Deletion from queue.


Int dequeue (queue *q)
{
int x;
x= qdata[qF]
if(qR = = qF)
{
qR=-1;
qF= -1;
}
else
qF=(qF+1)%MAX;
Return(x);
}

4.4 Priority Queue


Q.Define priority queue .describe the one way list representation of a priority queue with suitable
example and diagram.
Q.Describe priority queue.?
Q.Describe priority queue along with an example?
Q.Explain priority queue with example and its types.?
Q.Explain concept of the priority queue with suitable example.
Q.Describe priority queue and list its advantages?
Q.With neat sketch explain working of priority queue.
Q.What is priority queue ?Describe working of priority queue with suitable example?
Q.Define following term :priority queue?
4.4 Priority Queue
 Priority queue is order list of homogeneous elements.
 An element with high priority is processed before other elements with low priority.
 Elements with same priority are processed on “first come first severed” basis.
 Example:
 Hospital waiting room-
 a patient having more serious problem will be admitted before other patients.

UNIT-III Page 8
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
 Application :
 Scheduling of job processed in computer.

Implementation of Priority queue using circular queue or circular queue ADT (Abstract
Data type )
typedef struct pqueue
{
int data[size];
int F, R ;
} pqueue;

size is a constant ,maximum number of elements that are stored.


One dimensional array can be used to hold elements of Queue.
F and R indicate that F is Front and R is Rear.
Operations on priority queue
1)Initialize ():make the queue is empty.
2)empty():Determine if the queue is empty.
3)full():Determine if the queue is full.
4)enqueue():insert an element as per its priority.
5)dequeue():delete the front element
6)print():print element of the queue.

Application of Queue
Q.State any two application of queue?
Q.Give applications of queue?
Q.State any two applications of queue?
1)Josephus Problem
2)Job Scheduling
3)Queue simulation

UNIT-III Page 9
Unit-III QUEUE Marks-20
Prof.Salunkhe A.A Sir SUB: DSU Branch: SYCO/IF (Diploma)
1)Josephus Problem
 This is an interesting programming problem known as Josephus problem.
 Suppose there are n student standing in queue.
 student are numbered from 1 to n in clockwise direction.
 We have to choose lucky Student number say m.
 They start counting in clockwise direction from the student designated at 1 .
 The counting proceeds until the mth student is identified .
 This student is then eliminated and the process continues. after few rounds of counting only one
student is left and that student is winner.
2)Job Scheduling
 Programs (Jobs) entering a computer for execution are scheduled using some strategies.
 The purpose is to improve:
 1)CPU utilization.
 2)Response time
 3)System utilization
 The program(job) entering the system are stored in the queue. These jobs can be selected for
execution as per some pre-conceived strategy
Round Robin Technique:

Suppose there are n jobs waiting for execution .


In Round Robin Technique ,CPU given a fixed quantum of time to each job.
-Job is selected for from the queue of jobs.
It is executed for a fixed quantum time.
If job is not completed by this time I is removed and pushed back to queue of jobs.
3)Queue simulation
Computer can be used to simulate the behavior of the queue. Bank can utilize this information to
determine how many cash counter are needed to ensure reasonable service time.
A typical queue simulation involve processing of events
1)Customer arriving
2)Customer Departure.
On arrival of customer join the queue and on departure leaves the queue.
We can use probabilistic function to generate stream of ordered pair
1)Arrival time
2)Service time

UNIT-III Page 10

You might also like