summaryrefslogtreecommitdiff
path: root/queue.c
diff options
context:
space:
mode:
Diffstat (limited to 'queue.c')
-rw-r--r--queue.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/queue.c b/queue.c
new file mode 100644
index 0000000..e0923ec
--- /dev/null
+++ b/queue.c
@@ -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;
+}