$ 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 int index_start = -1; int ref_start = 0; struct QNode { int index; int ref; 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(void) { struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode)); temp->index = index_start++; temp->ref = ref_start++; 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) { // Create a new LL node struct QNode *temp = newNode(); // 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) { if (!(*q)) (*q) = createQueue(); store_asm((*q)); return 0; } struct Queue * list = NULL; int main(void) { index_start = 0; ref_start = 1; queue_add(&list); queue_add(&list); queue_add(&list); struct QNode * p = list->front; while(p) { printf("%d -> %d\n", p->index, p->ref); p = p->next; } puts("NULL"); return 0; } $ gcc new.c -o new $ ./new 0 -> 1 1 -> 2 2 -> 3 NULL