解题思路:

首先,需要创建一个链表

1
2
3
4
5
6
7
8
9
10
typedef struct student {
    int id;
    int chengji;
    struct student* next;
}*node, Node;
 
//一般创建链表我们都用typedef  struct,因为这样定义结构体变量时,
//我们就可以直接可以用node a 来定义结构体指针,Node b来定义结构体变量
//其实就是用 node 代替了struct student * 
//Node 代替了struct student



其次初始化一个链表,n为链表节点个数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
node creat(int n)
{
    node head, p, q;
    head = (node)malloc(sizeof(Node));
    head->next = NULL;
    q = head;
    for (int i = 0; i < n; i++)
    {
        p = (node)malloc(sizeof(Node));
        scanf("%d%d", &(*p).id, &p->chengji);
        p->next = q->next; //这里用的链表的后插法
        q->next = p;
        q = p;
    }
    return head;
}



之后是进行删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void fun(node h1, node h2)
{
    h2 = h2->next;
 
    node p, t = h2;
    while (h1->next != NULL)
    {
        h2 = t;
        int flag = 0;
        while (h2 != NULL)
        {
            p = (node)malloc(sizeof(Node));
            if (h1->next->id == h2->id)
            {
                p = h1->next;
                h1->next = p->next;
                free(p);
                flag = 1;
            }
            h2 = h2->next;
        }
        if(flag == 0)    //重点!
        h1 = h1->next;   //重点!
    }
}


这里有一个重点,就是如果进行了删除,是不需要进行指向下一个链表的,

因为h1之前指向的next已经被删除了,现在指向的next应该是之前的next

的next,所以不需要再往后移动了




最后进行打印

1
2
3
4
5
6
7
8
9
10
11
12
13
void outPut(node h1)
{
    int count = 0;
    node p = h1;
    while (h1 = h1->next) count++;
    printf("%d\n", count);
    p = p->next;
    while (p != NULL)
    {
        printf("%d %d\n", p->id, p->chengji);
        p = p->next;
    }
}




参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <malloc.h>
 
typedef struct student {
    int id;
    int chengji;
    struct student* next;
}*node, Node;
 
node creat(int n)
{
    node head, p, q;
    head = (node)malloc(sizeof(Node));
    head->next = NULL;
    q = head;
    for (int i = 0; i < n; i++)
    {
        p = (node)malloc(sizeof(Node));
        scanf("%d%d", &(*p).id, &p->chengji);
        p->next = q->next;
        q->next = p;
        q = p;
    }
    return head;
}
 
void fun(node h1, node h2)
{
    h2 = h2->next;
 
    node p, t = h2;
    while (h1->next != NULL)
    {
        h2 = t;
        int flag = 0;
        while (h2 != NULL)
        {
            p = (node)malloc(sizeof(Node));
            if (h1->next->id == h2->id)
            {
                p = h1->next;
                h1->next = p->next;
                free(p);
                flag = 1;
            }
            h2 = h2->next;
        }
        if(flag == 0)
        h1 = h1->next;
    }
}
 
 
void outPut(node h1)
{
    int count = 0;
    node p = h1;
    while (h1 = h1->next) count++;
    printf("%d\n", count);
    p = p->next;
    while (p != NULL)
    {
        printf("%d %d\n", p->id, p->chengji);
        p = p->next;
    }
}
 
 
int main()
{
    int m, n;
    node head1, head2;
    scanf("%d%d", &n, &m);
    head1 = creat(n);
    head2 = creat(m);
    fun(head1, head2);
    outPut(head1);
    return 0;
}


点赞(0)
 

0 分

0 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论