单链表(不带头结点)

article/2025/11/9 6:42:11

单链表不带头结点结构体设计:

//不带头结点的结构体设计:
typedef int ELEM_TYPE;//有效数据节点结构体设计
typedef struct Node
{ELEM_TYPE data;//数据域   (1.头结点:不保存任何数据    2.有效数据节点:保存有效值)   struct Node* next;//指针域  (1.头结点:保存第一个元素的地址    2.有效数据节点:保存下一个有效元素的地址)
}Node, * PNode;

老规矩,实现链表的基本功能:增删改查

函数声明如下:

/初始化函数(对于头结点进行赋初值)
void no_head_Init_list(struct Node** phead);//struct Node ** == PNode *//购买一个新节点
struct Node* no_head_BuyNode(ELEM_TYPE val);//头插
bool no_head_Insert_head(PNode* phead, ELEM_TYPE val);//尾插
bool no_head_Insert_tail(struct Node** phead, ELEM_TYPE val);//按位置插入(pos=0 相当于头插  pos==length 相当于尾插)
bool no_head_Insert_pos(struct Node** phead, int pos, ELEM_TYPE val);//头删
bool no_head_Del_head(struct Node** phead);//尾删
bool no_head_Del_tail(struct Node** phead);//按位置删(pos==0 相当于头删   pos==length-1 相当于尾删(pos==length非法))
bool no_head_Del_pos(struct Node** phead, int pos);
//按值删
bool no_head_Del_val(struct Node** phead, ELEM_TYPE val);//获取值位置  (如果val值存在, 则返回其地址  不然返回NULL)
struct Node* no_head_Search(struct Node** phead, ELEM_TYPE val);//判空
bool no_head_IsEmpty(struct Node** phead);//判满  单链表不存在满这个概念//获取单链表有效数据节点个数
int no_head_Get_length(struct Node** phead);//清空  相当于直接调用销毁
void no_head_Clear(struct Node** phead);//销毁1(malloc申请来的空间 全部释放掉)
void no_head_Destroy(struct Node** phead);//销毁2
void no_head_Destroy2(struct Node** phead);//打印
void no_head_Show(struct Node** phead);

对于函数括号里面的参数为什么是二级指针呢?由于该单链表没有头结点,所以我们需要一个指针来指向第一个有效节点,那么为了能使函数内部的改变能够影响外部数据,所以需要传参数时传入二级指针。

 1.初始化

void no_head_Init_list(struct Node** phead)//struct Node ** == PNode *
{//assert  phead!=NULLassert(phead != NULL);*phead = NULL;//修改一个指针,如果没有有效节点的话,将其指针指向NULL//这个指针保存第一个有效数据节点的地址   phead   *phead(保存的第一个有效数据的地址)   (*phead).next   
}

2.申请一个新节点便于插入

struct Node* no_head_BuyNode(ELEM_TYPE val)
{struct Node* pnewnode = (struct Node*)malloc(1 * sizeof(struct Node));assert(pnewnode != NULL);if (pnewnode == NULL){return NULL;}pnewnode->data = val;pnewnode->next = NULL;return pnewnode;
}

3.头插

bool no_head_Insert_head(PNode* phead, ELEM_TYPE val)
{//assertassert(phead != NULL);if (NULL == phead){return false;}//1.创建新节点struct Node* pnewnode = (struct Node*)malloc(1 * sizeof(struct Node));assert(pnewnode != NULL);pnewnode->data = val;pnewnode->next = NULL;//2.找到合适的插入位置 (头插函数 已经找到插入位置)//3.插入pnewnode->next = *phead;*phead = pnewnode;return true;
}

4.尾插

bool no_head_Insert_tail(struct Node** phead, ELEM_TYPE val)
{assert(phead != NULL);if (no_head_IsEmpty(phead))//如果我是一个空链,则执行头插函数 不要执行尾插函数{return no_head_Insert_head(phead, val);}//1.创建新节点PNode pnewnode = (PNode)malloc(sizeof(Node) * 1);assert(pnewnode != NULL);pnewnode->data = val;pnewnode->next = NULL;//2.找到合适的插入位置PNode p = *phead;for (p; p->next != NULL; p = p->next);//这里存在一个bug:如果是空链的话,*phead == p == NULL,那么语句2:p->next会报写入异常(在上面处理一下这个bug即可)//此时 指针p停留在尾结点,而不是尾结点的下一个节点p->next = pnewnode;return true;
}

