【静态链表】

[b][size=large]本文围绕以下四个部分展开: [/size][/b]

[b][size=large]一、静态链表[/size][/b]
[b][size=large]二、插入[/size][/b]
[b][size=large]三、删除[/size][/b]
[b][size=large]四、优缺点[/size][/b]


[b][size=large]一、静态链表[/size][/b]

[size=medium][b]1. 概念[/b][/size]

[size=medium]C语言有指针,Java、C#等有对象引用机制,因此也间接实现了指针的某些作用。但对于像Basic、Fortran等早期的高级语言,是没有指针的。[/size]

[size=medium]若不使用指针,如何处理链表结构?使用数组来代替指针,来描述单链表。[/size]

[size=medium]这种用数组描述的链表叫做静态链表。(给没有指针的高级语言设计的一种实现单链表能力的方法。)[/size]

[size=medium]游标实现法:[/size]

[size=medium]让数组的元素均由两个数据域组成:data(数据域)和cur(游标)。即:数组的每个下标都对应一个data和一个cur。[/size]

[size=medium]data:用来存放数据元素(要处理的数据)。[/size]

[size=medium]cur:相当于单链表的next指针,存放该元素的后继在数组中的下标。[/size]


[size=medium][b]2. 静态链表存储结构[/b][/size]

[img]http://dl2.iteye.com/upload/attachment/0111/4046/3aa2b1ea-fb49-383c-9249-d6eac4cfc2b9.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0111/4048/c07300dc-b12b-30c0-8f5e-7a17dece479d.png[/img]


[size=medium][b]3. 注意[/b][/size]

[size=medium](1)通常将数组建立得大一些,以便有一些空闲空间可以便于插入时不至于溢出。[/size]

[size=medium](2)数组的第一个和最后一个元素不存数据,作为特殊元素处理。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4050/d1e8ceb4-5d96-3680-a59a-1649bc76a753.png[/img][/align]


[size=medium][b]4. 例子[/b][/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4052/d1b6975a-4c1b-3ae1-9c84-6f8a163f359d.png[/img][/align]


[b][size=large]二、插入[/size][/b]

[size=medium][b]例子:上图中,在乙和丁之间插入丙。[/b][/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4054/f938fbe4-bbc4-31a0-9681-3cfe5ae92b43.png[/img][/align]

[size=medium][b](1)[/b]动态链表中,结点的申请使用的是malloc()函数。在静态链表中,操作的是数组,因此必须自己实现这样的函数,用来做插入操作。[/size]

[size=medium]解决办法:将所有未被使用过的及已被删除的分量用游标链成一个备用链表。每当插入时,从备用链表上取得第一个结点作为待插入的新结点。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4056/56bbc603-8a2f-31f5-a970-0e614555f40e.png[/img][/align]
[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4058/07c413d3-e5cb-3872-89c9-7a2806d39b2b.png[/img][/align]


[size=medium][b](2)[/b]插入的算法如下:[/size]

[img]http://dl2.iteye.com/upload/attachment/0111/4060/5078f82e-a061-34ba-a640-9eac4865a0ca.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0111/4062/4dde1d0f-b3e5-3492-800a-54f1898ec5a3.png[/img]


[b][size=large]三、删除[/size][/b]

[size=medium][b]例子:上图中,删除甲。[/b][/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4064/63e01133-2e0d-3c48-bfac-21a2a42f7aad.png[/img][/align]

[size=medium][b](1)[/b]动态链表中,结点的申请使用的是free()函数。在静态链表中,操作的是数组,因此必须自己实现这样的函数,用来做删除操作。[/size]

[img]http://dl2.iteye.com/upload/attachment/0111/4066/b6c7fc5c-6097-34fd-bd50-feeeb00c6970.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0111/4068/4b80467a-1cd8-31e5-b0fd-89582593bd42.png[/img]

[size=medium]其中,j=L[999].cur=1, L[k].cur=L[j].cur,即:L[999].cur=L[1].cur=2。意思是:甲已删除,现在乙是第一个元素。[/size]

[size=medium]代码中的:Free_SSL(L,j);如下。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4070/a66daffd-61fa-3153-9ee2-dca4b24ccf31.png[/img][/align]

[size=medium]代码中,space[1].cur=space[0].cur=8,意思是:把8给“甲”所在下标为1的分量的cur。space[0].cur=k=1,意思是:让删除的位置成为第一个优先空位,把它存入第一个元素(下标为0)处的cur中。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4064/63e01133-2e0d-3c48-bfac-21a2a42f7aad.png[/img][/align]


[b][size=large]四、优缺点[/size][/b]

[align=center][img]http://dl2.iteye.com/upload/attachment/0111/4072/ffedaa55-c247-3bc4-adb8-13aa7424f029.png[/img][/align]


[size=large]整理时重点参考:[color=red]《大话数据结构》程杰著[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
静态链表是一种使用数组实现的链表结构,它通过数组中的元素来模拟链表中的节点,并使用一个特殊的指针(游标)来表示节点之间的逻辑关系。静态链表的优点是实现简单,不需要频繁地申请和释放内存空间,但缺点是插入和删除操作比较麻烦,需要手动维护游标指针。 以下是一个简单的Python静态链表的实现示例[^1]: ```python class StaticLinkedList: def __init__(self, size): self.data = [None] * size # 存储数据的数组 self.next = [i + 1 for i in range(size)] # 游标数组,用于维护节点之间的逻辑关系 self.next[-1] = -1 # 最后一个元素的游标为-1,表示链表的末尾 self.head = -1 # 头指针,指向链表的第一个节点 def is_empty(self): return self.head == -1 def is_full(self): return self.next == -1 def insert(self, value): if self.is_full(): print("StaticLinkedList is full") return new_node = self.next # 获取一个空闲节点 self.next = self.next[new_node] # 更新空闲节点的游标 self.data[new_node] = value # 在空闲节点中存储数据 if self.is_empty(): self.head = new_node self.next[new_node] = -1 else: current = self.head while self.next[current] != -1: current = self.next[current] self.next[current] = new_node self.next[new_node] = -1 def delete(self, value): if self.is_empty(): print("StaticLinkedList is empty") return prev = -1 current = self.head while current != -1: if self.data[current] == value: break prev = current current = self.next[current] if current == -1: print("Value not found") return if prev == -1: self.head = self.next[current] else: self.next[prev] = self.next[current] self.next[current] = self.next # 将删除的节点加入空闲节点链表 self.next = current def display(self): if self.is_empty(): print("StaticLinkedList is empty") return current = self.head while current != -1: print(self.data[current], end=" ") current = self.next[current] print() # 创建一个容量为5的静态链表 static_list = StaticLinkedList(5) # 插入数据 static_list.insert(1) static_list.insert(2) static_list.insert(3) # 删除数据 static_list.delete(2) # 显示链表中的数据 static_list.display() ``` 输出结果为: ``` 1 3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值