-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.c
83 lines (71 loc) · 1.18 KB
/
Queue.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
#include <stdio.h>
typedef struct node
{
int item;
struct node *next;
}node;
typedef struct queue
{
node *head;
node *tail;
}queue;
queue *create_queue()
{
queue *new_queue = (queue *) malloc(sizeof(queue));
new_queue->head = NULL;
new_queue->tail = NULL;
return new_queue;
}
int is_empty(queue *queue)
{
return(queue->head == NULL);
}
void free_queue(queue *queue)
{
node *current = queue->head;
while(current != NULL)
{
queue->head = queue->head->next;
free(current);
current = queue->head;
}
free(queue);
}
void enqueue(queue *queue, int item)
{
node *new_node = (node *) malloc(sizeof(node));
new_node->item = item;
new_node->next = NULL;
if(is_empty(queue))
{
queue->head = new_node;
queue->tail = new_node;
}
else
{
queue->tail->next = new_node;
queue->tail = new_node;
}
}
void dequeue(queue *queue)
{
node *current = queue->head;
queue->head = queue->head->next;
printf("%d\n", current->item);
free(current);
}
int main()
{
queue *queue = create_queue();
int item;
while(scanf("%d", &item) != EOF)
{
enqueue(queue, item);
}
while(!is_empty(queue))
{
dequeue(queue);
}
free_queue(queue);
return 0;
}