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

数据结构---单链表实现

单链表是什么

我的理解是“特殊的数组”,通过访问地址来连接起来

1怎么创建链表 ----通过结构体(成员有存入数据的data和指向下一个节点的地址的指针(结构体指针)next

初始架构---DataType 对应存入数据类型,此处的Node==struct node *

//头文件
#include<stdio.h>
//宏定义
#define DataType int //全局变量//结构体
typedef struct node
{DataType data;struct node* next;
}node, * Node;int main()
{return 0;
}

2初始化链表+尾插+打印

Node Init(Node phead)
{phead = (Node)malloc(sizeof(node));if (phead == -1)return -1;phead->next = NULL;
}int PushBack(Node phead, DataType num)
{//创建新节点Node newnode = malloc(sizeof(node));newnode->next = NULL;newnode->data = num;//创建指针找到链表的尾,然后插入,Node p = NULL;//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了for (p = phead; p->next != NULL; p = p->next);p->next = newnode;return 0;
}void show_list(Node phead)
{Node cur = NULL;cur = phead;//判断是否为空if (phead == NULL){printf("NULL\n");}//遍历头节点没有数据所有从头节点下一个数据开始打印for (cur = phead->next; cur != NULL; cur = cur->next){printf("%d->", cur->data);}printf("NULL\n");
}

现象:


//头文件
#include<stdio.h>
#include<stdlib.h>//宏定义
#define DataType int 
//全局变量//结构体
typedef struct node
{DataType data;struct node* next;
}node, * Node;Node Init(Node phead)
{phead = (Node)malloc(sizeof(node));if (phead == -1)return -1;phead->next = NULL;
}int PushBack(Node phead, DataType num)
{//创建新节点Node newnode = malloc(sizeof(node));newnode->next = NULL;newnode->data = num;//创建指针找到链表的尾,然后插入,Node p = NULL;//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了for (p = phead; p->next != NULL; p = p->next);p->next = newnode;return 0;
}void show_list(Node phead)
{Node cur = NULL;cur = phead;//判断是否为空if (phead == NULL){printf("NULL\n");}//遍历头节点没有数据所有从头节点下一个数据开始打印for (cur = phead->next; cur != NULL; cur = cur->next){printf("%d->", cur->data);}printf("NULL\n");
}int main()
{Node phead = NULL;phead = Init(phead);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);show_list(phead);return 0;
}

3添加删除和修改的功能 

删除时利用两个指针一个找一个删,再指向

修改就是在查找的基础上再加一个if判断

#define _CRT_SECURE_NO_WARNINGS
//头文件
#include<stdio.h>
#include<stdlib.h>//宏定义
#define DataType int 
//全局变量//结构体
typedef struct node
{DataType data;struct node* next;
}node, * Node;Node Init(Node phead)
{phead = (Node)malloc(sizeof(node));if (phead == NULL)return -1;phead->next = NULL;
}int PushBack(Node phead, DataType num)
{//创建新节点Node newnode = malloc(sizeof(node));newnode->next = NULL;newnode->data = num;//创建指针找到链表的尾,然后插入,Node p = NULL;//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了for (p = phead; p->next != NULL; p = p->next);p->next = newnode;return 0;
}void show_list(Node phead)
{Node cur = NULL;cur = phead;//判断是否为空if (phead == NULL){printf("NULL\n");}//遍历头节点没有数据所有从头节点下一个数据开始打印for (cur = phead->next; cur != NULL; cur = cur->next){printf("%d->", cur->data);}printf("NULL\n");
}
int Delect(Node phead,DataType num)
{if (phead == NULL){printf("peahd is NULL\n");return -1;}Node cur1= NULL;Node cur2 = NULL;for (cur1 = phead, cur2 = phead->next; cur1->next != NULL; cur1 = cur2, cur2 = cur2->next){if (cur2->data == num){cur1->next = cur2->next;free(cur2);return 0;}}printf("no find num\n");return -1;
}int Change(Node phead, int num1, int num2)
{if (phead == NULL){printf("peahd is NULL\n");return -1;}Node cur = NULL;for (cur = phead->next;cur != NULL; cur = cur->next){if (cur->data == num1){cur->data = num2;return 0;}}printf("no find num1\n");return -1;
}
int main()
{Node phead = NULL;phead = Init(phead);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);Change(phead, 1, 2);show_list(phead);Delect(phead, 1);show_list(phead);Delect(phead, 1);Delect(phead, 1);Delect(phead, 1);show_list(phead);Delect(phead, 1);show_list(phead);Change(phead, 1, 2);show_list(phead);return 0;
}

