尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

单链表与节点

单链表与节点 经过上次的顺序表我们这次把链表代码敲出来看看它们之间不一样地方时间复杂度是否下降呢链表和顺序表有什么不一样为了方便大家容易理解AI帮我们生成表格容易理解链表是物理内存不连续的数据结构不像数组元素在内存里挨在一起。链表靠指针保存下一个节点的地址把分散的节点串起来形成逻辑上的连续序列。plist头指针保存第一个节点的地址0x0012FFB0通过它找到链表起点。每一个方框就是一个节点节点分为两部分数据域存放数据图中是1、2、3、4指针域 (next)存放下一个节点的内存地址节点 1 的 next 存0x0012FFA0指向节点 2 节点 2 的 next 存0x0012FFD0指向节点 3 节点 3 的 next 存0x0012FFC0指向节点 4最后节点 4 的指针域为NULL代表链表到此结束没有后续节点。什么意思呢我们直接上代码解释看过上一期的顺序表不用多介绍如何创建test.h#pragma once #includestdio.h #includestdlib.h #includeassert.h typedef int type; typedef struct tg { type date; struct tg* next; }ls; //打印 void print(ls* ps);test.c#include test.h //打印 void print(ls* ps) { ls* pre ps; while (pre) { printf(%d , pre-date); pre pre-next; } printf(NULL\n); }mian.c#includetest.h void test1() { ls* node1 (ls*)malloc(sizeof(ls)); ls* node2 (ls*)malloc(sizeof(ls)); ls* node3 (ls*)malloc(sizeof(ls)); ls* node4 (ls*)malloc(sizeof(ls)); node1-date 1; node2-date 2; node3-date 3; node4-date 4; node1-next node2; node2-next node3; node3-next node4; node4-next NULL; ls* ptail node1; print(ptail); }进阶代码https://gitee.com/yang-mianmian-1/singly-linked-list
返回列表