当前位置: 首页 > article >正文

数据结构(单向链表)

单向链表代码

#ifndef _LINK_H_

#define _LINK_H_

typedef int DataType;

typedef struct node
{
    DataType data;
    struct node *pnext;
}Link_Node_t;


typedef struct link
{
    Link_Node_t *phead;
    int clen;
}Link_t;

extern Link_t *link_creat();
extern int push_link_head(Link_t *plink, DataType data);
extern int print_link_all(Link_t *plink);
extern int push_link_tail(Link_t *plink, DataType data);
extern int pop_link_head(Link_t *plink);
extern int pop_link_tail(Link_t *plink);
extern Link_Node_t * find_link_data(Link_t *plink, DataType data);
extern int change_link_data(Link_t *plink, DataType des, DataType src);
extern int delall_link_data(Link_t *plink);
extern Link_Node_t* med_link_data(Link_t *plink);
extern Link_Node_t* find_link_tail_k(Link_t* plink, int k);
extern int del_k_link(Link_t *plink, int k);

#endif

#include "link.h"
#include <stdio.h>
int main(void)
{
    int ret = 0;
    Link_Node_t* find = NULL;
    Link_t *plink = link_creat();
    if(plink ==  NULL)
    {
        printf("link_creat fail");
        return -1;
    }
    
    push_link_head(plink, 1);
    push_link_head(plink, 2);
    push_link_head(plink, 3);
    push_link_head(plink, 4);
    push_link_head(plink, 5);

    push_link_tail(plink, 6);
    push_link_tail(plink, 7);
    push_link_tail(plink, 8);
    push_link_tail(plink, 9);

    print_link_all(plink);

    pop_link_head(plink);
    pop_link_tail(plink);

    print_link_all(plink);

    find = find_link_data(plink, 10);
    if(NULL == find)
    {
        printf("没找到\n");
    }
    else
    {
        printf("index = %p\n", find);
    }

    ret = change_link_data(plink, 3, 50);
    if(ret == 1)
    {
        printf("没有需要修改的数据\n");
    }

    print_link_all(plink);

    //delall_link_data(plink);

    /*
    printf("清除\n");
    plink = link_creat();
    push_link_tail(plink, 6);
    print_link_all(plink);
*/

   find = med_link_data(plink);
   if(NULL == find)
   {
        printf("空链表\n");
   }
   else
   {
        printf("index = %p\n", find);
   }

   find = find_link_tail_k(plink, 4);
    if(NULL == find)
    {
        printf("没找到\n");
    }
    else
    {
        printf("index = %p\n", find);
    }

    ret = del_k_link(plink, 4);
    if(-1 == ret)
    {
        printf("不存在该节点\n");
    }

    print_link_all(plink);
    return 0;
}

#include "link.h"
#include <stdlib.h>
#include <stdio.h>

//创建头部
Link_t *link_creat()
{
    Link_t *plink = (Link_t *)malloc(sizeof(Link_t));
    if(NULL == plink)
    {
        perror("malloc fail");
        return NULL;
    }

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

    return plink;
}

//头插
int push_link_head(Link_t *plink, DataType data)
{
    Link_Node_t *pnode = (Link_Node_t *)malloc(sizeof(Link_Node_t));
    if(NULL == pnode)
    {
        perror("malloc fail");
        return -1;
    }

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

    pnode->pnext = plink->phead;
    plink->phead = pnode;

    plink->clen++;

    return 0;
}
//判空
int is_empty_link(Link_t *plink)
{
    return NULL == plink->phead;
}

//尾插
int push_link_tail(Link_t *plink, DataType data)
{
    Link_Node_t *pnode = (Link_Node_t *)malloc(sizeof(Link_Node_t));
    if(NULL == pnode)
    {
        perror("malloc fail");
        return -1;
    }
    pnode->data = data;
    pnode->pnext = NULL;


    Link_Node_t *p = plink->phead;
    if(is_empty_link(plink))
    {
        plink->phead = pnode;
    }
    else
    {
        while(p->pnext != NULL)
        {
            p = p->pnext;
        }
        p->pnext = pnode;
    }

    plink->clen++;
    
    return 0;
}

//遍历
int print_link_all(Link_t *plink)
{
    Link_Node_t *p = plink->phead;
    while(p != NULL)
    {
        printf("%d ", p->data);
        p = p->pnext;
    }
    putchar('\n');
    return 0;
}

//头删
int pop_link_head(Link_t *plink)
{
    int i = 0;
    if(is_empty_link(plink))
    {
        i = 0;    
    }
    else
    {
        Link_Node_t *p = plink->phead;
        plink->phead = p->pnext;
        p->pnext = NULL;
        free(p);
        plink->clen--;

        i = 1;
    }

    return i;
}

