首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

单向链表的排序.解决办法

2013-01-09 
单向链表的排序.各位前辈老师:能不能给个链表的排序方法代码示例. 用选择排序的.或者其他的算法.PS:#inclu

单向链表的排序.
各位前辈老师:
  能不能给个链表的排序方法代码示例. 用选择排序的.或者其他的算法.

PS:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 50
struct person
{
char  name[MAX];
int   number;
struct person *next;
};

struct person * input()
{
struct person *p,*q,*head;
head = NULL;
char temp[MAX];
puts("In put name:");
while(gets(temp)!=NULL && temp[0] != '\0')
{
p = (struct person*)malloc(sizeof(struct person));
if(head == NULL)
head = p;
else
q->next = p;
    p->next = NULL;
strcpy(p->name,temp);
puts("number:");
scanf("%d",&p->number);
while(getchar()!='\n')
continue;
puts("The next name:");
q = p;

}
return head;
}
void out(struct person *head)
{
struct person *p;
if(head == NULL)
puts("Not name in line");
else
{
p = head;
puts("---------------------");
puts("the person with number:");
while(p)
{
{
printf("name is %s the number is %d\n",p->name,p->number);
p=p->next;
}

}
puts("out end");
}
}

void cleanup(struct person *head)
{
struct person *p;
p=head;
while(p!=NULL)
{
free(p);
p=p->next;
}
}
main()
{
struct person *head;

head = input();
out(head);
cleanup(head);

return 0;
}
    
这个链表想实现以int   number;的大小.从大到小的排序.怎么排.
[解决办法]


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 50

struct person
{
    char name[MAX];
    int number;
    struct person *next;
};

struct person * input()
{
    struct person *p, *q, *head;
    char temp[MAX];
    head = NULL;
    puts("In put name:");
    while(gets(temp)!=NULL && temp[0] != '\0')
    {
        p = (struct person*)malloc(sizeof(struct person));
        if(head == NULL)
            head = p;
        else
            q->next = p;
        p->next = NULL;
        strcpy(p->name,temp);
        puts("number:");
        scanf("%d",&p->number);
        while(getchar()!='\n')
            continue;
        puts("The next name:");


        q = p;
    }
    return head;
}

void out(struct person *head)
{
    struct person *p;
    if(head == NULL)
        puts("Not name in line");
    else
    {
        p = head;
        puts("---------------------");
        puts("the person with number:");
        while(p)
        {
            printf("name is %s the number is %d\n",p->name,p->number);
            p=p->next;
        }
        puts("out end");
    }
}

//这个函数我修改下
void cleanup(struct person *head)
{
/**楼主这种释放方法,虽然释放后p的值不变,但是p->next的值可能就不确定了,所以应该改为下面的方式
    struct person *p;
    p=head;
    while(p != NULL)
    {
        free(p);
        p=p->next;
    }
**/
    struct person *p;
    while (head != NULL)
    {
        p = head;
        head = head->next;
        free(p);
    }
}

//选择排序
struct person *sort_list(struct person *head)
{
    struct person *p1, *p2;

    if (NULL == head)
        return NULL;

    for (p1 = head; p1->next != NULL; p1 = p1->next)
    {
        int max = p1->number;
        struct person *p_max = p1;

        for (p2 = p1->next; p2 != NULL; p2 = p2->next)
        {
            if (p2->number > max)
            {
                max = p2->number;
                p_max = p2;
            }
        }
        if (p_max != p1)
        {
            int tmp;
            char name[MAX];

            tmp = p1->number;


            p1->number = max;
            p_max->number = tmp;
            
            strcpy(name, p1->name);
            strcpy(p1->name, p_max->name);
            strcpy(p_max->name, name);
        }
    }
    return head;
}

int main()
{
    struct person *head;
    
    head = input();
    sort_list(head);
    out(head);
    cleanup(head);
    
    system("pause");
    return 0;
}

热点排行