还是C链表,这次把我自己编的贴出来,望高人指点~

来源:百度知道 编辑:UC知道 时间:2024/06/30 14:44:30
按照年龄从大到小排序~~其他地方貌似没有问题,因为之前已经在http://zhidao.baidu.com/question/66152102.html悬赏了20分,有变的好还麻烦再进一下那个提问~我好给分啊~~谢谢了o(∩_∩)o...

/*函数头*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
#define M 10
int n;

/*结构体*/
struct student
{
int no;
char *name;
int age;
char sex;
struct student *next;
};

/*创建链表*/
struct student *creat()
{
struct student *head=NULL;
struct student *p1,*p2=(struct student *)malloc(LEN);
p1=p2;
n=0;
int i=1;
char name_stu[100][20];
p1->name=name_stu[0];
printf("请输入学号:\n");
scanf("%d",&p1->no);
printf("请输入姓名:\n");
scanf("%s",name_st

我给你解决方法,只是悬赏分太少 了。
方法思想是这样的,对一个链表排序,我们可以这样想,把链表中的每一个节点按顺序插入到另一个链表中,最后返回另一个链表的头指针。先写一个函数在一个链表中按顺序插入一个节点:代码如下
//在链表中按age升序插入节点
//输入:链表头指针,节点指针
//输出:链表头指针
struct student * insert(struct student *head,struct student *temp)
{
struct student * p=NULL;
if(NULL==temp)//如果插入空节点,直接退出返回头节点指针
{
return head;
}
if(NULL==head)//如果头指针为空,
{
head = temp;//新插入节点就作头
}else
{
if(head->age > temp->age)
{//如果头指针的值大于插入节点的值
//节点插入到头指针前面作为头节点
temp->next = head;//头放到当前插入节点后面
head = temp;//当前插入节点作头
}else
{
k = head;
p = head->next;//从第二个节点开始比较
while(NULL!=p)
{//如果插入节点值比当前节点值小,那插入到当前节点前面
//如果比当前节点值大,那么与下一节点值比较
if(temp->age < p->age)
{
temp->next=p;
k->next=temp;//在这之前k->next是等于p的
break;//节点插入,退出比较,退出while循环
}else
{
k=p;