单项链表排序

本文介绍了使用C语言实现单项链表的过程,并演示了如何创建链表、插入节点及进行排序的操作。通过具体代码示例,展示了不同排序方法的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天复习单项链表。自己编了一上午,蛋疼。。。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 2
struct student
{
    char name[M];
    double score;
    struct student *next;
};
struct student *creat(int n)
{
    struct student *phead, *pnew, *ptail;
    pnew = (struct student *)malloc(sizeof(struct student));
    phead = pnew;
    ptail = pnew;
    for (int i = 1; i < n; ++i)
    {
        pnew = (struct student *)malloc(sizeof(struct student));
        ptail->next = pnew;
        ptail = pnew;
    }
    ptail->next = NULL;
    return phead;
}
void pri(struct student *head)
{
    struct student *p;
    p = head;
    while(p != NULL)
    {
        printf("%s %f\n", p->name, p->score);
        p = p->next;
    }
}
struct student *insert(struct student *head)
{
    struct student *p, *pold, *pnew = (struct student *)malloc(sizeof(struct student));
    scanf("%s %lf", pnew->name, &pnew->score);
    p = head;
    if (pnew->score > head->score)
    {
        pnew ->next = head;
        head = pnew;
    }
    else
    {
        while(p != NULL && pnew->score < p->score)
        {
            pold = p;
            p = p->next;
        }
        pnew->next = p;
        pold->next = pnew;
    }
    return head;
}
int main(int argc, char const *argv[])
{
    struct student *head;
    int n = 0;
    scanf("%d", &n);
    head = (struct student *)malloc(sizeof(struct student));
    scanf("%s %lf", head->name, &head->score);
    head->next = NULL;
    for (int i = 1; i < n; ++i)
    {
        head = insert(head);
    }
    pri(head);
    return 0;
}

还有好些错的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 2
struct student
{
    // char name[M];
    double score;
    struct student *next;
};
struct student *creat(int n)
{
    struct student *head, *tail, *pnew;
    pnew = (struct student *)malloc(sizeof(struct student));
    scanf("%d", &pnew->score);
    head = pnew;
    tail = pnew;
    for (int i = 1; i < n; ++i)
    {
        pnew = (struct student *)malloc(sizeof(struct student));
        scanf("%d", &pnew->score);
        tail->next = pnew;
        tail = pnew;
    }
    tail->next = NULL;
    printf("Created\n");
    return head;
}

struct student *sort(struct student *head)
{
    struct student *p, *q, *small;
    double temp;
    for (p = head; p->next != NULL; p = p->next)
    {
        small = p;
        for (q = p->next; q; q = q->next)
        {
            if (q->score < small->score)
            {
                small = q;
            }
            if (small != p)
            {
                temp = p->score;
                p->score = small->score;
                small->score = temp;
            }
        }
    }
    return head;
}
int main(int argc, char const *argv[])
{
    int n = 0;
    scanf("%d", &n);
    struct student *head, *p, *q;
    head = creat(n);
    q = sort(head);
    for (p = q; p != NULL; p = p->next)
    {
        printf("%d\n", p->score);
    }
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 2
struct student
{
    char name[M];
    double score;
    struct student *next;
};
struct student *creat(int n)
{
    struct student *head, *tail, *pnew;
    pnew = (struct student *)malloc(sizeof(struct student));
    scanf("%s %d", &pnew->name, &pnew->score);
    head = pnew;
    tail = pnew;
    for (int i = 1; i < n; ++i)
    {
        pnew = (struct student *)malloc(sizeof(struct student));
        scanf("%s %d", &pnew->name, &pnew->score);
        tail->next = pnew;
        tail = pnew;
    }
    tail->next = NULL;
    printf("Created\n");
    return head;
}
// void swap(struct student *a, struct student *b)
// {
//  struct student c;
//  c = *a;
//  // a->name = b->name;
//  a->score = b->score;
//  *b->name = *c.name;

//  // b->score = 
//  // c = *a;
//  // *a = *b;
//  // *b = c;  
//  // printf("did swap\n");
//  // c->next = a->next;
//  // a-next = b->next;
// }
struct student *sort(struct student *head)
{
    printf("Began sort!\n");
    struct student *p,*q;
    // int k = 0;
    p = head;
    int i = 0;
    for (;p->next != NULL; p = p->next)
    {
        printf("first for\n");
        // for (q = p->next; q != NULL; q = q->next, i++)
        // {
        //  if (p->score < q->score)
        //  {
        //      // swap(p, q);
        //  }
        //  printf("i did it\n");
        // }
        // for (q = p->next; q != NULL; q = NULL)
        // {
        //  printf("second for\n");
        //  printf("q is %s\n", q->name);
        //  if (q->next == NULL)
        //  {
        //      printf("q's next == NULL\n");
        //  }
        //  printf("q->next = %s\n", q->next->name);
        //  // // if (q->next->next == NULL)
        //  // {
        //  //  printf("q-next-next = NULL\n");
        //  // }
        //  // printf("%s\n", q->next->name);
        //  // if (p->score < q->score)
        //  // {
        //  //  swap(p, q);
        //  // }
        // }
        // q = p->next;
        // while(q != NULL)
        // {
        //  if (p->score < q->score)
        //  {
        //      swap(p, q);
        //  }
        //  printf("before q = %s\n", q->next);
        //  q = q->next;
        //  printf("after q = %s\n", q->name);

    }
    // while(p->next != NULL)
    // {
    //  p = p->next;

    // }
    printf("Sorted\n");
}
int main(int argc, char const *argv[])
{
    int n = 0;
    scanf("%d", &n);
    struct student *head, *p;
    head = creat(n);
    // head = head->next;
    // for (int i = 1; head != NULL; head = head->next, i++)
    // {
    //  printf("%d'th yuansu\n", i);
    //  printf("%s\n", head);
    // }
    // if (head->next->next == NULL)
    // {
    //  printf("OK\n");
    // }
    // printf("%s\n", head);
    // printf("fist person is %s\n", head->name);
    head = sort(head);
    // if (head->next->next == NULL)
    // {
    //  printf("OK\n");
    // }
    // for (p = head; p != NULL; p = p->next)
    // {
    //  printf("%s %d\n", p->name, p->score);
    // }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值