Program to Operate Queue Which Is Implemented Using Linklist:
#include<stdio.h>
#include<stdlib.h> //for malloc function
struct node //node in a link list
{
int data;
struct node* next;
}*head;
void insert(int num) //for insertion
{ struct node *ptr=malloc(sizeof(struct node));
struct node *temp=malloc(sizeof(struct node));
temp->data=num;
temp->next=NULL;
if(head==NULL) //for first insertion
{
head=temp;
}
else
{
ptr=head;
while(ptr->next!=NULL) //for pointing to last node
{
ptr=ptr->next;
}
ptr->next=temp; //saving temp as last node
}}
void delete() // for deletion
{
if(head==NULL) //no node in queue
{
printf("Queue is Empty");
}
else{
head=head->next; }
}
void display() //for display
{
struct node * ptr=head;
if(head==NULL)
{
printf("\nQueue Is Empty\n");
return;
}
printf("\nElements Are:\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next; //for moving forward
}
}
main()
{ int choice,num;
head=NULL; //initially head is null
while(1) //for user interface
{ printf("\nOperations On Queue\n");
printf("Enter your choice\n");
printf("1. INSERT \n");
printf("2. DELETE \n");
printf("3. DISPLAY \n");
printf("4. EXIT \n");
scanf("%d",&choice);
switch(choice)
{
case 1: //If user wishes to insert element in the queue
printf("Enter number to be inserted in the queue: ");
scanf("%d",&num); //getting the element to be inserted from the user
insert(num); //calling insert function
break; //jumping out of switch statement
case 2://If user wishes to delete element from the queue
delete(); //calling delete function and saving the result in an int variable(num)
break;
case 3://If user wishes to display the queue
display(); //calling display function
break;
case 4: //If user wishes to exit(stop continuing the program)
exit(1); //end of while loop
default ://If user enters a number other than 1 to 4
printf("INVALID CHOICE \n");
}
}
}
Also see Program of Operation on Queue as Array