-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.cpp
145 lines (130 loc) · 2.44 KB
/
Queue.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Queue.h"
#include <malloc.h>
#include <stdio.h>
struct QueueRecord
{
int Capacity; //the size of array
int Front; //the first
int Rear; //the last
int Size; //the size of the queue now
ElementType* Array;
};
const static ElementType Error = 0;
bool IsEmpty(Queue Q)
{
return Q->Size == 0;
}
bool IsFull(Queue Q)
{
return Q->Capacity == Q->Size;
}
Queue CreateQueue(int MaxElements)
{
Queue Q = (Queue)malloc(sizeof(struct QueueRecord));
if (Q == NULL)
return Q;
else
{
ElementType* array = (ElementType*)malloc(sizeof(ElementType) * MaxElements);
Q->Array = array;
Q->Capacity = MaxElements;
MakeEmpty(Q);
}
return Q;
}
void DisposeQueue(Queue Q)
{
if(Q->Array)
free(Q->Array);
if(Q)
free(Q);
}
void Enqueue(ElementType X, Queue Q)
{
if (!IsFull(Q))
Q->Size++;
Q->Rear = (Q->Rear + 1) % Q->Capacity;
Q->Array[Q->Rear] = X;
}
void MakeEmpty(Queue Q)
{
Q->Front = 1;
Q->Rear = 0;
Q->Size = 0;
}
ElementType Front(Queue Q)
{
if (!IsEmpty(Q))
return Q->Array[Q->Front];
return Error;
}
ElementType FrontAndDequeue(Queue Q)
{
ElementType t = Front(Q);
Dequeue(Q);
return t;
}
void Dequeue(Queue Q)
{
if (!IsEmpty(Q))
{
Q->Front = (Q->Front + 1) % Q->Capacity;
Q->Size--;
}
}
/*int main()
{
printf("c.create a queue;\nd.delete a queue\nm.make queue\
empty\ne.enqueue an element\no.dequeue an element\nq.quit\n");
char action;
Queue q = NULL;
ElementType x;
scanf_s("%c", &action);
getchar();
while (action != 'q')
{
switch (action)
{
case 'c':
printf("please input size of array:\n");
int n;
scanf_s("%d", &n);
getchar();
q = CreateQueue(n);
printf("successfully create a queue of [%d] size\n", n);
break;
case 'd':
if(q)
DisposeQueue(q);
printf("delete the queue successfully!\n");
break;
case 'm':
MakeEmpty(q);
printf("make empty successfully!\n");
break;
case 'e':
printf("please input the element to enqueue:\n");
scanf_s("%d", &x);
getchar();
Enqueue(x, q);
printf("[%d], enqueue successfully!\n", x);
break;
case 'o':
x = Front(q);
Dequeue(q);
printf("[%d] successfully dequeue!\n", x);
break;
case 'p':
printf("front:%d, rear:%d\n", q->Front, q->Rear);
break;
default:
break;
}
printf("c.create a queue;\nd.delete a queue\nm.make queue\
\empty\ne.enqueue an element\no.dequeue an element\nq.quit\n\n");
scanf_s("%c", &action);
getchar();
}
DisposeQueue(q);
printf("quit successfully!\n");
}*/