Experiment 3 DS

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

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering


Computer Engineering Department
Program: MBA Tech. Sem III

Course: Data Structure

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

2. Fundamental concepts of C\C++.

A.3 Outcome:
After successful completion of this experiment students will be able to

1. Identify the need of appropriate selection of data structure


2. Identify the steps of queue data structure selection.
3. Implement Queue data structure to solve the given problem
4. Enlist the applications of stack data structure.
5. Differentiate between different types of Queue.

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:

1. Static representation using arrays.


2. Dynamic representation using linked list.

Array representation of a queue needs two indices: FRONT and REAR.

Array declaration in C as follows: int queue[maxsize]

Conditions to be assumed:

1. FRONT =REAR or FRONT=REAR=-1, if the queue empty


2. Whenever an element is deleted from the queue, FRONT=FRONT+1
3. Whenever an element is inserted into the queue, REAR=REAR+1

Operations on Queue:

Operation Description Restriction

Create It creates a new empty queue. This operation must be -


Queue carried out in order to make the queue logically
accessible

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

Queue full It returns true if the queue is full. Otherwise it returns -


false.

Queue It returns true if the queue is empty. Otherwise it returns


Empty false.

TASK 1:

1.Write a program to operations on queue.

2.Write a program to implement a circular queue.


Task 2:

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)

Roll No. Name:


Class : Batch :
Date of Experiment: Date of Submission
Grade : Time of Submission:
Date of Grading:

B.1 Software Code written by student: (Task 1)


(Paste your Matlab code completed during the 2 hours of practical in the lab here)

Task1:
#include<stdio.h>

void main()

int choice,size,q[50],item,front=-1,rear=-1,i,c=1;

printf("enter size of queue");

scanf("%d",&size);

q[size];
while(c!=0)

printf("enter 1. for inserting\n 2. for deleting \n 3. for display\n 4. exit");

scanf("%d",&choice);

switch(choice)

case 1:

printf("ënter the item to be inserted");

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

printf("deletion from front");

front=front+1;

break;

case 3:

if(front==-1 || front >rear)

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;

default: printf("ïnvalid choice");

Circular queue-

#include<stdio.h>

void main()

int choice,size,q[50],item,front=-1,rear=-1,i,c=1;

printf("enter size of queue");

scanf("%d",&size);

q[size];

while(c!=0)

printf("enter 1. for inserting\n 2. for deleting \n 3. for display\n 4.


exit");

scanf("%d",&choice);

switch(choice)
{

case 1:

if((front==0 && rear==size-1)||(front==rear+1))

printf("overflow");

else if(front==-1)

{front=0;

rear=0;

printf("ënter the item to be inserted");

scanf("%d",&item);

q[rear]=item;

else if(rear==size-1 && front!=0)

{rear=0;

printf("ënter the item to be inserted");

scanf("%d",&item);

q[rear=item];

else

rear=rear+1;

printf("ënter the item to be inserted");

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:

B.2 Input and Output: (Task 1)


(Paste your program input and output in following format, If there is error then paste the specific
error in the output part. In case of error with due permission of the faculty extension can be given to
submit the error free code with output in due course of time. Students will be graded accordingly.)

Task1:

Task2:

B.3 Observations and learning [w.r.t. all tasks]:


(Students are expected to comment on the output obtained with clear observations and learning for
each task/ sub part assigned)

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)

B.5 Question of Curiosity


(To be answered by student based on the practical performed and learning/observations)

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

************************

You might also like