-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue-with-linked-list.c
116 lines (92 loc) · 1.71 KB
/
queue-with-linked-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
#include "lib-linked-list.h"
int QUIT_CMD = 9;
void empty_msg()
{
printf(" Queue is empty.\n");
}
int value_from_stdin()
{
printf(" Enter your value: ");
int value;
scanf("%d", &value);
return value;
}
void print(Node *head)
{
if (head == NULL)
{
return empty_msg();
}
print_linked_list(head);
}
Node* enqueue(Node *tail)
{
Node *newnode = new_node();
newnode->data = value_from_stdin();
if (tail != NULL)
{
push_back(tail, newnode);
}
return newnode;
}
Node* dequeue(Node *head)
{
if (head == NULL)
{
empty_msg();
return NULL;
}
return pop_front(head);
}
void peek(Node *head)
{
if (head == NULL)
{
return empty_msg();
}
printf(" %d\n", head->data);
}
int main(void) {
int choice;
Node *head = NULL;
Node *tail = NULL;
do{
printf("0. Print\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Peek\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 0:
printf("Printing the queue...\n");
print(head);
break;
case 1:
printf("Adding to queue...\n");
Node* add_result = enqueue(tail);
tail = add_result;
printf("FINAL TAIL %p\n", tail);
if (head == NULL)
{
head = add_result;
}
break;
case 2:
printf("Removing from queue...\n");
Node *remove_result = dequeue(head);
head = remove_result;
if (head == NULL)
{
tail = NULL;
}
break;
case 3:
printf("Peeking...\n");
peek(head);
break;
}
} while (choice != QUIT_CMD);
};