一个有关用队列做舞伴的问题,请教下。

qq576240289 2011-08-19 09:32:57
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct nodetype
{
char name[10];
char sex;
struct nodetype *next;
}nodetype;

typedef struct
{
nodetype *front;
nodetype *rear;
}linkqueue;

nodetype *enter_queue(nodetype *p, linkqueue queue)
{
nodetype *s = NULL;

s = (struct nodetype *)malloc(sizeof(struct nodetype));
strcpy(s->name, p->name);
s->sex = p->sex;
s->next = NULL;
queue.rear->next = s; 调试到这一步就不行了,请问下是怎么了???????

return s;
}

nodetype *quit_queue(linkqueue queue)
{
nodetype *p;

printf("%s ", queue.front->name);
p = queue.front->next;
free(queue.front);

return p;

}

int main(void)
{
char ch, w;
nodetype person;
linkqueue man_queue, woman_queue;

man_queue.front = man_queue.rear = NULL;
woman_queue.front = woman_queue.rear = NULL;

printf("是否输入人员(Y/N):");
scanf("%c", &ch);
w = getchar();

while (ch == 'Y')
{
printf("请输入姓名:\n");
scanf("%s", &person.name);
w=getchar();
printf("请输入性别(F/M):\n");
scanf("%c", &person.sex);

if (person.sex = 'M')
{
man_queue.rear = enter_queue(&person, man_queue);
if(NULL == man_queue.front)
{
man_queue.front = man_queue.rear;
}
}
else
{
woman_queue.rear = enter_queue(&person, woman_queue);
if(NULL == woman_queue.front)
{
woman_queue.front = woman_queue.rear;
}
}

printf("是否输入人员(Y/N):");
w = getchar();
scanf("%c", &ch);
}

printf("男女舞伴配对为:\n");

while(man_queue.front != NULL && woman_queue.front != NULL)
{
man_queue.front = quit_queue(man_queue);
woman_queue.front = quit_queue(woman_queue);
printf("\n");
}

return 0;
}
...全文
135 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
xin_wu_hen 2011-08-19
  • 打赏
  • 举报
回复

queue.rear->next = s;

中,queue的rear值为NULL,NULL的next是什么?没有任何东东,你赋值可定会错了。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