The below is a simple program for the implementation of a Queue Data structure using Linked List. The program uses an user defined data structures namely “Node” which stores the value and the reference to the Next node. There are two basic functions defined in this Queue structure – Enqueue and Dequeue using Linked List. The advantage of using a Linked list over Array for Queue is that, we can store any number of elements in the Linked list, although the Array can also be made dynamic, it still has many disadvantages. The Function Init is called to Initialize the queue, without which we will can get segmentation fault during enqueue and dequeue( during memory accessing). You can download the C program using Linked List for Queue here – http://www.bytechip.com/wp-content/uploads/2010/09/queue.c.
Queue Program using Linked List in C
#include<malloc.h>
#include<stdio.h>
struct node{
int value;
struct node *next;
};
void Init(struct node *n){
n->next=NULL;
}
void Enqueue(struct node *root,int value){
struct node *j=(struct node*)malloc(sizeof(struct node));
j->value=value;
j->next=NULL;
struct node *temp ;
temp=root;
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=j;
printf(“Value Enqueued is : %dn”,value);
}
void Dequeue(struct node *root)
{
if(root->next==NULL)
{
printf(“No Element to Dequeuen”);
}
else
{
struct node *temp;
temp=root->next;
root->next=temp->next;
printf(“Value Dequeued is : %dn”,temp->value);
free(temp);
}
}
void main()
{
struct node sample_queue;
Init(&sample_queue);
Enqueue(&sample_queue,10);
Enqueue(&sample_queue,50);
Enqueue(&sample_queue,570);
Enqueue(&sample_queue,5710);
Dequeue(&sample_queue);
Dequeue(&sample_queue);
Dequeue(&sample_queue);
}
Download Program Here


For queue program to add element add at last by storing the node pointer in a tempeory variable called tail.. each time wen u enque.. the new element is tail.. so that we can avoid traversing entaire list… Good programming
Yeah had idea striked in mind only after posting this article, thanks for pointing it out, I’m changing it straight away.