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

手撕单链表

目录

链表的概念和结构

单链表的实现

申请新结点

打印

尾插

头插

 尾删

头删

 ​编辑

 查找

在pos位置前插入元素 

 在pos位置后插入元素

删除pos位置的元素 

 删除pos位置之后的位置的元素​编辑

完整代码

SListNode.h

 

SListNode.c


链表的概念和结构

链表是一种物理存储上非连续,非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的

链式结构逻辑连续,物理不一定连续

单链表的实现

无头 单向 非循环链表

申请新结点

打印

尾插

 

头插

 

 尾删

 

头删

 

 

 查找

在pos位置前插入元素 

 ​​​​

 

 在pos位置后插入元素

删除pos位置的元素 

 

 删除pos位置之后的位置的元素

完整代码

SListNode.h

 

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>typedef int SLTDataType;typedef struct SListNode
{SLTDataType data;struct SListNode* next;
}SLTNode;SLTNode* BuySListNode(SLTDataType x);     //申请一个结点void SListNodePrint(SLTNode* plist);      //打印void SListPushBack(SLTNode** pplist, SLTDataType x);    //尾插void SListPushfront(SLTNode** pplist, SLTDataType x);    //头插void SListPopBack(SLTNode** pplist);    //尾删void SListPopfront(SLTNode** pplist);    //头删SLTNode* SListFind(SLTNode* plist, SLTDataType x);        //查找void SListInsert(SLTNode** pplist, SLTNode* pos, SLTDataType x);    //在pos位置前插入元素void SListInsertAfter(SLTNode* pos, SLTDataType x);   //在pos位置后插入元素void SListErase(SLTNode** pplist, SLTNode* pos);     //删除pos位置前的元素void SListEraseAfter(SLTNode* pos);          //删除pos位置之后的位置的元素

SListNode.c

#define _CRT_SECURE_NO_WARNINGS 1#include "SListNode.h"SLTNode* BuySListNode(SLTDataType x)            //申请一个新结点
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));if (newnode == NULL){perror("malloc failed");exit(-1);}newnode->data = x;newnode->next = NULL;return newnode;
}void SListNodePrint(SLTNode* plist)      //打印
{SLTNode* cur = plist;while (cur){printf(" %d ->", cur->data);cur = cur->next;}printf("NULL");printf("\n");
}void SListPushBack(SLTNode** pplist, SLTDataType x)    //尾插
{assert(pplist);SLTNode* newnode = BuySListNode(x);if (*pplist == NULL){*pplist = newnode;}else{SLTNode* tail = *pplist;while (tail->next != NULL){tail = tail->next;}tail->next = newnode;}
}void SListPushfront(SLTNode** pplist, SLTDataType x)    //头插
{assert(pplist);SLTNode* newnode = BuySListNode(x);newnode->next = *pplist;*pplist = newnode;}void SListPopBack(SLTNode** pplist)    //尾删
{assert(pplist);assert(*pplist);//空链表//一个结点if ((*pplist)->next == NULL){free(*pplist);*pplist = NULL;}//一个以上结点else{SLTNode* tail = *pplist;while (tail->next->next != NULL){tail = tail->next;}free(tail->next);tail->next = NULL;}
}void SListPopfront(SLTNode** pplist)    //头删
{assert(pplist);assert(*pplist);SLTNode* cur = *pplist;*pplist = (*pplist)->next;free(cur);
}SLTNode* SListFind(SLTNode* plist, SLTDataType x)        //查找
{SLTNode* cur = plist;while (cur){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}void SListInsert(SLTNode** pplist, SLTNode* pos, SLTDataType x)    //在pos位置前插入元素
{assert(pplist);assert(pos);if (*pplist == pos){SListPopfront(pplist, x);}else{SLTNode* cur = *pplist;while (cur->next != pos){cur = cur->next;}SLTNode* newnode = BuySListNode(x);newnode->next = cur->next;cur->next = newnode;}
}void SListInsertAfter(SLTNode* pos, SLTDataType x)   //在pos位置后插入元素
{assert(pos);SLTNode* newnode = BuySListNode(x);newnode->next = pos->next;pos->next = newnode;
}void SListErase(SLTNode** pplist, SLTNode* pos)     //删除pos位置的元素
{assert(pplist);assert(pos);if (*pplist == pos){SListPopfront(pplist);}else{SLTNode* cur = *pplist;while (cur->next->next = pos){cur = cur->next;}cur->next = pos->next;free(pos);pos = NULL;}
}void SListEraseAfter(SLTNode* pos)          //删除pos位置之后的位置的元素
{assert(pos);assert(pos->next);SLTNode* cur = pos->next;pos->next = cur->next;free(cur);cur = NULL;
}

http://www.lryc.cn/news/127716.html

相关文章:

  • Spring-aop特点,专业术语及案例演示
  • 探秘Java的Map集合:键值映射的奇妙世界
  • git权限问题解决方法Access denied fatal: Authentication failed
  • Hands on RL 之 Off-policy Maximum Entropy Actor-Critic (SAC)
  • JavaScript中的this指向,call、apply、bind的简单实现
  • Linux学习之基本指令一
  • appium默认60秒关闭应用的问题
  • Docker 容器内无法使用vim命令 解决方法
  • Django的简介安装与配置及两大设计模式
  • Mybatis分页插件——PageHelper
  • k8s认证详解 k8s证书详解 2023推荐
  • php初解
  • 【C语言】回调函数,qsort排序函数的使用和自己实现,超详解
  • PHP手术麻醉系统源码,自动生成麻醉和护理医疗文书
  • 内网穿透实战应用——【通过cpolar分享本地电脑上有趣的照片:发布piwigo网页】
  • iPhone删除的照片能恢复吗?不小心误删了照片怎么找回?
  • LeetCode--HOT100题(32)
  • SAP MM学习笔记24-以评估收货(评价)和非评估收货(非评价)
  • Hadoop的DataNode无法启动的解决方案
  • re中的match和search有什么区别?
  • 《内网穿透》无需公网IP,公网SSH远程访问家中的树莓派
  • .net连接mysql,提示找不到请求的 .Net Framework Data Provider。可能没有安装
  • 销售自动化管理软件是什么,销售自动化管理软件有什么优势
  • MySQL 函数
  • 爬虫逆向实战(六)--猿人学第四题
  • 【大数据Hive】hive 事务表使用详解
  • 网络层协议
  • JWT(JSON Web Token )令牌
  • leetcode 力扣刷题 滑动窗口 部分题解(记录)
  • Intellij IDEA SBT依赖分析插件