关于数据结构单向链表的各种操作

 更新时间:2023年04月12日 10:38:40   作者:朦胧的雨梦  
这篇文章主要介绍了关于数据结构单向链表的各种操作,关于数据结构链表的操作一般涉及的就是增删改查,下面将关于无空头链表展开介绍,需要的朋友可以参考下

关于节点数据添加:

尾添加

最核心的是定义一个头指针和一个尾指针(尾指针可以不定义但是会增加代码的重复性,增加程序运行时间);

关于尾添加:(注意区分有节点和无节点的情况)

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    endadd(&phead,&pend,4);
    ......
 
 
    return 0;
}
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata)
 
{
  
    struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
    if(NULL == pt)
        return;
    pt->data = adddata;
    pt->pnext = NULL;
    if(NULL == *phead)
    {
       *phead = pt;
 
    }
    else
 
    {
        (*pend)->pnext = pt;
    }
    *pend= pt;
}

头添加

关于代码思路与尾添加基本一致,注意区分节点的链接:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    head_add(&phead,&pend,4);
    ......
 
 
    return 0;
}
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata)
 
{
    
    struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
    if(NULL == pt)
        return;
    pt->data = adddata;
    pt->pnext = NULL;
    if(NULL == *phead)
    {
       *pend = pt;
 
    }
    else
 
    {
        pt->pnext = (*phead);
    }
    *phead= pt;
}

一次性添加n个x数据节点:

利用循坏,直接调用头添加或者尾添加:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    circulate_add(&phead,&pend,4,5);
    ......
 
 
    return 0;
}
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int count, int adddata);
 
{
    for(int i = 0;i<count;i++)
    {
        endadd(phead, pend, adddata);
       
    }
 
 
 
}

关于查找:

根据指定数据:

核心就是通过头指针一个一个往下走找到指定节点的数据与所找数据是否匹配,最重要的是要使用中间变量记录头指针,否则就无法找到头指针了(因为是单项链表):

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void data_find(struct Mystruct *phead, int designated_data);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    middle_data_find(phead,4);
    ......
 
 
    return 0;
}
void data_find(struct Mystruct* phead, int designated_data)
{
    if (NULL == phead)
        return;
    struct Mystruct* ptemp = phead;
    while (ptemp != NULL)
    {
        if (ptemp->data == designated_data)
        {
            printf("找到了");
            break;
 
        }
        ptemp = ptemp->pnext;
    }
    return;
 
}

根据下标查找:

思路基本不变;区别传入指定下标;内部定义一个计数器,当下标和计数器数值相等;表示链表存在这个节点;可以选择传出或者提醒;大家思考一下,动手实践一下。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
struct Mystruct *index_find(struct Mystruct *phead, int index);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    middle_data_find(phead,4);
    ......
 
 
    return 0;
}
struct Mystruct* index_find(struct Mystruct* phead, int index)
 
{
    if (NULL == phead||index<0)
        return NULL;
 
    struct Mystruct* ptemp = phead;
    int i = 0;
    for (i = 0; i < index; i++)
    {
        ptemp = ptemp->pnext;
    }
    return ptemp;
 
}

删除头节点:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_head(struct Mystruct **phead,struct Mystruct **pend);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_head(struct Mystruct** phead, struct Mystruct** pend)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if ((*phead)->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
    else
    {
        *phead = (*phead)->pnext;
        free(pt);
    }
 
 
 
 
}
void deleat_end(struct My

删除尾节点:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_end(struct Mystruct**phead,struct Mystruct**pend);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_end(struct Mystruct** phead, struct Mystruct** pend)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if (pt->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
    else
    {
        while (pt->pnext != (*pend))
        {
            if (pt->pnext == (*pend))
            {
                free(*pend);
                *pend = pt;
                pt->pnext = NULL;
                pt = pt->pnext;
            }
           
 
        }
 
    }
    
  
 
 
}

删除中间节点:

这里思路改变一下:根据数据或者下标找到前一个节点,改变前一个节点的pnext指针的指向,直接指向下一个节点,也就是这个节点的pnext;简单示范一下删除中间指定数据:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if (pt->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
}

删除全部节点:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_all(&phead,&pend)
    ......
 
 
    return 0;
}
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
{
 
 
    while (*phead!= NULL)
    {
        struct Mystruct* pt = *phead;
        *phead = (*phead)->pnext;
        free(pt);
 
 
    }
    *phead = NULL;
    *pend = NULL;
 
 
 
}

到此这篇关于关于数据结构单向链表的各种操作的文章就介绍到这了,更多相关数据结构单向链表操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Matlab实现生成箭头坐标轴详解

    Matlab实现生成箭头坐标轴详解

    这篇文章主要介绍了如何利用Matlab实现生成箭头坐标轴,为坐标轴增添箭头,文中的示例代码讲解详细,对我们学习Matlab有一定帮助,需要的可以参考一下
    2022-03-03
  • C++之默认参数详解

    C++之默认参数详解

    这篇文章主要介绍了C++的默认参数,是C++入门学习中的基础知识,需要的朋友可以参考下,希望能够给你带来帮助
    2021-11-11
  • C语言深入探究自定义类型之结构体与枚举及联合

    C语言深入探究自定义类型之结构体与枚举及联合

    今天我们来学习一下自定义类型,自定义类型包括结构体、枚举、联合体,小编觉得挺不错的,现在就分享给大家,也给大家做个参考
    2022-05-05
  •  C++ new 和 delete 关键字详解

     C++ new 和 delete 关键字详解

    这篇文章主要介绍了 C++ new 和 delete 关键字详解,文章围绕主题展开new 和 delete 的使用方法的介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-06-06
  • C/C++ Zlib库封装MyZip压缩类的详细过程

    C/C++ Zlib库封装MyZip压缩类的详细过程

    在软件开发中,文件的压缩和解压缩是一项常见的任务,而ZIP是一种被广泛应用的压缩格式,本文将聚焦于一个简化的C++实现,通过分析代码,我们将深入了解其设计和实现细节,感兴趣的朋友一起看看吧
    2023-11-11
  • c++ 实现文件逐行读取与字符匹配

    c++ 实现文件逐行读取与字符匹配

    这里尝试通过C++来实现一个文件IO的功能,看看是否能够比python的表现更好一些,感兴趣的朋友可以参考下
    2021-05-05
  • C语言中如何利用循环嵌套输出一个菱形

    C语言中如何利用循环嵌套输出一个菱形

    这篇文章主要介绍了C语言中如何利用循环嵌套输出一个菱形问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • 一篇文章带你了解C语言指针进阶

    一篇文章带你了解C语言指针进阶

    这篇文章主要介绍了C语言指针详解及用法示例,介绍了其相关概念,然后分享了几种用法,具有一定参考价值。需要的朋友可以了解下
    2021-09-09
  • c语言基于stdarg.h的可变参数函数的用法

    c语言基于stdarg.h的可变参数函数的用法

    本篇文章主要介绍了c语言基于stdarg.h的可变参数函数的用法,详细的介绍了可变参数函数的详细用法和源码实例,有兴趣的可以了解一下
    2017-07-07
  • C++ vector在多线程操作中出现内存错误问题及解决

    C++ vector在多线程操作中出现内存错误问题及解决

    这篇文章主要介绍了C++ vector在多线程操作中出现内存错误问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08

最新评论