链表遍历,链表查找和统计节点,链表插入新节点,链表删除节点,链表修改指定节点,链表头插法,尾插法总结

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
简介: 链表遍历,链表查找和统计节点,链表插入新节点,链表删除节点,链表修改指定节点,链表头插法,尾插法总结

1.链表静态增加和动态遍历

#include <stdio.h>
 
struct Test
{
        int data;
        struct Test *next;
};
 
void printflink(struct Test *head)
{
        while(head!=NULL){
                printf("%d\n",head->data);
                head = head->next;
        }
}
 
int main()
{
        struct Test *head = NULL;
        struct Test p1 = {11,NULL};
        struct Test p2 = {22,NULL};
        struct Test p3 = {33,NULL};
        struct Test p4 = {44,NULL};
 
        head = &p1;
        p1.next = &p2;
        p2.next = &p3;
        p3.next = &p4;
        printflink(&p1);
 
        return 0;
}
 

2.统计链表节点各数及链表查找

#include <stdio.h>
 
struct Test
{
        int data;
        struct Test *next;
};
 
void printflink(struct Test *head)
{
        while(head!=NULL){
                printf("%d\n",head->data);
                head = head->next;
        }
}
 
int getlinksum(struct Test *head)
{
        int cnt;
        while(head!=NULL){
                cnt++;
                head = head->next;
        }
        return cnt;
}
 
int findlink(struct Test *head,int newdata)
{
        while(head!=NULL){
                if(head->data == newdata){
                        return 1;
                }
                head = head->next;
        }        return 0;
}
 
int main()
{
        struct Test *head = NULL;
        struct Test p1 = {11,NULL};
        struct Test p2 = {22,NULL};
        struct Test p3 = {33,NULL};
        struct Test p4 = {44,NULL};
 
        head = &p1;
        p1.next = &p2;
        p2.next = &p3;
        p3.next = &p4;
        printflink(&p1);
 
        int ret1 = getlinksum(head);
        printf("the link sum = %d\n",ret1);
 
        int ret2 = findlink(&p1,111);
        if(ret2 == 1){
                printf("hava newdata\n");
        }
        else{
                printf("no newdata\n");
        }
 
        return 0;
}
 

3.链表从指定节点后方插入新节点

struct Test* insertToBehind(struct Test *head, int data, struct Test *new)
{
        struct Test *p = head;
 
        while (p != NULL) {
                if ( data == p->data) {
                        new->next = p->next;
                        p->next = new;
                        return head;
                }
                p = p->next;
        }
}
 

4.链表从指定节点前方插入新节点

最后返回头节点

 
struct Test* insertToFront(struct Test *head, int data, struct Test *new)
{
        struct Test *p = head;
 
        if (data == p->data) {
                new->next = head;
                printf("insert success\n");
                return new;
        }
 
        while (p->next != NULL) {
                if (data == p->next->data) {
                        new->next = p->next;
                        p->next = new;
                        printf("insert success\n");
                        return head;
                }
                p = p->next;
        }
}
 

5.链表删除指定节点

include <stdio.h>
 
struct Test
{
        int data;
        struct Test *next;
};
 
void printflink(struct Test *head)
{
        while(head!=NULL){
                printf("%d\n",head->data);
                head = head->next;
        }
}
 
int getlinksum(struct Test *head)
{
        int cnt;
        while(head!=NULL){
                cnt++;
                head = head->next;
        }
        return cnt;
}
 
int findlink(struct Test *head,int newdata)
{
        while(head!=NULL){
                if(head->data == newdata){
                        return 1;
                }
                head = head->next;
        }
        return 0;
}
 
 
struct Test* deleteNode(struct Test *head,int data)
{
        if(head->data == data){
                head = head->next;
                return head;
        }
 
        while(head->next!=NULL){
                if(head->next->data == data){
                          head->next = head->next->next;
                          return head;
                }
                head = head->next;
        }
        return head;
}
 
int main()
{
        struct Test *head = NULL;
        struct Test p1 = {11,NULL};
        struct Test p2 = {22,NULL};
        struct Test p3 = {33,NULL};
        struct Test p4 = {44,NULL};
        struct Test new = {520,NULL};
 
        head = &p1;
        p1.next = &p2;
        p2.next = &p3;
        p3.next = &p4;
 
        printflink(head);
 
        int ret1 = getlinksum(head);
        printf("the link sum = %d\n",ret1);
 
        head = deleteNode(head,11);
        printflink(head);
        int ret2 = getlinksum(head);
        printf("the newlink sum = %d\n",ret2);
 
        return 0;
}

6.链表修改指定节点

#include <stdio.h>
 
struct Test
{
        int data;
        struct Test *next;
};
 
void printflink(struct Test *head)
{
        while(head!=NULL){
                printf("%d\n",head->data);
                head = head->next;
        }
}
 
int getlinksum(struct Test *head)
{
        int cnt;
        while(head!=NULL){
                cnt++;
                head = head->next;
        }
        return cnt;
}
 
int changeNode(struct Test *head,int data,int newdata)
{
        while(head!=NULL){
                if(head->data == data){
                        head->data = newdata;
                        return 1;
                }
                head = head->next;
        }
        return 0;
}
 
