You are on page 1of 5

Fundamental of Data Structures 6-2

Unie VI
wwwwwevww

2.
The insertion and deletion Oueue
eler ents in the stack is of the The insertion
of the
end, called top.done| the queue is done element in
www.maMA oe

from only one


called rear and by the end
the element the deletion
from the of
Queue
6 6.2:Queue as Abstract Data Type
done by the end queue is
called front.

6.1: Basic Concept Give an ADT for Queue


a.3
The ADT for queue is as
Q.1 What is queue ? DSPPU: June-22, Marks 3 Ans. :
given below.
defined as AbstractData1ype Queue
The queue can formaly be ordered
Ans.: Definition
that has two ends named as front and rear. From
collection of elements
fYrom the rear end one can
the front end one can delete the elements and Instancess:

insert the elements. is finite


Que[Ma a ollection of elements in which
end and deletion of element is insertion of
For example: who are waiting for a city
element is by rear
by front end.
The typical example can be a queue of people
at one end of the queue, Preconditlon
bus at the bus stop. Any new person is joining shou
bus arrives the person at thee The front and rear be within the maximum
end. When the size MAX.
you can call it as the rear
call it as the front end of the insertion
other end first enters in the bus. You can Refore operation, whether the queue is full or not is
queue. checked.
few elements.
Following Fig. Q.1.1 represents the queue of Before any deletion operation, whether the queue is empty or not is
checked.
94 10 20 11 55 72 61

Front Rear Operations


1. Create)- The queue is created by declaring the data structure
Fig. Q.1.1 Queue
for it.
a.2 Compare Stack and Queue. 2. insert)- The element can be inserted in the queue by rear end.
Ans. 3. delet() The element at front end deleted each time.

Sr. No. Stack Queue 4.Display0 The elements of queue are displayed from front to
rear.

1. The stack is a LIFO data| The queue is a FIFO data


That means the
structure. That is the element|structure.
which is inserted last will be element which is inserted first
removed first from the stack. will be removed first

(6-1) Students
QIcODI A Gulde for Engineerlng
Fundamenials of Data Structures
6-3 6-4
Fundamentals of Data
Structures
Queue i n t M y O : : d e l e t ( )
6.3:Queue Operatlons Ouewe
Q4 Explain
the insertion
of element in queue
mplemented using
implementer
item
int item;
Q.que[0.front:
w
arrays. eue will
will alwave
of any
element in the queue always take place Q . f r o n t + + ;
Ans. The insertion out<<"\n The deleted item is
from the rear end.
retum Q . f o n t ;
<<itemi em
The deleted
We have Finaly oouraMem is 10
Ofroas
queue
40 50 inserted first 10,
10 20 30 then 20, then
then 40, then 30,
From this front
rear From this
end you can
in the queue. 50, 64: Circular Queue
end you can insert
delete the elemeent
Q.6
Explain the concept of circular quene,
the element
the insertion SPPU: June-22, Marks 3
Fig. Q.4.1 Representing Ans.: Circular queue a
in which read and front 3
int MyQ:insert(int itemn) queue
adjacent to each other. It can 40 front
are
follows. as
if(a.front ==-1)
This condition will occur initially berepresented 50/
when queue is empty For rear and front pointers 30 2
a.front++ following formula is used
a.quel++Q.rear] = item; rear
retun Q.rear; Always increment the rear pointer and rear= (rear + 1)%SIZE
place the element in the queue. front (front + 1)%SIZE
where SIZE represents the SIZE Fig. Q.6.1 Circular queue
of a queue
Q.5 Explain the deletion of element from queue implemented using
arrays. Q.7 Implement insert and delete operations of circular queue.
Ans.: The deletion of any element in the queue takes place by thefront OR Write Pseudo C+t code to implement circular queue using
end always. arrays. S SPPU : June-22, Marks 9]
50 60The number 10 gets deleted logically Ans.
10 20 30 40
That means queue is from front to rear only.
front rear 1.e. from 20 to 60
This much queue
insert function
will be considered ososeee
i.e. queue is
from front to rear.
Fig. Q.5.1 Representing the deletion void Queue:insert(int item)
QicoDD A Guide for Engineering Students
QIcOD A Guidefor Engineering Students
6-5 s of Data Stru
of Data Structures
Structures
Fundamentals of Data
Fundamentals
Owewe 6-6
i f ( f r o n t = = ( r e a r + 1 ) % M A X )

