基于Python单向链表实现尾部、任意位置添加,删除
来源:互联网 发布:s8不祥之刃符文 知乎 编辑:程序博客网 时间:2024/04/29 14:35
# coding = utf-8
# 创建节点类
class Node(object):
def __init__(self, data):
# 定义指向域
self.next = None
# 定义数据域
self.data = data
# 创建链表类
class LinkDict(object):
def __init__(self):
self._head = Node(None)
self._length = 0
# 尾部添加元素
def append(self, data):
# 构造新节点
new_code = Node(data)
# 查找尾部节点并定义游标指向头节点,向尾部添加新节点
cur = self._head
# 循环遍历
while cur.next is not None:
# 移动游标
cur = cur.next
else:
# 向尾部添加新节点
cur.next = new_code
# 链表长度加1
self._length += 1
return self._length
# 循环遍历链表元素
def traval(self):
cur = self._head
while cur.next is not None:
# 打印后边节点
print(cur.next.data, "--->", end="")
# 游标后移
cur = cur.next
else:
print("None:now is terminal")
# 任意位置添加元素
def insert(self, pos, data):
# 判断插入点
if isinstance(pos, int):
if pos < 0:
link_dict.insert(0, data)
elif pos > self._length:
link_dict.append(data)
else:
# 构造新节点
new_code = Node(data)
# 构造游标
cur = self._head
# 构造计数器
cnt = 0
while cur.next is not None:
if cnt == pos:
# 让新节点有所指向
new_code.next = cur.next
# 让与新节点有关的节点有所指向
cur.next = new_code
self._length += 1
return
else:
# 移动游标
cur = cur.next
# 计数器加1
cnt += 1
else:
print("pos位置无效, 请确认")
exit()
def is_empty(self):
if self._length == 0:
print("链表为空")
return True
else:
return False
def remove(self, data):
if link_dict.is_empty():
return True
else:
cur = self._head
while cur.next is not None:
if cur.next.data == data:
# 删除节点
cur.next = cur.next.next
self._length -= 1
return
else:
# 游标移动
cur = cur.next
else:
print("data is not in link_data")
if __name__ == "__main__":
link_dict = LinkDict()
print("尾 部 添 加: ", end="")
for i in range(10):
link_dict.append(i)
link_dict.traval()
print("任意位置添加: ", end="")
link_dict.insert(-1, "a")
link_dict.insert(100, "z")
link_dict.insert(6, "x")
link_dict.traval()
print("删 除 节 点: ", end="")
link_dict.remove("a")
link_dict.remove("x")
link_dict.remove("z")
# 创建节点类
class Node(object):
def __init__(self, data):
# 定义指向域
self.next = None
# 定义数据域
self.data = data
# 创建链表类
class LinkDict(object):
def __init__(self):
self._head = Node(None)
self._length = 0
# 尾部添加元素
def append(self, data):
# 构造新节点
new_code = Node(data)
# 查找尾部节点并定义游标指向头节点,向尾部添加新节点
cur = self._head
# 循环遍历
while cur.next is not None:
# 移动游标
cur = cur.next
else:
# 向尾部添加新节点
cur.next = new_code
# 链表长度加1
self._length += 1
return self._length
# 循环遍历链表元素
def traval(self):
cur = self._head
while cur.next is not None:
# 打印后边节点
print(cur.next.data, "--->", end="")
# 游标后移
cur = cur.next
else:
print("None:now is terminal")
# 任意位置添加元素
def insert(self, pos, data):
# 判断插入点
if isinstance(pos, int):
if pos < 0:
link_dict.insert(0, data)
elif pos > self._length:
link_dict.append(data)
else:
# 构造新节点
new_code = Node(data)
# 构造游标
cur = self._head
# 构造计数器
cnt = 0
while cur.next is not None:
if cnt == pos:
# 让新节点有所指向
new_code.next = cur.next
# 让与新节点有关的节点有所指向
cur.next = new_code
self._length += 1
return
else:
# 移动游标
cur = cur.next
# 计数器加1
cnt += 1
else:
print("pos位置无效, 请确认")
exit()
def is_empty(self):
if self._length == 0:
print("链表为空")
return True
else:
return False
def remove(self, data):
if link_dict.is_empty():
return True
else:
cur = self._head
while cur.next is not None:
if cur.next.data == data:
# 删除节点
cur.next = cur.next.next
self._length -= 1
return
else:
# 游标移动
cur = cur.next
else:
print("data is not in link_data")
if __name__ == "__main__":
link_dict = LinkDict()
print("尾 部 添 加: ", end="")
for i in range(10):
link_dict.append(i)
link_dict.traval()
print("任意位置添加: ", end="")
link_dict.insert(-1, "a")
link_dict.insert(100, "z")
link_dict.insert(6, "x")
link_dict.traval()
print("删 除 节 点: ", end="")
link_dict.remove("a")
link_dict.remove("x")
link_dict.remove("z")
link_dict.traval()
程序运行结果:
尾 部 添 加: 0 --->1 --->2 --->3 --->4 --->5 --->6 --->7 --->8 --->9 --->Node:now is terminal
任意位置添加: a --->0 --->1 --->2 --->3 --->4 --->x --->5 --->6 --->7 --->8 --->9 --->z --->Node:now is terminal
删 除 节 点: 0 --->1 --->2 --->3 --->4 --->5 --->6 --->7 --->8 --->9 --->Node:now is terminal
阅读全文
0 0
- 基于Python单向链表实现尾部、任意位置添加,删除
- 基于Python单向循环链表实现尾部、任意位置添加,删除
- 单向链表实现任意位置插入元素
- C语言实现链表之单向链表(九)在任意位置插入结点
- C语言实现链表之单向链表(十)删除任意结点
- Python 实现单向链表
- python实现单向链表
- Python 实现单向链表
- 单向链表(二) 删除指定位置的节点
- 实现单向链表节点删除
- python实现任意位置文件分割
- 顺序链表 : 在任意位置添加节点
- 单向链表--初始化、添加删除节点、排序链表
- 单向链表的删除元素,添加元素等操作
- 单向链表的添加、删除与遍历
- 单向链表的建立,添加与删除
- 单向链表的建立,添加与删除
- Python 实现简单的单向链表
- 关闭windows的autoplay
- 单例模式的几种实现方式
- CS231n学习笔记--8.Deep Learning Software
- Linux之Shell脚本学习笔记
- 文件处理命令
- 基于Python单向链表实现尾部、任意位置添加,删除
- 【图像融合】基于小波变换的图像融合
- 51nod 1290 Counting Diff Pairs(莫队算法)
- 创建不能双击打开也不能删除的文件夹
- Mysql之查询
- 关于面向接口编程之一
- POJ 2155 Matrix 3468 A Simple Problem with Integers
- matrix (dp)
- 制作可启动DOS优盘