iwiiw 2023-01-23 19:56 采纳率: 70%
浏览 39
已结题

关于#链表#的问题,如何解决?

img


调了一天程序了,题目是将两个链表合并病按照学号升降排序,不知道是哪里出了问题

  • 写回答

3条回答 默认 最新

  • 有一只小精灵 2023-01-23 20:22
    关注

    实现的是2个有序链表按升序排,修改了合并处的代码

    #include <stdio.h>
    #include <stdlib.h>
    
    struct circle{
        int ID;
        char name;
        struct circle* next;
    };
    
    int main() {
        struct circle* head = (struct circle*) malloc(sizeof(struct circle));
        head->next=NULL;
        struct circle* p;
        p = head;
        int n;
        printf("请输入表长:");
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            struct circle* s = (struct circle*) malloc(sizeof(struct circle));
            p->next = s;
            printf("请输入ID: ");
            scanf("%d", &(s->ID));
            printf("请输入name: ");
            getchar();
            scanf("%c", &(s->name));
            s->next = NULL;
            p = s;
        }
        p = head;
    
        struct circle* head1 = (struct circle*) malloc(sizeof(struct circle));
        head1->next=NULL;
        struct circle* p1;
        p1 = head1;
        int k;
        printf("请输入表长:");
        scanf("%d", &k);
        for (int i = 1; i <= k; i++) {
            struct circle* s = (struct circle*) malloc(sizeof(struct circle));
            p1->next = s;
            printf("请输入ID: ");
            scanf("%d", &(s->ID));
            printf("请输入name: ");
            getchar();
            scanf("%c", &(s->name));
            s->next = NULL;
            p1 = s;
        }
        p1 = head1;
    
        struct circle* head2 = (struct circle*) malloc(sizeof(struct circle));
        head2->next=NULL;
        struct circle* p2;
        p2 = head2;
        p = p->next;
        p1 = p1->next;
    
        while(p != NULL || p1 != NULL) {
            if (p1 == NULL || (p != NULL && p->ID < p1->ID)) {
                p2->next = p;
                p2 = p;
                p = p->next;
            } else {
                p2->next = p1;
                p2 = p1;
                p1 = p1->next;
            }
        }
    
        p2 = head2;
        printf("输出:");
        for (int i = 0; i < n+k; i++) {
            p2 = p2->next;
            printf("%d %c ", (p2->ID), (p2->name));
        }
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月31日
  • 已采纳回答 1月23日
  • 创建了问题 1月23日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算