-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueue.c
89 lines (71 loc) · 1.6 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
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
typedef struct node{
struct node* ant;
struct node* prox;
QueueInfo info;
}NodeQueue;
typedef struct{
NodeQueue* primeiro;
NodeQueue* ultimo;
int size;
int isEmpty;
}QueueStruct;
Queue createQueue(){
QueueStruct* queue = (QueueStruct*)malloc(sizeof(QueueStruct));
queue->primeiro = NULL;
queue->ultimo = NULL;
queue->size = 0;
queue->isEmpty = 1;
return queue;
}
int isQueueEmpty(Queue queue){
QueueStruct* q = (QueueStruct*)queue;
return q->isEmpty;
}
int getQueueSize(Queue queue){
QueueStruct* q = (QueueStruct*)queue;
return q->size;
}
void enqueue(Queue queue, QueueInfo info){
QueueStruct* q = (QueueStruct*)queue;
NodeQueue* nod = (NodeQueue*)malloc(sizeof(NodeQueue));
nod->prox = NULL;
nod->info = info;
if(q->isEmpty == 1){
nod->ant = NULL;
q->primeiro = nod;
q->ultimo = nod;
q->isEmpty = 0;
q->size++;
return;
}
q->ultimo->prox = nod;
nod->ant = q->ultimo;
q->ultimo = nod;
q->size++;
}
QueueInfo dequeue(Queue queue){
QueueStruct* q = (QueueStruct*)queue;
NodeQueue* aux = q->primeiro;
if(q->isEmpty == 1){
return NULL;
}
if(q->size > 1){
q->primeiro = q->primeiro->prox;
}
QueueInfo info = aux->info;
q->size--;
free(aux);
if(q->size == 0){
q->primeiro = NULL;
q->ultimo = NULL;
q->isEmpty = 1;
}
return info;
}
void deleteQueue(Queue queue){
QueueStruct* q = (QueueStruct*)queue;
free(q);
}