C source code for implementation of Queue using Linked List
/* Queue using Linked List */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
typedef struct node
{
int data;
struct node *next;
}node;
typedef struct Q
{
node *R,*F;
}Q;
void initialize(Q*);
int full(Q*);
int empty(Q*);
void addqueue(Q*,int);
int delqueue(Q*);
void print(Q*);
void main()
{
Q q;
int x,i,op;
initialize(&q);
clrscr();
do
{
printf("\n1.insert\n2.delete\n3.print\n4.quit");
printf("\nenter your choice");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nenter a value");
scanf("%d",&x);
addqueue(&q,x);
break;
case 2:
if(!empty(&q))
{
x=delqueue(&q);
printf("\ndeleted data=%d",x);
}
else printf("\nqueue is empty");
break;
case 3:
print(&q);
break;
}
}while(op!=4);
}
void initialize(Q *qP)
{
qP->R=NULL;
qP->F=NULL;
}
void addqueue(Q *qP, int x)
{
node *P;
P=(node*)malloc(sizeof(node));
P->data=x;
P->next=NULL;
if(empty(qP))
{
qP->R=P;
qP->F=P;
}
else
{
(qP->R)->next=P;
qP->R=P;
}
}
int delqueue(Q *qP)
{
int x;
node *P;
P=qP->F;
x=P->data;
if(qP->F==qP->R) //deleting the last element
initialize(qP);
else
qP->F=P->next;
free(P);
return(x);
}
void print(Q *qP)
{
int i;
node *P;
P=qP->F;
while(P!=NULL)
{
printf("\n%d",P->data);
P=P->next;
}
}
int empty(Q *qP)
{
if(qP->R==NULL)
return 1;
return 0;
}
0 comments:
Post a Comment