数据结构之单链表

数据结构就是存储数据的不同方式。

数据与数据之间的关系

数据的逻辑结构:数据元素与元素之间的关系

集合:关系平等

线性结构:元素之间一对一的关系(表(数组,链表),队列。栈。。。)

树型结构:元素之间一对多的关系(二叉树)

图形结构:元素之间多对多的关系(网状结构)

单链表:链表的头插、尾插、头删尾删、查找、更改、销毁

cs 复制代码
/*************************************************************************
	> File Name: link.c
	> Author: yas
	> Mail: rage_yas@hotmail.com
	> Created Time: Mon 18 Mar 2024 01:42:51 PM CST
 ************************************************************************/
#include "link.h"
#include<stdio.h>
link_list *create_head_link(void)
{
    link_list *head = NULL;

    head = malloc(sizeof(link_list));
    if (head == NULL)
    {
        perror("fail to malloc");
        return NULL;
    }

    head->phead = NULL;
    head->clen = 0;

    return head;
}
/* 头插 */
int push_head_node(link_list *plist, DATA_TYPE data)
{
    link_node *pnode = NULL;
    pnode = malloc(sizeof(link_node));
    if (pnode == NULL)
    {
        perror("fail to malloc");
        return -1;
    }

    pnode->data = data;
    pnode->pnext = NULL;
    
    pnode->pnext = plist->phead;
    plist->phead = pnode;

    plist->clen++;

    return 0;
}

/* 尾插 */
int push_tail_node(link_list *plist, DATA_TYPE data)
{
    link_node *pnode = NULL;

    pnode = malloc(sizeof(link_node));
    if (pnode == NULL)
    {
        perror("fail to malloc");
        return -1;
    }
    
    pnode->pnext = NULL;
    pnode->data = data;

    if (plist->phead == NULL)
    {
        plist->phead = pnode;
    }
    else
    {
        link_node *ptmp = NULL;
        ptmp = plist->phead;

        while(ptmp->pnext != NULL)
        {
            ptmp = ptmp->pnext;
        }

        ptmp->pnext = pnode;
        
    }

    plist->clen++;
    return 0;
}
/* 尾删 */
int pop_tail_node(link_list *plist)
{
    link_node *ptmp = plist->phead;
    
    if(plist->phead == NULL)    //空链表
    {
        return 0;
    }

    else if (NULL == ptmp->pnext)
    {
        free(ptmp);
        plist->phead = NULL;
    }
    else
    {
        while(ptmp->pnext->pnext != NULL)
        {
            ptmp = ptmp->pnext;
        }

        free(ptmp->pnext);      //释放尾节点
        ptmp->pnext = NULL;     //新的尾节点
    }

    plist->clen--;

    return 0;
}
/* 头删 */
int pop_head_node(link_list *plist)
{
    link_node *ptmp = plist->phead;
    if(plist->phead == NULL)    //空链表
    {
        return 0;
    }

    else if (NULL == ptmp->pnext)
    {
        free(ptmp);
        plist->phead = NULL;
    }
    else
    {
        plist->phead = ptmp->pnext;
        free(ptmp);
    }

    plist->clen--;
    return 0;
}

/* 查找链表第一个相同的数的地址 */
link_node *find_local(link_list *plist, DATA_TYPE data)
{
    link_node *ptmp = plist->phead;
    while(ptmp->pnext != NULL)
    {
        if(ptmp->data != data)
        {
            ptmp = ptmp->pnext;
        }
        else if(ptmp->data == data)
        {
            return ptmp;
        }
    }

    return NULL;
    
}
/* 更改链表数据 */
int change_link(link_list *plist, DATA_TYPE olddata, DATA_TYPE newdata)
{
    link_node *ptmp = NULL;
    ptmp = find_local(plist, olddata);
    if (ptmp == NULL)
    {
        return -1;
    }
    ptmp->data = newdata;

    return 0;
}

/* 销毁 */
int destroy_link(link_list *plist)
{

    while(plist->clen != 0)
    {
        pop_tail_node(plist);
    }

    return 0;
}
相关推荐
liangbm31 分钟前
MATLAB系列02:MATLAB基础
开发语言·数据结构·笔记·matlab·教程·工程基础·高级绘图
我要学编程(ಥ_ಥ)2 小时前
双指针算法专题(2)
数据结构·算法·leetcode
我要学编程(ಥ_ಥ)4 小时前
滑动窗口算法专题(1)
java·数据结构·算法·leetcode
大油头儿4 小时前
排序算法-冒泡排序
数据结构·算法·排序算法
真的很上进4 小时前
【Git必看系列】—— Git巨好用的神器之git stash篇
java·前端·javascript·数据结构·git·react.js
Beginner_bml4 小时前
结构体---C语言
c语言·开发语言·数据结构
LluckyYH4 小时前
代码随想录Day 46|动态规划完结,leetcode题目:647. 回文子串、516.最长回文子序列
数据结构·人工智能·算法·leetcode·动态规划
问道飞鱼5 小时前
每日学习一个数据结构-B+树
数据结构·b树·学习
DKPT6 小时前
数据结构之快速排序、堆排序概念与实现举例
java·数据结构·算法
Element_南笙6 小时前
数据结构_1、基本概念
数据结构·人工智能