//尾删
int pop_link_tail(Link_t *plink)
{
    int i = 0;
    if(is_empty_link(plink))
    {
        i = 0;
    }
    else
    {
        if(plink->clen == 1)
        {
            pop_link_head(plink);
        }
        else
        {
            Link_Node_t *p = plink->phead;
            while(p->pnext->pnext != NULL)
            {
                p = p->pnext;
            }

            free(p->pnext);
            p->pnext = NULL;
            i = 1;
            plink->clen--;
        }
    }
    return i;
}
//查找对应数据的节点地址
Link_Node_t * find_link_data(Link_t *plink, DataType data)
{
    Link_Node_t *p = plink->phead;

    while(p != NULL)
    {
        if(p->data == data)
        {
            return p;
        }
        p = p->pnext;
    }

    return NULL;
}
//改变对应数据
int change_link_data(Link_t *plink, DataType des, DataType src)
{
    Link_Node_t *p = plink->phead;

    while(p != NULL)
    {
        if(p->data == des)
        {
            p->data = src;
            return 0;
        }
        p = p->pnext;
    }

    return 1;
}
//清楚
int delall_link_data(Link_t *plink)
{
    while(1)
    {
        if(is_empty_link(plink))
        {
            free(plink);
            break;
        }
        else
        {
            pop_link_head(plink);
        }
    }
    return 0;
}

Link_Node_t* med_link_data(Link_t *plink)
{
    printf("clen = %d\n", plink->clen);
    int med = (plink->clen / 2) + 1;
    int num = 1;
    Link_Node_t *p = plink->phead;

    if(plink->clen == 0)
    {
        return NULL;
    }

    while(p != NULL)
    {
        if(num == med)
        {
            printf("data = %d\n", p->data);
            return p;
        }
        p = p->pnext;
        num ++;
    }

    return NULL;
}

Link_Node_t* find_link_tail_k(Link_t* plink, int k)
{
    int num = 0;
    if(k > plink->clen)
    {
        return NULL;
    }

    int find_k = plink->clen - k;
    printf("num = %d  k = %d\n", num, k);

    Link_Node_t *p = plink->phead;

    while(p != NULL)
    {
        if(num == find_k)
        {
            printf("data = %d\n", p->data);
            return p;
        }
        ++num;
        p = p->pnext;
    }

    return NULL;
}

int del_k_link(Link_t *plink, int k)
{
    int num = 1;

    if(k > plink->clen)
    {
        return -1;
    }
    else if(k == 1)
    {
        pop_link_head(plink);
        return 0;
    }
    else if(k == plink->clen)
    {
        pop_link_tail(plink);
        return 0;
    }

    Link_Node_t* p = plink->phead;
    
    while(p != NULL)
    {
        if(num == k - 1)
        {
            Link_Node_t *p1 = p->pnext;
            p->pnext = p->pnext->pnext;
            free(p1);
        }
        ++num;
        p = p->pnext;
    }

    return 0;
}

http://www.kler.cn/news/293976.html

相关文章:

  • 软文发稿相比其他广告形式有哪些持续性优势?
  • 如何从硬盘恢复已删除/丢失的文件?硬盘恢复已删除的文件技巧
  • 如何录制黑神话悟空的游戏BGM导入iPhone手机制作铃声?
  • notepad下载安装使用以及高级使用技巧
  • Vue 中 nextTick 的最主要作用是什么,为什么要有这个 API
  • spring项目使用邮箱验证码校验
  • Vue3状态管理Pinia
  • APS开源源码解读: 排程工具 optaplanner
  • PHP批量修改MySQL数据表字符集为utf8mb4/utf8mb4_unicode_ci
  • 全网首发!!!opencv三通道Mat点云转halcon点云—HTuple类型
  • linux编译出现报错
  • ★ 算法OJ题 ★ 力扣3 - 无重复字符的最长子串
  • 百家云 BRTC:革新华为 HarmonyOS NEXT 系统的实时通信体验
  • ctfshow-php特性(web123-web150plus)
  • 安卓玩机工具-----ADB方式的刷机玩机工具“秋之盒”’ 测试各项功能预览
  • SpinalHDL之数据类型(一)
  • 【LeetCode】11.盛最多水的容器
  • UE4_后期处理_后期处理材质及后期处理体积三—遮挡物体描边显示
  • 网络安全与恶意攻击:如何应对?
  • 2024 年高教社杯全国大学生数学建模竞赛 C 题 农作物的种植策略 完整参考论文
  • Jenkins+Svn+Vue自动化构建部署前端项目(保姆级图文教程)
  • [数据集][目标检测]智慧农业草莓叶子病虫害检测数据集VOC+YOLO格式4040张9类别
  • 仿论坛项目--Kafka,构建TB级异步消息系统
  • IOS 20 发现界面(UITableView)歌单列表(UICollectionView)实现
  • 51单片机-第十二节-LCD1602液晶显示屏
  • MyBatis-Plus 框架 QueryWrapper UpdateWrapper 方法修复sql注入漏洞事件
  • 2024社区版IDEA springboot日志输出颜色
  • Excel数据导入MySQL数据库的完整指南
  • 4.6 Sensors -- useMouse
  • EmguCV学习笔记 C# 10.2 人脸识别 FaceRecgnizer类