Exp 4
Exp 4
THEORY:
In queue, insertion and deletion happen at the opposite ends, so implementation is not as
simple as stack.
To implement a queue using array, create an array arr of size n and take two
variables front and rear both of which will be initialized to 0 which means the queue is
currently empty. Element rear is the index up to which the elements are stored in the array
and front is the index of the first element of the array. Now, some of the implementation of
queue operations are as follows:
2. Dequeue: Removal of an element from the queue. An element can only be deleted
when there is at least an element to delete i.e., rear > 0. Now, element
at arr[front] can be deleted but all the remaining elements must shift to the left by one
position for the dequeue operation to delete the second element from the left on
another dequeue operation.
3. Front: Get the front element from the queue i.e., arr[front] if queue is not empty.
4. Display: Print all element of the queue. If the queue is non-empty, traverse and print
all the elements from index front to rear.
Algorithm:
enQueue(value) - Inserting value into the queue
In a queue data structure, enQueue () is a function used to insert a new element into the
queue. In a queue, the new element is always inserted at rear position. The enQueue ()
function takes one integer value as a parameter and inserts that value into the queue. We can
use the following steps to insert an element into the queue-
Program Code:
#include<stdio.h>
#include<conio.h>
struct queue
{
int a[5];
int front;
int rear;
};
int main()
{
struct queue s;
int c,v;
s.front=s.rear=-1;
do
{
printf("\n\n-------Menu-------\n1.Insert\n2.Delete\n3.Exit\nPlease select an operation :");
scanf("%d",&c);
switch(c)
{
case 1:
if(full(&s))
printf("Queue is Full.");
else
{
printf("Enter an element to be inserted :");
scanf("%d",&v);
insert(&s,v);
}
break;
case 2:
if(empty(&s))
printf("Queue is Empty.");
else
{
v=del(&s);
printf("Element Deleted : %d",v);
}
break;
default :
printf("Please select a proper Operation:");
break;
}while(c!=3);
getch();
return 0;
}
Output :