#include #include #include typedef struct node_t { char *name; struct node_t *next; } node_t; /* The queue is initially empty. */ node_t *head = NULL; node_t *tail = NULL; void insert(const char *name); int main() { char name[256]; node_t *new; node_t *doomed; node_t *p; for (;;) { printf("Type a name to insert, delete to delete, quit to quit: "); scanf("%s", name); if (strcmp(name, "quit") == 0) { break; } if (strcmp(name, "delete") != 0) { /* Insert a new node at the tail of the queue. */ new = malloc(sizeof(node_t)); new->name = malloc(strlen(name) + 1); strcpy(new->name, name); if (tail == NULL) { /* Insert first node into empty queue. */ head = new; } else { tail->next = new; } tail = new; } else { /* Remove a node from the head of the queue. */ if (head == NULL) { fprintf(stderr, "The queue is already empty.\n"); continue; } doomed = head; printf("%s\n", doomed->name); if (head == tail) { /* The queue is now empty. */ head = tail = NULL; } else { head = doomed->next; } free(doomed->name); free(doomed); } } if (head == NULL) { printf("The queue is empty.\n"); } else { printf("Here are the people who are still on the queue:\n"); for (p = head;; p = p->next) { printf("%s\n", p->name); if (p == tail) { break; } } } return EXIT_SUCCESS; }