int main()
{
        struct Test *head = NULL;
        struct Test p1 = {11,NULL};
        struct Test p2 = {22,NULL};
        struct Test p3 = {33,NULL};
        struct Test p4 = {44,NULL};
        struct Test new = {520,NULL};
 
        head = &p1;
        p1.next = &p2;
        p2.next = &p3;
        p3.next = &p4;
 
        printflink(head);
        int ret1 = getlinksum(head);
        printf("the link sum = %d\n",ret1);
 
        changeNode(head,11,111);
        printflink(head);
        int ret2 = getlinksum(head);
        printf("the newlink sum = %d\n",ret2);
 
        return 0;
}

7.头插法动态创建链表

#include <stdio.h>
#include <stdlib.h>
 
struct Test
{
        int data;
        struct Test *next;
};
 
void printflink(struct Test *head)
{
        while(head!=NULL){
                printf("%d\n",head->data);
                head = head->next;
        }
}
 
struct Test* insertfromhead(struct Test *head,struct Test *new)
{
        if(head == NULL){
                head = new;
        }
        else{
                new->next = head;
                head = new;
        }
        return head;
}
 
struct Test* headinsert(struct Test *head)
{
        struct Test *new;
        while(1){
                new = (struct Test*)malloc(sizeof(struct Test));
                scanf("%d",&(new->data));
                if(new->data == 0){
                        printf("end\n");
                        free(new);
                        return head;
                }
                head = insertfromhead(head,new);
        }
}
 
int main()
{
        struct Test *head = NULL;
 
        head = headinsert(head);
        printflink(head);
 
        return 0;
}

8.尾插法动态创建链表

#include <stdio.h>
#include <stdlib.h>
 
struct Test
{
        int data;
        struct Test *next;
};
 
void printflink(struct Test *head)
{
        while(head!=NULL){
                printf("%d\n",head->data);
                head = head->next;
        }
}
 
struct Test* insertfromend(struct Test *head,struct Test *new)
{
        struct Test *p = head;
        if(p == NULL){
                head = new;
                return head;
        }
 
        while(p!=NULL){
                if(p->next == NULL){
                        p->next = new;
                        return head;
                }
                p = p->next;
        }
}
 
struct Test* endinsert(struct Test *head)
{
        struct Test *new;
        while(1){
                new = (struct Test*)malloc(sizeof(struct Test));
                scanf("%d",&(new->data));
                if(new->data == 0){
                        printf("end\n");
                        free(new);
                        return head;
                }
                head = insertfromend(head,new);
        }
}
 
int main()
{
        struct Test *head = NULL;
 
        head = endinsert(head);
        printflink(head);
 
        return 0;
}
相关实践学习
基于Hologres轻松玩转一站式实时仓库
本场景介绍如何利用阿里云MaxCompute、实时计算Flink和交互式分析服务Hologres开发离线、实时数据融合分析的数据大屏应用。
Linux入门到精通
本套课程是从入门开始的Linux学习课程,适合初学者阅读。由浅入深案例丰富,通俗易懂。主要涉及基础的系统操作以及工作中常用的各种服务软件的应用、部署和优化。即使是零基础的学员,只要能够坚持把所有章节都学完,也一定会受益匪浅。
相关文章
|
2月前
LeetCode第二十四题(两两交换链表中的节点)
这篇文章介绍了LeetCode第24题的解法,即如何通过使用三个指针(preNode, curNode, curNextNode)来两两交换链表中的节点,并提供了详细的代码实现。
22 0
LeetCode第二十四题(两两交换链表中的节点)
|
2月前
Leetcode第十九题(删除链表的倒数第N个节点)
LeetCode第19题要求删除链表的倒数第N个节点,可以通过快慢指针法在一次遍历中实现。
44 0
Leetcode第十九题(删除链表的倒数第N个节点)
05_删除链表的倒数第N个节点
05_删除链表的倒数第N个节点
|
2月前
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
(剑指offer)18、删除链表的节点—22、链表中倒数第K个节点—25、合并两个排序的链表—52、两个链表的第一个公共节点(2021.12.07)
52 0
|
4月前
|
算法
LeetCode第24题两两交换链表中的节点
这篇文章介绍了LeetCode第24题"两两交换链表中的节点"的解题方法,通过使用虚拟节点和前驱节点技巧,实现了链表中相邻节点的交换。
LeetCode第24题两两交换链表中的节点
04_两两交换链表中的节点
04_两两交换链表中的节点
|
4月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
54 5
|
4月前
|
Python
【Leetcode刷题Python】剑指 Offer 18. 删除链表的节点
Leetcode题目"剑指 Offer 18. 删除链表的节点"的Python解决方案,通过使用双指针法找到并删除链表中值为特定数值的节点,然后返回更新后的链表头节点。
45 4
|
5月前
|
安全 云计算
云计算自旋锁问题之在线程安全地删除链表节点时,需要频繁加锁会影响性能如何解决
云计算自旋锁问题之在线程安全地删除链表节点时,需要频繁加锁会影响性能如何解决
53 2
|
5月前
|
存储
链表的遍历方式
链表的遍历方式