5.按位置插

//按位置插入(pos=0 相当于头插  pos==length 相当于尾插)
bool no_head_Insert_pos(struct Node** phead, int pos, ELEM_TYPE val)
{assert(phead != NULL);assert(pos >= 0 && pos <= no_head_Get_length(phead));//pos == length合法  相当于尾插 但是这个位置不能用于删除if (pos == 0){return no_head_Insert_head(phead, val);}//1.购买新节点PNode pnewnode = no_head_BuyNode(val);assert(pnewnode != NULL);//2.找到合适的位置PNode p = *phead;for (int i = 1; i < pos; ++i)//这里存在一个bug:如果pos = 0,p没有办法跑到合适的位置,提前处理掉即可{p = p->next;}//3.插入pnewnode->next = p->next;p->next = pnewnode;return true;
}

6,。头删

bool no_head_Del_head(struct Node** phead)
{assert(phead != NULL);if (no_head_IsEmpty(phead))//如果是空链,返回假{return false;}struct Node* p = *phead;*phead = p->next;free(p);return true;
}

7.尾删(不带头结点的单链表尾删较为麻烦,会有两种风险)

bool no_head_Del_tail(struct Node** phead)
{assert(phead != NULL);//两种风险   第一种:q有可能为NULL(由于空链导致)if (no_head_IsEmpty(phead)){return false;}//什么情况下:q不为NULL,但是q->next为NULL?  //有节点,但是仅有一个节点//两种风险   第二种:q->next有可能为NULL(由于导致)if ((*phead)->next == NULL) //仅有一个首元素节点{return no_head_Del_head(phead);//仅有一个节点,尾删和头删没区别}PNode q = *phead;for (q; q->next->next != NULL; q = q->next);//q向后一次项探测两步(有bug风险:1.p可能为NULL  2.p->next为NULL)//此时q停在倒数第二个节点(尾删的待删除节点的上一个节点)struct Node* p = q->next;//p通过q来直接指向待删除节点q->next = p->next;free(p);return true;
}

8.按位置删

//按位置删(pos==0 相当于头删   pos==length-1 相当于尾删(pos==length非法))
bool no_head_Del_pos(struct Node** phead, int pos)
{assert(phead != NULL);assert(pos >= 0 && pos < no_head_Get_length(phead));if (no_head_IsEmpty(phead))//判空{return false;}if (pos == 0){return no_head_Del_head(phead);}struct Node* q = *phead;for (int i = 1; i < pos; i++){q = q->next;}struct Node* p = q->next;q->next = p->next;free(p);return true;
}

9.按值删

bool no_head_Del_val(struct Node** phead, ELEM_TYPE val)
{assert(phead != NULL);if ((*phead)->data == val)//判断首元素的值  如果  == val{return no_head_Del_head(phead);}PNode p = no_head_Search(phead,val);if (p == NULL){return false;}PNode q = *phead;for (q; q->next != p; q = q->next);;//存在一个bug:如果要删除的值就是首元素的值,这时,q没有合适的位置去指向,所以在开头加上处理//此时  p指向待删除节点//q应该指向p的上一个节点q->next = p->next;free(p);return true;
}

10.获取值得位置

//获取值位置  (如果val值存在, 则返回其地址  不然返回NULL)
struct Node* no_head_Search(struct Node** phead, ELEM_TYPE val)
{assert(phead != NULL);for (struct Node* p = *phead; p != NULL; p = p->next){if (p->data == val){return p;}}return NULL;
}

11.判空

12.获取单链表有效节点的个数

//判空
bool no_head_IsEmpty(struct Node** phead)
{assert(phead != NULL);return (*phead) == NULL;
}//判满  单链表不存在满这个概念//获取单链表有效数据节点个数
int no_head_Get_length(struct Node** phead)
{assert(phead != NULL);int cout = 0;for (PNode p = *phead; p != NULL; p = p->next){cout++;}return cout;
}

13.清空、销毁、打印(销毁给出两种方法)

//清空  相当于直接调用销毁
void no_head_Clear(struct Node** phead)
{no_head_Destroy(phead);
}//销毁1(malloc申请来的空间 全部释放掉)
void no_head_Destroy(struct Node** phead)
{assert(phead != NULL);while (*phead != NULL){PNode p = (*phead);*phead = p->next;free(p);}*phead = NULL;
}//销毁2
void no_head_Destroy2(struct Node** phead)
{assert(phead != NULL);struct Node* p = *phead;struct Node* q;*phead = NULL;while (p != NULL){q = p->next;free(p);p = q;}
}//打印
void no_head_Show(struct Node** phead)
{assert(phead != NULL);for (PNode p = *phead; p != NULL; p = p->next){printf("%d ", p->data);}printf("\n");
}

