Unit 5 DSU 1
Unit 5 DSU 1
Unit 5 DSU 1
com/data-structure-
using-c-for-msbte-3k-scheme/
S. N. Exam
MSBTE Board Asked Questions Marks
Year
S - 24
Implement a ‘C ’ program to insert element into the queue and delete the W – 23
1 04
element from the queue W - 22
W - 19
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
#define FULL MAX - 1
struct queue
{
int item[MAX];
int f;
int r;
};
Ans.
void initqueue(struct q1 *pq)
{
pq->f = pq->r = -1;
}
void addq( struct q1 *pq, int num)
{
if(!isfull(pq)) /*check if queue is not full then*/
{
pq->r++; /*first increments the rear and then*/
pq->item[pq->r] = num; /*add element*/
}
else
{
printf("\n\t\tQueue is FULL ! . . . . ");
}
}
int deleteq(struct q1 *pq)
{
int ele;
pq->f++;
ele = pq->item[pq->f];
return( ele);
}
int isempty( struct q1 *pq)
{
return(pq->f == pq->r);
}
int isfull( struct q1 *pq)
{
return(pq->r == FULL);
}
void main()
{
struct stack q1;
int n, ch;
clrscr();
do
{
clrscr();
printf("\n\t\t * * * * * Queue * * * *");
printf("\n\t\t 1. Insert an element .");
printf("\n\t\t 2. Delete an element . ");
printf("\n\t\t 3. Exit");
printf("\n\t\t * Enter your choice :: ");
scanf("%d",&ch);
switch(ch)
{
case 1: //add
printf("\n\t\tEnter eleemnt to be added :: ");
scanf("%d",&n);
addq(q1, n);
getch();
break;
case 2: //delete
if(iempty(q1) == 1)
printf("\n\t\tQueue is empty!. . . .");
else
printf("\n\t\tThe deleted element is %d", deleteq(&q1));
getch();
break;
}
}while(ch != 3);
getch();
}
S -24
2 With a neat sketch explain working of priority queue 06
W - 22
A priority queue is an abstract data type that stores elements with associated priorities and serves
them in order of priority.
A priority queue is a type of queue that arranges elements based on their priority values. Elements with
higher priority values are typically retrieved or removed before elements with lower priority values.
Each element has a priority value associated with it. When we add an item, it is inserted in a position
based on its priority value.
Properties of Priority Queue
A priority Queue is an extension of the queue with the following properties.
Every item has a priority associated with it.
An element with high priority is dequeued before an element with low priority.
If two elements have the same priority, they are served according to their order in the queue.
Ans
W -23
3 List any four applications of queue W -22 02
W -18
Aplications of Queue data structure :
Ans
Queues are used for many applications in computer system. Basically, queues are used in
computer system for scheduling of resources to various applications, like CPU, printer,
etc.
When the job is given to the printer, most probably multiple print jobs given to a printer
which are organized in the FIFO(First In First Out) manner in a print queue and then
given to the printer.
In batch programming system, multiple are jobs combined into a group or a batch. First
program is executed first, the second is executed next and so on in a sequential manner.
4
Draw the diagram of Linear Queue to represent front and rear pointers. S -23 02
A Queue Data Structure follows the principle of "First in, First out" (FIFO), where the first
element added to the queue is the first one to be removed.
Ans -
Show the effect of INSERT and DELETE operation onto the linear queue
of size 10. The linear queue sequentially contains 10,20,30,40, and 50 where
10 is front of the queue. Show diagrammatically the effect of –
I INSERT(75)
II INSERT(85)
5 III DELETE S -23 06
IV INSERT(60)
V DELETE
VI INSERT(90)
10 20 30 40 50
0 4 6 7 8 9
1 2 3 5
Front Rear
I INSERT(75)
10 20 30 40 50 75
Ans 0
1 2 3
4 5 6 7 8 9
Front Rear
II INSERT(85)
10 20 30 40 50 75 85
0 6 7 8 9
1 2 3 4 5
Front Rear
III DELETE
20 30 40 50 75 85
1 6 7 8 9
0 2 3 4 5
Front Rear
IV INSERT(60)
20 30 40 50 75 85 60
1 6 7 8 9
0 2 3 4 5
Front Rear
V DELETE
30 40 50 75 85 60
2 6 7 8 9
0 1 3 4 5
Front Rear
VI INSERT(90)
30 40 50 75 85 60 90
6 7 8 9
2
0 1 3 4 5 Rear
Front
7 Draw the diagram of circular queue with front and rear pointers. S -22 02
Ans
Ans
A Circular Queue is an extended version of a normal queue where the last element of the
queue is connected to the first element of the queue forming a circle.
Ans.
The operations are performed based on FIFO (First In First Out) principle. It is also
called ‘Ring Buffer’.
In a normal Queue, we can insert elements until queue becomes full. But once queue
becomes full, we cannot insert the next element even if there is a space in front of
queue.
Operations on Circular Queue:
Front: Get the front item from the queue.
Rear: Get the last item from the queue.
enQueue(value) This function is used to insert an element into the circular queue. In a
circular queue, the new element is always inserted at the rear position.
o Check whether the queue is full – [i.e., the rear end is in just before the front end in
a circular manner].
o If it is full then display Queue is full.
o If the queue is not full then, insert an element at the end of the queue.
deQueue() This function is used to delete an element from the circular queue. In a
circular queue, the element is always deleted from the front position.
o Check whether the queue is Empty.
o If it is empty then display Queue is empty.
o If the queue is not empty, then get the last element and remove it from the queue.
Describe queue full and queue empty operation conditions on linear queue
13 W – 18 04
with suitable diagrams.
Queue empty
Queues may be represented in computer in various ways by means of one-
way list or linear array.
Usually all the queues are maintained by a linear array QUEUE and two
Ans
pointers variables FRONT – containing the location of the first element of
the queue and REAR – containing the location the last element of the
queue.
The condition FRONT = NULL indicates that the queue is empty.
The element is added at the end of the queue. So whenever an element is
added to the queue, the REAR is incremented by one, which can be done
with the assignment operator.
REAR = REAR + 1
The element is deleted from the start of the queue. So whenever an
element is deleted from the queue, the FRONT is incremented by one,
which can be done with the assignment operator.
FRONT + FRONT + 1
As we insert elements into the queue, rear gets incremented. At some stage it
will reach the array limit i.e MAX – 1 after which no elements can be added. Thus,
the function can be written as,
int isfull (Q *pq)
{
return( pq - > rear = = FULL );
}
Calling a function :
isfull (&Q1);
QUEUE
FRONT = 1 aaa Bbb Ccc
REAR = 3 1 2 3 4 5 ..... N
(a)
QUEUE
FRONT = 2 Bb Cc
b c
REAR = 3 1 2 3 4 5 ..... N
(b)
QUEUE
FRONT = 2 Bb Cc Ddd
b c
REAR = 4 1 2 3 4 5 ..... N
(c)
QUEUE
REAR = 5 1 2 3 4 5 . . . . N
.
–1 0 1 2
A B C
Front Rear
After removing the element A, B and C front and rear will coincide. Thus,
the queue empty condition can be written as,
int isempty (Q *pq)
{
return(pq -> front = = pq - > rear );
}
Calling a function :
isempty (&Q1);
Visit
https://shikshamentor.com/