-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkQueue.cpp
96 lines (88 loc) · 1.66 KB
/
LinkQueue.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
#include <iostream>
#include <string>
using namespace std;
typedef struct node
{
string data;
struct node * pnext;
}Lnode;
typedef struct
{
Lnode * front;
Lnode * end;
}LinkQueue;
int empty(LinkQueue * LQ)
{
return LQ->front == LQ->end ? 1 : 0;
}
void InitLinkQueue(LinkQueue * LQ)
{
Lnode * pt;
pt = new Lnode;
pt->pnext = NULL;
LQ->front = pt;
LQ->end = pt;
}
void EnQueue(LinkQueue * LQ, string elem)
{
Lnode * pt;
pt = new Lnode;
if(!pt)
{
cout << "内存分配失败。" << endl;
return;
}
pt->data = elem;
pt->pnext = LQ->end->pnext;
LQ->end->pnext = pt;
LQ->end = pt;
}
void OutQueue(LinkQueue * LQ, string * elem)
{
if(empty(LQ))
{
cout << "队列已经是空的。" << endl;
return;
}
Lnode * pt = LQ->front->pnext;
LQ->front->pnext = pt->pnext;
*elem = pt->data;
delete pt;
if(LQ->front->pnext==NULL)
{
LQ->end = LQ->front;
}
}
void GetHead(LinkQueue * LQ, string * elem)
{
if(empty(LQ))
{
cout << "队列已经是空的。" << endl;
return;
}
*elem = LQ->front->pnext->data;
}
int main()
{
LinkQueue LQ;
InitLinkQueue(&LQ);
EnQueue(&LQ, "zhang");
EnQueue(&LQ, "lei");
EnQueue(&LQ, "shi");
EnQueue(&LQ, "hao");
EnQueue(&LQ, "ren");
string str;
OutQueue(&LQ, &str);
cout << str << endl;
OutQueue(&LQ, &str);
cout << str << endl;
OutQueue(&LQ, &str);
cout << str << endl;
OutQueue(&LQ, &str);
cout << str << endl;
OutQueue(&LQ, &str);
cout << str << endl;
OutQueue(&LQ, &str);
cout << str << endl;
return 0;
}