#include <stdio.h>
#include <stdlib.h>
// to create a node for list
struct node
{
int data;
struct node *next;
};
// creating front and rear pointers, and assigning null values to them.
struct node*front=NULL, *rear=NULL;
// To enqueue
void enqueue(int val)
{
//allocating a dynamic memory to the new node
struct node*new=malloc(sizeof(struct node));
new->data=val;
new->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=new;
}
else
{
rear->next=new;
rear=new;
}
printf("The element %d has been added to the queue successfully!\n", val);
}
void dequeue()
{
struct node*temp;
if(front==NULL)
printf("The queue is empty!\n");
else
printf("The element %d has been removed from the queue successfully!\n", front->data);
temp=front;
front=front->next;
if(front==NULL)
rear=NULL;
free(temp);
}
void show()
{
struct node*temp=front;
while(temp!=NULL)
{
printf("%d ", temp->data);
temp=temp->next;
}
}
void main()
{
int a,n;
do
{
printf("\nEnter 1 to enqueue, 2 to dequeue, 3 for display and 4 for exit\n");
scanf("%d", &a);
switch(a)
{
case 1 : printf("Enter element to be added\n");
scanf("%d", &n);
enqueue(n);
break;
case 2 : dequeue();
break;
case 3 : show();
break;
case 4 : printf("Program terminated successfully!\n");
break;
default : printf("Enter valid number!\n");
break;
}
}
while(a!=4);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: