Experiment - 6
Experiment - 6
1. Display:
➢ Display function:
▪ Write a function named display which is used to display
the queue elements. It will be accessed by using for loop,
iterating from 0 to he maximum size of the queue.
void display();
2. Enqueue:
➢ Enqueue function:
▪ Write a function named Enqueue which is used to
push/add an element into the queue by taking an input of
variable. Here we would firstly check whether the queue
is full or not. If the queue is not full then we would
increment the rear variable and push the variable into the
queue.
void enqueue(int value);
3. Dequeue:
➢ Dequeue function:
▪ Write a function named Dequeue which is used to
pop/remove an element from the queue. Here we would
firstly check whether the queue is empty or not. If the
queue is not empty then we would increment the front
variable and dequeue the variable from the queue.
void dequeue();
4. isFull:
➢ isFull function:
▪ Write a function named isFull which is used to check that
weather the queue is full or not.
int isFull();
5. isEmpty:
➢ isEmpty function:
▪ Write a function named isEmpty which is used to check
that weather the queue is empty or not.
int isEmpty();
❖ Main Function:
• Call the Enqueue function with entering a value.
• Call the Dequeue function.
• Call the Display function.
• By this check various queue operation.
❖ CODE
struct node
{
int data;
struct node * next;
};
int dequeue()
{
if (front == NULL)
{
printf("\nUnderflow\n");
return -1;
}
else
{
struct node * temp = front;
int temp_data = front - > data;
front = front - > next;
free(temp);
return temp_data;
}
}
void display()
{
struct node * temp;
if ((front == NULL) && (rear == NULL))
{
printf("\nQueue is Empty\n");
}
else
{
printf("The queue is \n");
temp = front;
while (temp)
{
printf("%d--->", temp - > data);
temp = temp - > next;
}
printf("NULL\n\n");
}
}
int main()
{
int choice, value;
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4)
{
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", & choice);
switch (choice)
{
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", & value);
enqueue(value);
break;
case 2:
printf("Popped element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
return 0;
}
OUTPUT