[code=c][/# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
struct stduent * creat_list(void);
void printf_list(struct stduent *);
struct stduent
{
int age;
char * name; // 看这里!
struct stduent * pnext;
};
int main(void)
{
struct stduent * phead;
phead = creat_list();
printf_list(phead);
return 0;
}
// 创建一个链表;
struct stduent * creat_list()
{
// 创建头结点;
struct stduent * phead = (struct stduent *)malloc(sizeof(struct stduent));
struct stduent * ptemp = phead;
// 创建后续节点;
int ren,i;
printf (“请输入学生的人数:\n”);
scanf (“%d”,&ren);
for (i = 0; i<ren; i++)
{
struct stduent * pnew = (struct stduent *)malloc(sizeof(struct stduent));
printf (“请输入第%d个学生的年龄:”,i+1);
scanf (“%d”,&pnew->age);
printf (“请输入第%d个学生的姓名:”,i+1);
scanf (“%s”,&pnew->name); // 看这里!
pnew->pnext = NULL;
ptemp->pnext = pnew;
ptemp = pnew;
}
return phead;
}
// 遍历输出每一个节点;
void printf_list(struct stduent * phead)
{
struct stduent * p, * h;
int temp1;
char * temp2; // 看这里!
for (p = phead->pnext; p != NULL; p = p->pnext)
{
for (h = p->pnext; h != NULL; h = h->pnext)
{
if (p->age < h->age)
{
temp1 = p->age;
p->age = h->age;
h->age = temp1;
// 看这里!
temp2 = p->name;
p->name = h->name;
h->name = temp2;
}
}
}
for (p = phead->pnext; p != NULL; p = p->pnext)
{
printf (“%d\n”,p->age);
printf (“%s\n”,p->name);
}
}
code]
运行结果是这样的,请高手给解决一下,谢谢

40
1.需要为pnew->name给malloc字符数组空间
2.printf_list函数里字符串拷贝要用strcpy,而不是直接用=
3.char temp2[32] = {0}; 要分配空间,而不是char *temp2;
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
#include<string.h>
struct stduent * creat_list(void);
void printf_list(struct stduent *);
struct stduent
{
int age;
char * name; // 看这里!
struct stduent * pnext;
};
int main(void)
{
struct stduent * phead;
phead = creat_list();
printf_list(phead);
return 0;
}
// 创建一个链表;
struct stduent * creat_list()
{
// 创建头结点;
struct stduent * phead = (struct stduent *)malloc(sizeof(struct stduent));
struct stduent * ptemp = phead;
// 创建后续节点;
int ren, i;
printf("请输入学生的人数:\n");
scanf("%d", &ren);
for (i = 0; i<ren; i++)
{
struct stduent * pnew = (struct stduent *)malloc(sizeof(struct stduent));
pnew->name = (char *)malloc(32);
printf("请输入第%d个学生的年龄:", i + 1);
scanf("%d", &pnew->age);
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", pnew->name); // 看这里!
pnew->pnext = NULL;
ptemp->pnext = pnew;
ptemp = pnew;
}
return phead;
}
// 遍历输出每一个节点;
void printf_list(struct stduent * phead)
{
struct stduent * p, *h;
int temp1;
char temp2[32] = {0}; // 看这里!
for (p = phead->pnext; p != NULL; p = p->pnext)
{
for (h = p->pnext; h != NULL; h = h->pnext)
{
if (p->age < h->age)
{
temp1 = p->age;
p->age = h->age;
h->age = temp1;
printf("%s %s\n", p->name, h->name);
// 看这里!
strcpy(temp2, p->name);
strcpy(p->name, h->name);
strcpy(h->name, temp2);
//temp2 = p->name;
//p->name = h->name;
//h->name = temp2;
}
}
}
for (p = phead->pnext; p != NULL; p = p->pnext)
{
printf("%d\n", p->age);
printf("%s\n", p->name);
}
}