Experiment No: 05
1.
Experiment vision:
Write a program to study and implement Queue.
2. OBJECTIVE: Program to implement an array of object
in C and to find the specific
element.
3. THEORY:
In
computer
science,
a queue is a particular kind of abstract
data type
or collection in which the
entities in the collection are kept in order and the principal (or only)
operations on the collection are the addition of entities to the rear terminal
position, known as enqueue, and removal of entities from the front
terminal position, known as dequeue. This makes the queue a First-In-First-Out
(FIFO) data structure.
In a FIFO data structure, the first element added to
the queue will be the first one to be removed. This is equivalent to the
requirement that once a new element is added, all elements that were added
before have to be removed before the new element can be removed. Often a peek or front operation is also
implemented, returning the value of the front element without dequeuing it. A
queue is an example of a linear
data structure,
or more abstractly a sequential collection.
4. PROGRAM:
#include<stdio.h>
#define MAX 10
#include<stdlib.h>
void insert(int
queue[],int *rear,int front,int value)
{
*rear=(*rear+1);
if(*rear==front)
{
printf("the queue is full can not
insert a value\n");
exit(0);
}
queue[*rear]=value;
}
void delete(int
queue[],int *front,int rear,int * value)
{
if(*front==rear)
{
printf("the queue is empty can not
delete a value\n");
exit(0);
}
*front=(*front+1);
*value=queue[*front];
}
void main()
{
int queue[MAX];
int front,rear;
int n,value;
front =0;
rear=0;
do
{
do
{
printf("enter the element to be inserted\n");
scanf("%d",&value);
insert(queue,&rear,front,value);
printf("enter 1 to continue\n");
scanf("%d",&n);
}while(n==1);
printf("enter 1 to delete an
element\n");
scanf("%d",&n);
while(n==1)
{
delete(queue,&front,rear,&value);
printf("the value deleted is %d\n",value);
printf("enter 1 to delete an element\n");
scanf("%d",&n);
}
printf("enter 1 to
continue\n");
scanf("%d",&n);
}while(n==1);
}
5. OUTPUT:------------------------------------------
------------------------------------------
6. conclusion:
Thus a
C program of queue has been executed successfully.
7. REMARK:
In
the queue, how the element added and remove. We understood all the procedure
through this program.
8. Discussion Questions:
1) what
is Queue?
_________________________________________________________________________________________________________________________________________________________
2)
How is queue working, explain with example? (diagrammatically)
________________________________________________________________________________________________________________________________________________________
3) What are the types of queue?
No comments:
Post a Comment