C program to implement queue using doubly linked list
#include<stdio.h> #include<conio.h> #define MAXSIZE 50; #define TRUE 1 #define FALSE 0 struct Node { int iData; struct Node *ptrNext; }; typedef struct Node DLLNode; DLLNode *front=NuLL, *rear=NULL; void fnQInsertion(int iData) { DLLNode *ptrNewNode; ptrNewNode = (DLLNode*) malloc(sizeof(DLLNode)); ptrNewNode->iData=iData; ptrNewNode->ptrNext=NULL; if(fnQEmpty()==TRUE) front=rear=ptrNewNode; else { rear->ptrNext=ptrNewNode; rear=ptrNewNode; } } // End of Algorithm
int fnQDelete() { int iData; DLLNode *ptrDeleteNode; if(fnQEmpty()== TRUE) { printf("\nQueue is Empty\n"); return 0; } else { iData=front->iData; ptrDeleteNote=front; front=front->ptrNext; free(ptrDeleteNode); return(iData); } } // End of Algorithm
void fnQDisplay() { DLLNode *ptrDisplayNode; if(fnQEmpty()==TRUE) printf("\nNothing to Display\n"); else { ptrDisplayNode=front; while(ptrDisplayNode!=NULL) { printf("%d\n", ptrDisplayNode->iData); ptrDisplayNode=ptrDisplayNode->ptrNext; } } } // End of Algorithm int fnQEmpty() { if(front==NULL || rear==NULL) return TRUE; else return FALSE; }
void main() { int iChoice, iData; clrscr(); do { printf("1. Insertion\n"); printf("2. Deletion\n"); printf("3. Display\n"); printf("4. Exit\n"); printf("Enter your choice\n"); scanf("%d", &iChoice); switch(iChoice) { case 1: printf("Enter the element"); scanf("%d", &iData); fnQInsertion(iData); break; case 2: if(fnQEmpty()==TRUE) printf("Queue is empty\n"); else printf("Item is deleted =%d\n", fnQDelete()); break; case 3: printf("\n**** Queue Content****\n"); fnQDisplay(); printf("\n***********************\n"); break; case 4: exit(1); default: printf("\nWrong Choice\n"); } } while(1); }