接下来在主函数里测试一下

int main()
{struct Node* head;//头指针no_head_Init_list(&head);//初始化for (int i = 0; i < 20; i++){no_head_Insert_pos(&head, i, i + 1);}//将1到20分别按位置插入no_head_Show(&head);//打印no_head_Insert_head(&head, 100);//头插100no_head_Insert_tail(&head, 200);//尾插200no_head_Show(&head);//打印printf("length = %d\n", no_head_Get_length(&head));//打印单链表有效数据节点的长度no_head_Del_head(&head);//头删no_head_Del_tail(&head);//尾删no_head_Show(&head);//打印no_head_Del_pos(&head, 4);//将第4个有效节点删除no_head_Del_val(&head, 14);//将数据为14的节点删除no_head_Show(&head);//打印printf("length = %d\n", no_head_Get_length(&head));//打印单链表有效数据节点的长度//Destroy(&head);no_head_Destroy2(&head);//销毁//printf("%d\n", sizeof(struct Node));return 0;
}

结果:

 


http://chatgpt.dhexx.cn/article/21RtOg20.shtml

相关文章

数据结构之不带头结点的单链表

我们都知道不管是单链表、双向链表还是循环链表&#xff0c;都带有头结点&#xff0c;这个头结点相当于一个起始的位置。我们在设计带头结点的单链表的时候&#xff0c;我们会在主函数中设计一个头结点&#xff0c;并把它的指针域置为空&#xff0c;这样我们就可以进行增删查改…

数据结构--带头结点的单链表

单链表分为&#xff1a;带头结点和不带头结点&#xff0c;不带头结点的单链表需要用到二级指针&#xff0c;容易出错。 1、结构体设计 typedef int ELEM_TYPE; //有效数据节点结构体设计&#xff08;头结点借用&#xff09; typedef struct Node {ELEM_TYPE data;//数据域 &…

带头结点的单链表的总结

线性表的链式存储是用若干地址分散的存储单元存储数据元素&#xff0c;逻辑上相邻的数据元素在物理位置上不一定相邻。 带头结点的单链表是指&#xff0c;在单链表的第一个结点之前增加一个特殊的结点&#xff0c;称为头结点。 头结点的作用&#xff1a;使所有链表&#xff08;…

单链表的创建(带头结点和不带头结点)

伪代码&#xff1a; 创建结点 创建头结点(单独定义一个结构体来保存单链表的首地址和尾地址还有链表的长度) 创建带头结点的单链表 注意&#xff1a;创建头结点中的首尾指针都要指空&#xff0c;长度等于0&#xff1b; 从终端接收数据 创建结点保存数据&#xff1a; 创建的节点…

单链表———带头结点跟不带头结点的区别

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、单链表&#xff1a;带头结点跟不带头结点二、使用步骤总结 前言 数据结构中单链表的创建 带头结点跟不带头结点的区别 一、单链表&#xff1a;带头结点跟…

【数据结构】带头结点的单链表

文章目录 一、单链表的概念二、结构体声明&#xff1a;三、函数1.购买节点2.释放节点3.单链表的初始化4.判空函数5.获取单链表有效值个数6.按数据查询&#xff08;返回含有此数据节点的前驱&#xff09;7.按数据查询&#xff08;返回含有当前数据的节点&#xff09;8.按pos位置…

【考研】分清带头结点和不带头结点的单链表

CSDN话题挑战赛第2期 参赛话题&#xff1a;学习笔记 前言 为分清带结点与不带头结点的单链表操作&#xff0c;本文以图文和表格形式描述了两者之间的区别。考研中&#xff0c;数据结构的单链表操作是重要考点&#xff0c;其中&#xff0c;比较常考带头结点的链表操作。 所以&…

【带头结点的单链表】

带头结点的单链表 前言一、带头结点的单链表结构体设计1. 带头结点的单链表2. 结构体声明 二、函数实现1. 初始化2. 申请新节点3. 头插4. 尾插5. 按位置插入6. 头删7. 尾删8. 销毁 总结 前言 单链表的概念&#xff1a; 单链表是一种链式存取的数据结构&#xff0c;用一组地址…

