You are on page 1of 3

Queue using linked list

#include<stdio.h> #include<conio.h> struct node { int info; struct node *link; }; struct node *start,*rear,*newptr; struct node *create_new_node(int i); void enqueue(struct node *n); int dequeue(); void display(struct node *x); int count(struct node *c); void main() { int info,a,c,x; struct node *np; char ch; start=NULL; rear=NULL; clrscr(); do { printf("ENTER YOUR CHOICE:\n"); printf("Enter 1 to insert\n"); printf("Enter 2 to delete\n"); printf("Enter 3 to print data at front\n"); printf("Enter 4 to print data at end\n"); printf("Enter 5 to display the queue\n"); printf("Enter 6 to print number of elements\n"); scanf("%d",&a); if(a==1) { printf("Enter the element\n"); scanf("%d",&info); np=create_new_node(info); enqueue(np); } else if(a==2) { x=dequeue(); printf("\nDequeue successfull,Element deleted is %d\n",x); } else if(a==3) { printf("Element at front is %d\n",start->info); } else if(a==4) { printf("Elemet at rear is %d\n",rear->info); }

} while(ch=='y'); getch();

else if(a==5) { if(start==NULL) { printf("Queue is empty\n"); } else display(start); } else if(a==6) { c=count(start); printf("The number of nodes are %d\n",c); } printf("\nDo you want to proceed? y/n\n"); scanf(" %c",&ch); clrscr();

struct node *create_new_node(int i) { newptr=(struct node *)malloc(sizeof(struct node)); newptr->info=i; newptr->link=NULL; return newptr; } void enqueue(struct node *n) { struct node *temp; if(start==NULL) { start=n; rear=n; n->link=NULL; } else { rear->link=n; rear=n; n->link=NULL; } } int dequeue() { int x; struct node *temp; temp=start; x=(temp->info); start=start->link; free(temp); return x; }

void display(struct node *x) { printf("\nCurrent queue is:\t"); do { printf("%d\t",x->info); x=x->link; } while(x!=NULL); printf("\n"); } int count(struct node *c) { int co=0; do { co++; c=c->link; } while(c!=NULL); return co; }

Output

You might also like