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

news/2025/2/23 9:43:01

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.链表从指定节点后方插入新节点

#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;
}

void insertfrombehind(struct Test *head,int data,struct Test *new)
{
        while(head!=NULL){
                if(head->data == data){
                        new->next = head->next;
                        head->next = new;
                }
                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};
        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);

        insertfrombehind(head,44,&new);
        printflink(head);
        int ret2 = getlinksum(head);
        printf("the newlink sum = %d\n",ret2);

        return 0;
}

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

#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;
}

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

        while(head->next!=NULL){
                if(head->next->data == data){
                        new->next = head->next;
                        head->next = new;
                        printf("insert ok\n");
                        return head;
                }
                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};
        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 = insertfromfront(head,11,&new);
        printflink(head);
        int ret2 = getlinksum(head);
        printf("the newlink sum = %d\n",ret2);

        return 0;
}

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;
}


http://www.niftyadmin.cn/n/4971946.html

相关文章

【层序遍历】637. 二叉树的层平均值

637. 二叉树的层平均值 解题思路 二叉树的层序遍历累加每一层的节点值 然后求平均值将每一层的平均值添加到list 返回 class Solution {public List<Double> averageOfLevels(TreeNode root) {// 二叉树的层序遍历List<List<Integer>> result new ArrayLi…

算法:图解前缀和问题

文章目录 实现原理实现思路一维前缀和模板二维前缀和模板 典型例题一维前缀和二维前缀和寻找数组中心下标除自身以外数组的乘积关系矩阵和 总结 实现原理 前缀和问题和二分查找类似&#xff0c;也是有一些固定的模板的&#xff0c;在理解原理的基础上进行实践&#xff0c;就能…

抖音电商,从消费者体验中做增量

夜晚总是最容易emo&#xff0c;也最容易冲动的时候。 王雪临睡前刷着抖音&#xff0c;看到一家化妆品品牌在直播&#xff0c;刚好最近她想买抗老精华&#xff0c;点进去听主播小姐姐介绍一番后下了单。第二天早上起来犹豫要不要退货&#xff0c;再货比三家时&#xff0c;手机收…

YOLOv5算法改进(5)— 添加ECA注意力机制

前言&#xff1a;Hello大家好&#xff0c;我是小哥谈。ECA注意力机制是一种用于图像处理中的注意力机制&#xff0c;是在通道注意力机制的基础上做了进一步的改进。通道注意力机制主要是通过提取权重&#xff0c;作用在原特征图的通道维度上&#xff0c;而ECA注意力机制则使用了…

使用Coding对vue项目进行自动化的部署 (亲测有用) coding部署vue项目

使用Coding对vue项目进行自动化的部署 &#xff08;亲测有用&#xff09; 登陆coding 官网 1. 新建项目看下面 这篇文字&#xff0c;新建 vue 项目和 java 一样 选择这个新建 选择代码仓库 点击确定 选择文本编辑器 把下面 内容 粘贴 进去 &#xff0c;然后改几个内容 服务器…

设计模式之八:模板方法模式

泡咖啡和泡茶的共同点&#xff1a; 把水煮沸沸水冲泡咖啡/茶叶冲泡后的水倒入杯子添加糖和牛奶/柠檬 class CoffeineBeverage { public:void prepareRecipe(){boilWater();brew();pourInCup();addCondiments();}private:void boilWater(){std::cout << "Boiling w…

Matlab图像处理-加法运算

加法运算 图像加法运算的一个应用是将一幅图像的内容叠加到另一幅图像上&#xff0c;生成叠加图像效果&#xff0c;或给图像中每个像素叠加常数改变图像的亮度。 在MATLAB图像处理工具箱中提供的函数imadd()可实现两幅图像的相加或者一幅图像和常量的相加。 程序代码 I1 i…

C 语言编程规范 -- 华为

1. 代码总体原则 1.1 清晰第一&#xff0c;清晰性是易于维护&#xff0c;易于重构的程序必须具备的特征 代码首先是给人读的&#xff0c;好的代码应当可以像文章一样发生朗诵出来&#xff0c;“程序必须为阅读它的人而编写&#xff0c;只是顺便用于机器执行” – Harold Abel…