Experiment 3 DS
Experiment 3 DS
Experiment 3 DS
LAB Manual
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment No.04
A.1 Aim:
To study and implement concept of queue data structure.
A.2 Prerequisite:
1. Knowledge of different operations performed on Queue data structure
A.3 Outcome:
After successful completion of this experiment students will be able to
A.4 Theory:
A.4.1. Introduction of Queue
A.5 Procedure/Algorithm:
A.5.1:
Queue is a linear data structure in which items are inserted at one end called ‘Rear’ and deleted
from the other end called ‘Front’. Queues are based on the First-In-First-Out (FIFO) principle
that means the data item that is inserted first in the queue is also the first one to be removed
from the queue.
A queue or FIFO (first in, first out) is an abstract data type that serves as a collection of
elements, with two principal operations: enqueue, the process of adding an element to the
collection.(The element is added from the rear side) and dequeue, the process of removing the
first element that was added. (The element is removed from the front side). It can be
implemented by using both array and linked list.
Representation of Queue:
Conditions to be assumed:
Operations on Queue:
Qinsert It inserts a new data item at the rear of the queue Queue must not
be full
Qdelete It deletes and then return the data item at the front of the Queue must not
queue. be empty
TASK 1:
Stack to Queue:
Write an algorithm\Program called stackToQueue that creates a queue from a stack. After the
queue has been created, the top of the stack should be the front of the queue and the base of the
stack should be the rear of the queue. At the end of the algorithm, the stack should be empty
PART B
(PART B : TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the
concerned lab in charge faculties at the end of the practical in case the there is no Black
board access available)
Task1:
#include<stdio.h>
void main()
int choice,size,q[50],item,front=-1,rear=-1,i,c=1;
scanf("%d",&size);
q[size];
while(c!=0)
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&item);
if(rear==size-1)
printf("overflow");
else
{ if(front==-1)
{front=0;
rear=rear+1;
q[rear]=item;
break;
}
case 2:
if(front==-1)
printf("underflow");
else
front=front+1;
break;
case 3:
printf("\nqueue is empty");
else{
printf("QUEUE IS \n");
for(i=front;i<=rear;i++)
printf("%d\n",q[i]);
}
}break;
case 4: c=0;
Circular queue-
#include<stdio.h>
void main()
int choice,size,q[50],item,front=-1,rear=-1,i,c=1;
scanf("%d",&size);
q[size];
while(c!=0)
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("overflow");
else if(front==-1)
{front=0;
rear=0;
scanf("%d",&item);
q[rear]=item;
{rear=0;
scanf("%d",&item);
q[rear=item];
else
rear=rear+1;
scanf("%d",&item);
q[rear]=item;
break;
case 2:
if(front==-1)
printf("underflow");
if(front==rear)
front=-1;
rear=-1;
else if(front==size-1)
front=0;
else
{
printf("deletion from front");
front=front+1;
break;
case 3:
if(front==-1 )
printf("\nqueue is empty");
if(rear>=front)
printf("QUEUE IS \n");
for(i=front;i<=rear;i++)
printf("%d\n",q[i]);
}break;
case 4: c=0;
default: printf("ïnvalid choice");
Task2:
Task1:
Task2:
B.4 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed above and
learning/observation noted in section B.3)
1. What would be the contents of queue Q1 after the following code is executed
and the following data are entered?
1. Q1 = createQueue
2. S1 = createStack
3. Loop(not end of file)
a. Read number
b. If(number not 0)
i. Pushstack(S1, number)
c. Else
i. Popstack(S1,x)
ii. Popstack(S1,x)
iii. Loop (not empty S1)
1. Popstack(S1, x)
2. Enqueue(Q1, x)
iv. End loop
d. End if
4. End loop
The data are 5, 7, 12, 4, 0, 4, 6, 8, 67, 34, 23, 5, 0, 44, 33, 22, 6, 0
************************