$ cat new.c #include #include #include #include #define pp(x) printf("%s = %p\n", #x, x); #define pi(x) printf("%s = %d\n", #x, x); #define pc(x) printf("%s = %c\n", #x, x); // A C program to demonstrate linked list based implementation of queue // A linked list (LL) node to store a queue entry struct QNode { int data; struct QNode *next; }; // The queue, front stores the front node of LL and rear stores ths // last node of LL struct Queue { struct QNode *front, *rear; }; // A utility function to create a new linked list node. struct QNode* newNode(int d) { struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode)); temp->data = d; temp->next = NULL; return temp; } // A utility function to create an empty queue struct Queue *createQueue() { struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue)); q->front = q->rear = NULL; return q; } void store_asm(struct Queue *q, int data) { // Create a new LL node struct QNode *temp = newNode(data); // If queue is empty, then new node is front and rear both if (q->rear == NULL) { q->front = q->rear = temp; return; } // Add the new node at the end of queue and change rear q->rear->next = temp; q->rear = temp; } struct QNode * load_asm(struct Queue **q) { // If queue is empty, return NULL. if ((q) == NULL) return NULL; if ((*q) == NULL) return NULL; if ((*q)->front == NULL) return NULL; // Store previous front and move front one node ahead struct QNode *temp = (*q)->front; (*q)->front = (*q)->front->next; // If front becomes NULL, then change rear also as NULL if ((*q)->front == NULL) (*q)->rear = NULL; return temp; } int queue_add(struct Queue **q, int data) { if (!(*q)) (*q) = createQueue(); store_asm((*q), data); return 0; } struct Queue * list = NULL; int main(void) { queue_add(&list, 1); queue_add(&list, 2); queue_add(&list, 3); struct QNode * p = list->front; while(p) { printf("%d -> ", p->data); p = p->next; } puts("NULL"); return 0; } $ gcc new.c -o new $ ./new 1 -> 2 -> 3 -> NULL