forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LINKED_QUEUE.c
75 lines (61 loc) · 1.27 KB
/
LINKED_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
#include <stdio.h>
#include <stdlib.h>
// LINKED_QUEUE
typedef struct node{
int inf;
struct node *next;
}NODE;
typedef struct{
NODE *begin;
NODE *end;
int size;
}DESCRITOR;
typedef DESCRITOR * LINKED_QUEUE;
void make_queue(LINKED_QUEUE *q){
*q = (DESCRITOR * )malloc(sizeof(DESCRITOR));
if(!(*q)){
puts("The memory is full\n");
exit(1);
}
(*q)->begin = (*q)->end = NULL;
(*q)->size = 0;
}
int isEmpty(LINKED_QUEUE q){
return (q->begin == NULL); //return (q->size == 0);
}
int sizeQUEUE (LINKED_QUEUE q){
return (q->size);
}
void insert_QUEUE (LINKED_QUEUE q, int info){
NODE *new_node;
new_node = (NODE *)malloc(sizeof(NODE));
if(!new_node){
puts("The memory is full\n");
exit(2);
}
new_node->inf = info;
new_node->next = NULL;
if(isEmpty(q))
q->begin = new_node;
else q->end->next = new_node;
q->end = new_node;
q->size++;
}
int query(LINKED_QUEUE q){
if(isEmpty(q)){
puts("QUEUE is Empty!\n");
exit(3);
}else return q->begin->inf;
}
void removeQUEUE(LINKED_QUEUE q){
NODE *aux = q->begin;
if(isEmpty(q)){
puts("QUEUE is Empty!\n");
exit(4);
}
q->begin = q->begin->next;
if(!(q->begin->next))
q->end = NULL;
free(aux);
}
int main(){}