Queue Implementation Using Linked List
Queue implementation using linked list gives following advantages:
- Linked list allow dynamic allocation of memory
- Insertion and deletion is easy
- Efficient memory utilization due to run time allocation
- Deletion of elements free the memory, that can further utilized by other programs
Implementation of Queue
- First step to define the structure of the node
- Define two pointer called rear and front assigned with NULL
- Create the List of Operation in Main function
- Decide the condition for Queue empty or full
Last inserted elements always pointed by rear |
#include
#include
struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void Inqueue()
{
int n;
struct node *newNode=(struct node*)malloc(sizeof(struct node));
printf("Insert the data");
scanf("%d",&n);
newNode->data=n;
if(rear==NULL)
{
front=newNode;
rear=newNode;
newNode->next=NULL;
}
else
{
rear->next=newNode;
rear=newNode;
}
}
void Dequeue()
{
if(front==NULL)
printf("Queue is empty");
else
{
struct node *temp;
temp=front;
front=front->next;
printf("Dequeue element is =%d",temp->data);
free(temp);
}
}
void Display()
{
if (front==NULL)
printf("Queue is empty");
else
{
struct node *temp;
temp=front;
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}
int main()
{
int choice;
while(1)
{
printf("------Queue_l-------\n");
printf("1:Insert the element\n");
printf("2:Delete the element\n");
printf("3:Display\n");
printf("4:Exit\n");
printf("----------------------------\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
Inqueue();
break;
case 2:
Dequeue();
break;
case 3:
Display();
break;
case 4:
exit(0);
break;
default :
printf("Invalid choice");
}
}
}
This code you can get it on below link..
https://drive.google.com/open?id=0B2O8tJ9QXc-3NG1wRlJLUVFfcGM
#include
struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void Inqueue()
{
int n;
struct node *newNode=(struct node*)malloc(sizeof(struct node));
printf("Insert the data");
scanf("%d",&n);
newNode->data=n;
if(rear==NULL)
{
front=newNode;
rear=newNode;
newNode->next=NULL;
}
else
{
rear->next=newNode;
rear=newNode;
}
}
void Dequeue()
{
if(front==NULL)
printf("Queue is empty");
else
{
struct node *temp;
temp=front;
front=front->next;
printf("Dequeue element is =%d",temp->data);
free(temp);
}
}
void Display()
{
if (front==NULL)
printf("Queue is empty");
else
{
struct node *temp;
temp=front;
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}
int main()
{
int choice;
while(1)
{
printf("------Queue_l-------\n");
printf("1:Insert the element\n");
printf("2:Delete the element\n");
printf("3:Display\n");
printf("4:Exit\n");
printf("----------------------------\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
Inqueue();
break;
case 2:
Dequeue();
break;
case 3:
Display();
break;
case 4:
exit(0);
break;
default :
printf("Invalid choice");
}
}
}
This code you can get it on below link..
https://drive.google.com/open?id=0B2O8tJ9QXc-3NG1wRlJLUVFfcGM
Comments
Post a Comment