-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
63 lines (54 loc) · 1.11 KB
/
Copy pathqueue.cpp
File metadata and controls
63 lines (54 loc) · 1.11 KB
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
#include <iostream>
#define E int
using namespace std;
typedef struct linked_list {
struct linked_list * next;
E element;
}Link ;
Link* creat_link(Link *nextval){
Link *n = new Link;
n->next = nextval;
return n;
}
Link* create_link(E it, Link* nextval){
Link *n = new Link;
n->element = it;
n->next = nextval;
return n;
}
typedef struct q{
Link *front;
Link *rear;
int size;
}queue;
queue* create_queue(){
queue *q = new queue;
q->front = q->rear = creat_link(NULL);
q->size = 0;
return q;
}
void enqueue(queue* q, E it){
q->rear->next = create_link(it, NULL);
q->rear = q->rear->next;
q->size++;
}
E dequeue(queue* q){
if(q->size == 0){
printf("Erro!");
}
else{
E it = q->front->next->element;
q->front->next = q->front->next->next;
if(q->front->next == NULL){
q->rear = q->front;
}
q->size--;
return it;
}
}
void clear(queue *q){
while (q->size>0){
dequeue(q);
}
delete(q);
}