-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.h
64 lines (52 loc) · 929 Bytes
/
Queue.h
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
#ifndef QUEUE_H_
#define QUEUE_H_
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
Item2 value;
struct Node* next;
}Node;
typedef struct Queue{
Node* front;
Node* rear;
}Queue;
Queue* createQueue(){
Queue* q = (Queue *) malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
return q;
}
int isQueueEmpty(Queue *q){
return (q->front == NULL);
}
void enqueue(Queue *q, Item2 x){
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->value = x;
if(isQueueEmpty(q)){
q->front = newNode;
q->rear = newNode;
}else{
q->rear->next = newNode;
q->rear = newNode;
}
}
void dequeue(Queue *q){
if(isQueueEmpty(q))
return;
Node *del = q->front;
if( q->front == q->rear )
q->front = q->rear = NULL;
else
q->front = q->front->next;
free(del);
}
Item2 front(Queue *q){
return q->front->value;
}
void destroyQueue(Queue *q){
while(!isQueueEmpty(q)){
dequeue(q);
}
free(q);
}
#endif