Unit 5 DSU 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

https://shikshamentor.

com/data-structure-
using-c-for-msbte-3k-scheme/

313301 – Data Structure (Sem III)


As per MSBTE’s K Scheme
CO / CM / IF

Unit V Queue Marks – 10

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

Operations of a Priority Queue:


A typical priority queue supports the following operations:
1) Insertion in a Priority Queue
When a new element is inserted in a priority queue, it moves to the empty slot from top to bottom and
left to right. However, if the element is not in the correct place then it will be compared with the
parent node. If the element is not in the correct order, the elements are swapped. The swapping
process continues until all the elements are placed in the correct position.
2) Deletion in a Priority Queue
As you know that in a max heap, the maximum element is the root node. And it will remove the
element which has maximum priority first. Thus, you remove the root node from the queue. This
removal creates an empty slot, which will be further filled with new insertion. Then, it compares the
newly inserted element with all the elements inside the queue to maintain the heap invariant.

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)

Initial status of Queue

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

6 List any four types of queues. W -22 02


Following are the types of Queue:
1.Linear Queue
2. Circular Queue
3. Double Ended Queue
4. Priority Queue

7 Draw the diagram of circular queue with front and rear pointers. S -22 02

Ans

A. Circular Queue B. Full Circular Queue

8 Explain Queue overflow and underflow conditions with examples. S -22 04


Ans  Queue Overflow :
Queue is a data structure in which insertion take place at one end called rear and deletion take
place at other end called front.
When Queue is fully occupied and if insertion operation is performed then overflow take place.
 Queue Underflow :
When there is no element in the queue then deletion operation generates ‘underflow’ condition.

9 Define queue. State any two applications where queue is used. W - 19 02


Ans

10 Explain the concept of circular Queue along with its need. W - 19 04

Ans

11 Enlist queue operations condition. S – 19 02


1. Queue Full
Ans. 2. Queue Empty

12 Draw and explain construction of circular queue. S – 19 04

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

FRONT = Bb Cc Ddd Sss


2 b c

REAR = 5 1 2 3 4 5 . . . . N
.

Checking for an empty queue :


A queue will be empty when both, front and rear are at the same position. For
example, if the queue contains A, B and C, the position of the front and rear will be

–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);

14 Write a program in ‘C’ to insert an element in a linear queue. W – 18 04


#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;
};
void initqueue(struct q1 *pq)
{
pq->f = pq->r = -1;
Ans.
}
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();
}
Thank You
https://shikshamentor.com/data-structure-using-c-for-
msbte-3k-scheme/

Visit
https://shikshamentor.com/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy