/*PROGRAM TO IMPLEMENT QUEUES USING SINGLY LINKED LIST*/ #include #include #include #include #include struct node { int info; struct node *link; }; typedef struct node *NODE; NODE getnode() { NODE x; x=(NODE)malloc(sizeof(struct node)); if(x==NULL) { printf("Out of Memory\n"); exit(0); } return x; } NODE insert_rear(int item,NODE first) { NODE temp,cur,prev; temp=getnode(); temp->info=item; temp->link=NULL; if(first==NULL) return temp; cur=first; while(cur->link!=NULL) { cur=cur->link; } cur->link=temp; return first; } NODE delete_front(NODE first) { NODE temp= first; if(first == NULL) { printf("\n Queue Empty\n "); getch(); return first; } printf(" %d item has been deleted\n",first->info); first = first->link; free(temp); return first; } void display(NODE first) { NODE temp = first; if(first == NULL) { printf("\n Queue Empty\n "); getch(); return; } printf("\nThe contents of queue is"); while(temp != NULL) { printf("\n %d ", temp->info); temp = temp->link; } } void main() { int choice,item,pos; NODE first=NULL; for(;;) { clrscr(); printf("\n1:INSERT\n2:DELETE\n3:DISPLAY\n4:QUIT\n"); printf("\nENTER YOUR CHOICE :"); scanf ("%d",&choice); switch(choice) { case 1: printf("Enter the element to be inserted\n"); scanf("%d",&item); first=insert_rear(item,first); break; case 2: first=delete_front(first); break; case 3: display(first); break; case 4: exit(0); default: printf("Invalid input\n"); } getch(); } }