Queue using Link List in C

#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 5 typedef struct node{ int data; struct node *next; }nptr; int count=0; nptr *front=NULL,*rear=NULL; void enqueue(int x){ nptr *nn; nn=(nptr *)malloc(sizeof(nptr)); nn->data=x; nn->next=NULL; if(front==NULL) front=rear=nn; else{ rear->next=nn; rear=nn; } count++; } int dequeue(){ int x; nptr *temp=front; x=front->data; if(front==rear) front=rear=NULL; else front=front->next; free(temp); count--; return x; } void display(){ nptr *temp=front; while(temp!=NULL){ printf(" %d ",temp->data); temp=temp->next; } } int main() { int ch,data; printf("\nQUEUE\n"); while(1){ printf("\nEnter Choice\n1 Enqueue \n2 Dequeue\n3 Display\n4 Exit\n\n"); ch=getch(); switch(ch){ case '1':if(count==MAX) printf("\nQueue Overflow\n"); else{ printf("\nEnter data\n"); scanf("%d",&data); enqueue(data); printf("\nInserted data: %d\n",data); } break; case '2':if(!count) printf("\nQueue Underflow\n"); else{ data=dequeue(); printf("\nDeleted data: %d\n",data); } break; case '3':display();break; case '4':exit(0);break; default: printf("\nInvalid Choice\n"); } } }
Queue using Link List in C

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.