1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #include<math.h>
5 #define TRUE 1
6 #define FALSE 0
7 #define OK 1
8 #define ERROR 0
9
10 typedef struct Node
11 {
12 char name[10];
13 struct Node *next;
14 }QNode;
15 typedef struct
16 {
17 QNode *front;
18 QNode *rear;
19 }LinkQueue;
20
21 int InitQueue(LinkQueue &Q) //构建带头结点的空队列
22 {
23 Q.front=(QNode *)malloc(sizeof(QNode));
24 if(Q.front!=NULL)
25 {
26 Q.front->next=NULL;
27 Q.rear=Q.front;
28 }
29 return OK;
30 }
31
32 int EnQueue(LinkQueue &Q,char *str) //将字符串入队
33 {
34 QNode *p;
35 p=(QNode *)malloc(sizeof(QNode));
36 strcpy(p->name,str);
37 p->next=NULL;
38 Q.rear->next=p;
39 Q.rear=p;
40 return OK;
41 }
42
43 int DeQueue(LinkQueue &Q,char *str) //出队
44 {
45 QNode *p;
46 p=Q.front->next;
47 strcpy(str,p->name);
48 Q.front->next=p->next;
49 Q.rear->next=p; //结点出队后连到队列后方,形成循环
50 Q.rear=p;
51 Q.rear->next=NULL;
52 return OK;
53 }
54 int VisitQueue(LinkQueue Q) //访问队列元素
55 {
56 QNode *p;
57 if(Q.front->next==NULL)
58 return ERROR;
59 p=Q.front->next;
60 while(p!=NULL)
61 {
62 printf("%s->",p);
63 p=p->next;
64 }
65 printf("NULL\n");
66 return OK;
67 }
68
69 void Party(LinkQueue &Q1,LinkQueue &Q2)
70 {
71 InitQueue(Q1);
72 InitQueue(Q2);
73 char str[10],boy[10],girl[10];
74
75 int b,g,ls,i,j,k,l; //b为男生人数,g为女生人数,ls为轮数
76 /*
77 在此处补充完整代码,使Party()函数完整
78 */
79 char *p,*h,*str1;
80 p=boy;
81 h=girl;
82 str1=str;
83 printf("请输入轮数:");
84 scanf("%d",&ls);
85 printf("请输入男生人数:");
86 scanf("%d",&b);
87 printf("请输入男生姓名\n");
88 for(j=1;j<=b;j++)
89 {
90 scanf("%s",p);
91 EnQueue(Q1,p);
92 p++;
93 }
94 VisitQueue(Q1);
95
96 printf("请输入女生人数:");
97 scanf("%d",&g);
98 printf("请输入女生姓名\n");
99 for(j=1;j<=g;j++)
100 {
101 scanf("%s",h);
102 EnQueue(Q2,h);//入
103 h++;
104 }
105 VisitQueue(Q2) ;//访问
106
107 QNode *L1,*L2;
108 L1=Q1.front->next;
109 L2=Q2.front->next;
110 QNode *wo;
111
112 for(i=1;i<=ls;i++)
113 {
114 printf("第%d轮配对\n",i);
115 if(g-b>0)
116 {
117 while(L1&&L2)
118 {
119 printf("%s<------->%s\n",L1->name,L2->name);
120 L1=L1->next;
121
122 L2=L2->next;
123 wo=L2;
124 if(L2==NULL)
125 {
126 L2=Q2.front->next;
127 }
128 }
129
130 L1=Q1.front->next;
131 for(int z=1;z<=g-b;z++)
132 {
133 if(wo==NULL)//到尾时,指向头
134 wo=Q2.front->next;
135 printf("%s待会。。。。\n",wo->name);
136 wo=wo->next;
137 }
138 }
139 else if(g==b)
140 {
141 while(L1&&L2)
142 {
143 printf("%s<------->%s\n",L1->name,L2->name);
144 L2=L2->next;
145 L1=L1->next;
146 wo=L1;
147 if(L1==NULL)
148 {
149 L1=Q1.front->next;
150 }
151 }
152 DeQueue(Q2,str1);
153 L2=Q2.front->next;
154 for(int z=1;z<=g-b;z++)
155 {
156 if(wo==NULL)
157 wo=Q1.front->next;
158 printf("%s待会。。。。\n",wo->name);
159 wo=wo->next;
160 }
161 }
162 else
163 {
164 while(L1&&L2)
165 {
166 printf("%s<------->%s\n",L1->name,L2->name);
167 L2=L2->next;
168 L1=L1->next;
169 wo=L1;
170 if(L1==NULL)
171 {
172 L1=Q1.front->next;
173 }
174 }
175 L2=Q2.front->next;
176 for(int z=1;z<=b-g;z++)
177 {
178 if(wo==NULL)
179 wo=Q1.front->next;
180 printf("%s待会。。。。\n",wo->name);
181 wo=wo->next;
182 }
183 }printf("\n");
184 }
185 }
186
187 void main()
188 {
189 LinkQueue Q1,Q2;
190 Party(Q1,Q2);
191 }