m0_67597520 2022-05-03 16:01 采纳率: 86.4%
浏览 259
已结题

如何把它按学号顺序从小到大建立有序链表

如何把它按学号顺序从小到大建立有序链表,最后遍历链表,并按顺序输出学生信息
输入
1
3
20080108 Zhangsan
20070328 Lisi
20070333 Wangwu
输出
20070328 Lisi
20070333 Wangwu
20080108 Zhangsan
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stud_node {

 char   name[20];
 int    xh;
 struct stud_node *next;

};
struct stud_node *head, *tail;

void input();

int main()
{
struct stud_node *p;

head = tail = NULL;
input();
for ( p = head; p != NULL; p = p->next )
    printf("%d %s\n",  p->xh ,p->name);

return 0;

}
void input()
{
struct stud_node *pt;
pt = (struct stud_node *)malloc(sizeof(struct stud_node));
int n,num;
scanf("%d",&n);
while(n--){
scanf("%d",&num);
for(int i=1;i<=num;i++){
scanf("%d %s", &pt->xh,pt->name);
if (head == NULL)
{
head = pt;
head->next = NULL;
}

    if (tail != NULL)
    {
        tail->next = pt;
    }

    
    tail = pt;
    tail->next = NULL;
    pt = (struct stud_node *)malloc(sizeof(struct stud_node));
    
}

}}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-05-04 00:01
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct stud_node {
        char   name[20];
        int    xh;
        struct stud_node *next;
    };
    struct stud_node *head, *tail;
    void input();
    int main()
    {
        struct stud_node *p;
        head = tail = NULL;
        input();
        for (p = head; p != NULL; p = p->next )
             printf("%d %s\n",  p->xh ,p->name);
        return 0;
    }
    void input()
    {
        struct stud_node *pt, *ptr;
        int n,num;
        scanf("%d",&n);
        while(n--){
             scanf("%d",&num);
             for(int i=1;i<=num;i++){
                 pt = (struct stud_node *)malloc(sizeof(struct stud_node));
                 pt->next = NULL;
                 scanf("%d %s", &pt->xh,pt->name);
                 if (head == NULL)
                 {
                     head = pt;
                     head->next = NULL;
                 }
                 else   //if (tail != NULL)
                 {
                     tail = head; ptr = NULL;
                     while (tail && tail->xh < pt->xh){
                           ptr = tail;
                           tail = tail->next;
                     }
                     if (ptr == NULL){
                         pt->next = head;
                         head = pt;
                     }
                     else{
                         pt->next = ptr->next;
                         ptr->next = pt;
                     }
                 }
                 //tail = pt;
                 //tail->next = NULL;
                 //pt = (struct stud_node *)malloc(sizeof(struct stud_node));
             }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月12日
  • 已采纳回答 5月4日
  • 创建了问题 5月3日

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)