文章

36

粉丝

0

获赞

4

访问

9.3k

头像
链表合并 题解:c实现
P1025 贵州大学2019机试
发布于2024年3月26日 12:38
阅读数 310

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*LinkList;

void freeList(LinkList l) {
    LNode *current = l->next;
    while (current != NULL) {
        LNode *temp = current;
        current = current->next;
        free(temp);
    }
    free(l);
}

LinkList InitList(LinkList &l){
    l = (LNode*)malloc(sizeof(LNode));
    if(l) l->next = NULL;
    return l;
}

LinkList list_TailInsert(LinkList &l,int x){
    LNode *s,*r=l;
    int num;
    while(x>0){
        scanf("%d",&num);
        s=(LNode*)malloc(sizeof(LNode));
        s->data = num;
        s->next = NULL; // 新节点的next指针初始化为NULL
        r->next = s; // 将新节点连接到链表的尾部
        r = s; // 更新r指向最后一个节点
        x--;
    }
    return l;
}

int main(){
    Link...
登录查看完整内容


登录后发布评论

2 条评论
snake
2024年3月26日 17:36

问题有点多

这里应该等于next

下面几个if里的赋值方向反了

应该是r3->next = s;

最后输出之前

l3 = l3->next;

赞(0)

williams : 回复 snake: ok了哥

2024年3月28日 11:51