-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-链表队列.cpp
68 lines (67 loc) · 1.23 KB
/
02-链表队列.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
#include <cstdio>
using PtrToNode=struct Node*;
using Position=PtrToNode;
struct Node{
int Data;
PtrToNode Next;
};
struct QNode{
Position Front,Rear;
int Maxsize;
};
using Queue=struct QNode*;
//创建空队列
Queue CreatQueue(int Maxsize){
Queue Q=new QNode;
Q->Front=Q->Rear=nullptr;
Q->Maxsize=Maxsize;
}
//判空
bool IsEmpty(Queue Q){
return Q->Front==nullptr;
}
//出队
constexpr auto ERROR=-1;
int DeleteQ(Queue Q){
PtrToNode FrontCell;
int FrontData;
if(IsEmpty(Q)){
printf("Empty\n");
return ERROR;
}
else{
FrontCell=Q->Front;
Q->Front=FrontCell->Next;
FrontData=FrontCell->Data;
delete FrontCell;
return FrontData;
}
}
//判满
bool IsFull(Queue Q){
PtrToNode P=Q->Front;
int count=0;
while(P){
count++;
P=P->Next;
}
return count==Q->Maxsize;
}
//入队
void AddQ(Queue Q,int X){
if(IsFull){
return;
}
PtrToNode N=new Node;
N->Data=X;
N->Next=nullptr;
if(IsEmpty){
Q->Front=Q->Rear=N;
return;
}
else{
Q->Rear->Next=N;
Q->Rear=N;
return;
}
}