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

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

如何把它按学号顺序从小到大建立有序链表,最后遍历链表,并按顺序输出学生信息
输入
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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址