/*
Linked List Implementation of the Queue in C
Author: Kasun Ranga Wijeweera
Email: krw19870829@gmail.com
Date: 20130514
*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct queueNode *link;
struct queueNode
{
int data;
link next;
};
typedef struct
{
link front;
link rear;
}queue;
void initQueue(queue *q)
{
q->front=NULL;
q->rear=NULL;
}
int isEmpty(queue *q)
{
if((q->front)==NULL)
{
return 0;
}
else
{
return 1;
}
}
void putQueue(queue *q,int x)
{
link t=(link)malloc(sizeof(struct queueNode));
t->data=x;
t->next=NULL;
if(isEmpty(q))
{
q->rear->next=t;
q->rear=t;
}
else
{
q->front=t;
q->rear=t;
}
}
int getQueue(queue *q)
{
link t=q->front;
int x=t->data;
if((q->front)==(q->rear))
{
q->front=NULL;
q->rear=NULL;
}
else
{
q->front=t->next;
}
free(t);
return x;
}
void printQueue(queue *q)
{
link t=q->front;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->next;
}
}
void main()
{
queue *q;
int x;
clrscr();
q=(queue*)malloc(sizeof(queue));
initQueue(q);
putQueue(q,10);
putQueue(q,20);
putQueue(q,30);
putQueue(q,40);
putQueue(q,50);
printf("n");
printQueue(q);
if(isEmpty(q))
x=getQueue(q);
printf("n");
printQueue(q);
putQueue(q,60);
putQueue(q,70);
printf("n");
printQueue(q);
getch();
}

Linked List Implementation of Queue in C