4释放---释放后链表为空,不会显示后面功能


void Releas(Node phead)
{Node cur1 = NULL;Node cur2 = NULL;for (cur1 = cur2 = phead; cur1->next != NULL; cur1 = cur2){cur2 = cur1->next;free(cur1);}
}
int main()
{Node phead = NULL;phead = Init(phead);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);Change(phead, 1, 2);Releas(phead);show_list(phead);Delect(phead, 1);show_list(phead);Delect(phead, 1);Delect(phead, 1);Delect(phead, 1);show_list(phead);Delect(phead, 1);show_list(phead);Change(phead, 1, 2);show_list(phead);return 0;
}

5最终代码---链表实现方法有很多掌握自己熟练的解决问题才是关键

#define _CRT_SECURE_NO_WARNINGS
//头文件
#include<stdio.h>
#include<stdlib.h>//宏定义
#define DataType int 
//全局变量//结构体
typedef struct node
{DataType data;struct node* next;
}node, * Node;Node Init(Node phead)
{phead = (Node)malloc(sizeof(node));if (phead == NULL)return -1;phead->next = NULL;
}int PushBack(Node phead, DataType num)
{//创建新节点Node newnode = malloc(sizeof(node));newnode->next = NULL;newnode->data = num;//创建指针找到链表的尾,然后插入,Node p = NULL;//出来for循环就插入不用考虑链表为空因为for循环里面已经考虑判断了for (p = phead; p->next != NULL; p = p->next);p->next = newnode;return 0;
}void show_list(Node phead)
{Node cur = NULL;cur = phead;//判断是否为空if (phead == NULL){printf("NULL\n");}//遍历头节点没有数据所有从头节点下一个数据开始打印for (cur = phead->next; cur != NULL; cur = cur->next){printf("%d->", cur->data);}printf("NULL\n");
}
int Delect(Node phead,DataType num)
{if (phead == NULL){printf("peahd is NULL\n");return -1;}Node cur1= NULL;Node cur2 = NULL;for (cur1 = phead, cur2 = phead->next; cur1->next != NULL; cur1 = cur2, cur2 = cur2->next){if (cur2->data == num){cur1->next = cur2->next;free(cur2);return 0;}}printf("no find num\n");return -1;
}int Change(Node phead, int num1, int num2)
{if (phead == NULL){printf("peahd is NULL\n");return -1;}Node cur = NULL;for (cur = phead->next;cur != NULL; cur = cur->next){if (cur->data == num1){cur->data = num2;return 0;}}printf("no find num1\n");return -1;
}
void Releas(Node phead)
{Node cur1 = NULL;Node cur2 = NULL;for (cur1 = cur2 = phead; cur1->next != NULL; cur1 = cur2){cur2 = cur1->next;free(cur1);}
}
int main()
{Node phead = NULL;phead = Init(phead);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);PushBack(phead, 1);Change(phead, 1, 2);Releas(phead);show_list(phead);Delect(phead, 1);show_list(phead);Delect(phead, 1);Delect(phead, 1);Delect(phead, 1);show_list(phead);Delect(phead, 1);show_list(phead);Change(phead, 1, 2);show_list(phead);return 0;
}


http://www.mrgr.cn/news/6139.html

相关文章:

  • vue Formily动态表单解决方案
  • 揭秘电子版招生简章的制作方法!
  • 昇腾 - AscendCL C++应用开发 图像文件的解码时硬件对图像的宽度和高度的处理方式
  • 【GitLab】使用 Docker 安装 3:gitlab-ce:17.3.0-ce.0 配置
  • 股票买卖的思路与代码
  • Vue `<script setup>` 属性的深入解析
  • docker私有仓库创建与推送
  • 马思克聊天机器人Grok-2图像生成审查过滤机制引发巨大的争议
  • VR(虚拟现实)和AR(增强现实)
  • Nacos 修复 Tomcat 信息泄露漏洞CVE-2024-21733
  • 响应式 Web 设计:纯 HTML 和 CSS 的实现技巧
  • dm 到 dm 的 HS 同步部署
  • 《深入剖析原型模式:浅克隆、深克隆与单例模式的碰撞》
  • 线程的控制
  • ThreeJs学习笔记--GUI(可视化三维改变场景)
  • 【学习笔记】卫星网络(NTN)的窄带物联网(NB-IoT)研究 -- 3GPP TR 36.763(四)
  • [数据集][目标检测]机械常用工具检测数据集VOC+YOLO格式4713张8类别
  • Selenium元素定位
  • 【python】Python中小巧的异步web框架Sanic快速上手实战
  • [go] 在遍历map过程中删除成员是否安全?