-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1_queue.c
executable file
·77 lines (65 loc) · 1.13 KB
/
1_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
typedef struct s_node {
void *content;
struct s_node *next;
} Node;
typedef struct s_queue {
struct s_node *first;
struct s_node *last;
} Queue;
#include <stdlib.h>
Node *newNode(void *item)
{
Node *this = malloc(sizeof(Node));
this->content = item;
this->next = 0;
return this;
}
Queue *init(void)
{
Queue *new = malloc(sizeof(Queue));
new->first = 0;
new->last = 0;
return new;
}
void enqueue(Queue *q, void *item){
if (q->last)
{
(q->last)->next = newNode(item);
if (q->first == q->last)
q->first->next = q->last->next;
(q->last) = (q->last)->next;
}
else{
q->last = newNode(item);
q->first = q->last;
}
}
void *dequeue(Queue *queue)
{
if (queue->first == 0)
return NULL;
else
{
void * temp;
temp = queue->first->content;
if (queue->last == queue->first)
queue->last = queue->first->next;
queue->first = queue->first->next;
return temp;
}
}
void *peek(Queue *queue)
{
if (queue->first == 0)
return NULL;
else
{
void * temp;
temp = queue->first->content;
return temp;
}
}
int isEmpty(struct s_queue *queue)
{
return (queue->first == 0);
}