怎么在这个链表中互换字符串

C语言 码拜 8年前 (2016-06-10) 1127次浏览
本人创建了一个链表,想要在年龄大小排序后,把姓名这个字符串也进行对应的排序,但是现在遇到了不知道怎么在这个链表中互换字符串的问题,求高手解决,提供一种解决方法,谢谢
[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);
	}
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么在这个链表中互换字符串
喜欢 (1)
[1034331897@qq.com]
分享 (0)