diff options
Diffstat (limited to 'queue.c')
-rw-r--r-- | queue.c | 68 |
1 files changed, 68 insertions, 0 deletions
@@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +struct node { + int val; + struct node *next; +}; + +struct node *head; +struct node *tail; + +void +enqueue(int val) +{ + struct node *tmp = malloc(sizeof(struct node)); + tmp->val = val; + if (head == NULL) { + tail = head = tmp; + return; + } + tail->next = tmp; + tail = tmp; +} + +int +deque() +{ + if (head == NULL) { + return -1; + } + struct node *tmp = head; + int val = tmp->val; + head = head->next; + free(tmp); + return val; +} + +int +peek() +{ + return head == NULL ? -1 : head->val; +} + +void +print() +{ + struct node *cur = head; + while (cur != NULL) { + printf("%d ", cur->val); + cur = cur->next; + } + printf("\n"); +} + +int +main() +{ + int val, n = 0; + printf("How many integers?\n"); + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + printf("Enter an integer \n"); + scanf("%d", &val); + enqueue(val); + print(); + } + return 0; +} |