6.5 Multl-queues Qweue


short note
Write a on
cout < <
"Queue is full\n"; a Multiple
-
Ans.
of the
:.One of the
can be used
multiple queues can
application of quenes.
queues is
else
ltiple queues using
to store
variety
variety
single dimensional
of data. of data.
We can
categorization
And
I/setting front pointer
for a single element in Queue
In a
mal array,
one dimensional arays. implement
iffront=-1)
from its
rear end and multiple
deletion from queues can be
front rear=0; desired queue. Refer Fig. Q.8.1 its frot end can placed. Insertion
else
be possible for.
rear=(rear+1)%MAX;
front 1 rear front 2 rear 3
rear 2 front 3
Querear=item; front 4 rear 4
10 20 30 40 50 60 70 80
* 90 100 101 102
delet function 0 1 2 3
10 11 12 13 14 15
queue 1 of size 5 queue 2 of size 4
queue 3 queue 4 of size 5
of size 2
int Queue:delet() Q.8.1 Multiple queues
Fia.
using single array
There are four queues having their own front
int val; and rear positioned at
appropriate points in a single dimensional array.
if(front=-1)
.We perform insertion and deletion of any element for any queue.
can
We can declare the messages "queue full"
cout << "Queue is empty\n";
appropriate situations for that particular queue
and "queue empty"at
retun 0; // return null on empty Queue
val= Quelfront}://item to be deleted 6.6 Linked Queue and Operations
if(front==rear)//when single element is present
Q.9 Write a routine to insert an element in a linked queue.
front=rear=-1; SPPU June-22, Marks 9
}
else Ans.
front=(front+1)%MAX; void Lqueue:insert()
retun val;
char ch;
a temp:
QECODD Students
A Guidefor Engineering Students OEcOD A Guidefor Engineerlng
Structures
6-7 entals of Data Struc
Fundamentals ofData
undame
Queue 10
6-8
clrscr() for temp node
20
=new
Q://allocates memory 30
temp
temp->next=NULL; front
the element in the Queueln".
cout<<"n\n\n\tInsert fear
cin>>temp->data; wish to inser
nsert any element
if we
Now
o shift all the elements to the right. from front end
first node then first we
ifffont == NULL)//creating
want to insert 40
sue want have
example if
we
For by front end then, the deque will
front= temp; 10 20 30 be
40
50 40 10 20
rear=temp; 30
rear
l/attaching other nodes ront
front
else in the queue by
Inserted 40
front end Inserted 50 in the
queue by front
rear->next=temp;
rear=rear->next; (a) Insertion by front end
50 40
6.7:Dequeue rear
front front rear
Q.10 Explain the concept of Dequeue with example.
Ans.: Dequeue is a data structure in which we can insert the element (b) Deletion by rear end
both by front and rear end. Similarly we can delete the element from Fig. Q.10.2 Operations on deque
deque by both the rear and front ends. We can place -1 for the element which has to be deleted.
Insertion
Deletion by
by front rear end
end 6.8: Priority Queue
Deletion
by front Insertion by
end Q.11 Write a short note on priority queue.
front rear end
rear
SPPU : June-22, Marks 81
Fig. Q.10.1 Doubly ended queue Ans.: The elements in the priority queue have specific ordering. There
As we know, normally we insert the are two
types of priority queues
elements by rear end and delete the in which the
elements from front end. Let us say we have
inserted the elements 10, 20, .Ascending Priority Queue It is a collection of items
-
can be
element
30 by rear end. ems can be inserted arbitarily but only smallest
removed.
QIcODD A Guide for Engineering Students A Guide for Engineering
Students
ucODL
no
if

You might also like