带头结点单链表 (详解)

单链表结构体 结构体后的*List是一个指向结构体的指针类型&#xff0c;我们通过它来定义该类型的指针。 如&#xff1a;List p ;  则这个p就是指向LinkedList结构体的一个指针&#xff0c;也就是单链表的头指针。&#xff08;所以说头指针是必然存在的&#xff0c;但单链表不…

数据结构-带头节点的单链表(C语言)超详细讲解

前面我们学到线性表的顺序存储结构&#xff08;顺序表&#xff09;&#xff0c;发现它有着明显的缺点&#xff1a;插入和删除元素时需要频繁的移动元素&#xff0c;运算效率低。必须按事先估计的最大元素个数申请连续的存储空间。存储空间估计大了&#xff0c;造成浪费空间&…

Windows配置端口转发绕过samba 445端口限制共享linux磁盘

概述 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件。SMB&#xff08;Server Messages Block&#xff0c;信息服务块&#xff09;是一种在局域网上共享文件和打印机的一种通信协议&#xff0c;它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议…

Linux安装samba服务

个人推荐: &#x1f4e2;&#x1f4e2;&#x1f4e2; 前些天发现了一个蛮有意思的人工智能学习网站,8个字形容一下 "通俗易懂&#xff0c;风趣幽默"&#xff0c;感觉非常有意思,忍不住分享一下给大家。点击跳转到教程。 一&#xff1a;使用到的Linux指令 1:检查是否…

samba服务

目录 一&#xff1a;samba概述 1.1samba简介 1.2samba的监听端口 1.3samba的进程 1.4samba安全级别 二&#xff1a;samba服务的特点 三&#xff1a;samba的主要作用 四&#xff1a;常见文件服务器软件的对比 五&#xff1a;samba配置文件 5.1samba主配置文件 5.2常用…

Samba配置详解

一、简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件&#xff0c;而SMB是Server Message Block的缩写&#xff0c;即为服务器消息块 &#xff0c;SMB主要是作为Microsoft的网络通讯协议&#xff0c;后来Samba将SMB通信协议应用到了Linux系统上&#xff0c;就形成…

Windows 10 下修改 smb 连接的默认端口(445)

服务器&#xff08;samba 共享文件夹所在服务器&#xff09;上已经frp外网映射445 端口转接4455 windows10 右键开始–powershell&#xff08;管理员&#xff09; netsh interface portproxy add v4tov4 listenport445 listenaddress127.0.0.1 connectport4455 connectaddress …

Samba配置与使用

1.安装Samba Centos7系统 yum -y install samba2. 查看安装情况(可选) rpm -qa |grep samba3.设置开机自启 systemctl enable smb.service systemctl enable nmb.service4.启动服务 //启动服务 systemctl start smb.service //查看启动服务的状态 systemctl status smb.se…

Linux——samba服务器配置

实验目的&#xff1a;实现Linux的主机与windows系列主机进行文件的传输 实验环境&#xff08;保证两台主机能够相互访问&#xff09;&#xff1a; 1、Windows7&#xff1a;客户端 2、centos7&#xff1a; 服务端&#xff08;文件共享&#xff09; 实验案例&#xff1a; J…

samba服务2---安装与端口

具体说明 一、安装二、samba的守护进程三、启动samba服务 一、安装 安装简单&#xff0c;centos 8 &#xff1a;dnf -y install samba samba-client就可以了 主要安装的包是这些&#xff0c;我们一一来说。 samba # 主服务包 samba-common # 通用工具包 samba-common-libs # …

在windows系统中映射网络驱动器时,如何通过非455端口远程访问Linux服务器的Samba服务

声明 通常情况下&#xff0c;在windows中通过Linux服务器的Samba服务去映射网络驱动器时候&#xff0c;一般默认就是直接填入内网Linux服务器ip即可&#xff0c;它会默认445端口。若是我的windows并不和Linux服务器在一个网段时&#xff0c;该如何操作呢&#xff1f; 1 用管理…

Winows通过非445端口远程访问公网Samba

一、用管理员账号打开CMD&#xff0c;以下命令查询445端口占用情况 输入命令&#xff1a; netstat -ano | findstr 445可以看出是pid为4的进程进行监听&#xff0c;从任务管理器上看是名叫System&#xff0c;备注"NT Kernel&System"。 二、禁用该服务 本地 …