-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
55 lines (45 loc) · 980 Bytes
/
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
#include "queue.h"
//========================================
Cell *new_cell(){
Cell *tmp = (Cell*) malloc(sizeof(Cell));
tmp->next = NULL;
return tmp;
}
//========================================
void new_queue(Queue *q){
Cell *news = new_cell();
q->start = news;
q->end = news;
q->size = 0;
}
//========================================
void enqueue(Queue *q, int data){
Cell *news = new_cell();
news->data = data;
q->end->next = news;
q->end = news;
q->size++;
}
//========================================
void print_queue(Queue *q){
Cell *tmp = q->start->next;
while(tmp != NULL){
printf("\n%d",tmp->data);
tmp = tmp->next;
}
}
//========================================
int size_queue(Queue *q){
return q->size;
}
//========================================
int dequeue(Queue *q){
if(q->size == 0){
return -1;
}
Cell *tmp = q->start;
q->start = q->start->next;
free(tmp);
q->size--;
return q->start->data;
}