python散装笔记——24: 链表(链表数据结构python)
haoteby 2025-01-23 17:20 3 浏览
链表是一个节点集合,每个节点由一个引用和一个值组成。节点通过引用串成序列。链接表可用于实现更复杂的数据结构,如列表、堆栈、队列和关联数组。
Section 24.1: 单链表范例
这个示例实现了一个链接列表,其中的许多方法与内置列表对象的方法相同。
class Node:
def __init__(self, val):
self.data = val
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, val):
self.data = val
def setNext(self, val):
self.next = val
class LinkedList:
def __init__(self):
self.head = None
def isEmpty(self):
"""Check if the list is empty"""
return self.head is None
def add(self, item):
"""Add the item to the list"""
new_node = Node(item)
new_node.setNext(self.head)
self.head = new_node
def size(self):
"""Return the length/size of the list"""
count = 0
current = self.head
while current is not None:
count += 1
current = current.getNext()
return count
def search(self, item):
"""Search for item in list. If found, return True. If not found, return False"""
current = self.head
found = False
while current is not None and not found:
if current.getData() is item:
found = True
else:
current = current.getNext()
return found
def remove(self, item):
"""Remove item from list. If item is not found in list, raise ValueError"""
current = self.head
previous = None
found = False
while current is not None and not found:
if current.getData() is item:
found = True
else:
previous = current
current = current.getNext()
if found:
if previous is None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
else:
raise ValueError
print 'Value not found.'
def insert(self, position, item):
"""
Insert item at position specified. If position specified is
out of bounds, raise IndexError
"""
if position > self.size() - 1:
raise IndexError
print "Index out of bounds."
current = self.head
previous = None
pos = 0
if position is 0:
self.add(item)
else:
new_node = Node(item)
while pos < position:
pos += 1
previous = current
current = current.getNext()
previous.setNext(new_node)
new_node.setNext(current)
def index(self, item):
"""
Return the index where item is found.
If item is not found, return None.
"""
current = self.head
pos = 0
found = False
while current is not None and not found:
if current.getData() is item:
found = True
else:
current = current.getNext()
pos += 1
if found:
pass
else:
pos = None
return pos
def pop(self, position = None):
"""
If no argument is provided, return and remove the item at the head.
If position is provided, return and remove the item at that position.
If index is out of bounds, raise IndexError
"""
if position > self.size():
print 'Index out of bounds'
raise IndexError
current = self.head
if position is None:
ret = current.getData()
self.head = current.getNext()
else:
pos = 0
previous = None
while pos < position:
previous = current
current = current.getNext()
pos += 1
ret = current.getData()
previous.setNext(current.getNext())
print ret
return ret
def append(self, item):
"""Append item to the end of the list"""
current = self.head
previous = None
pos = 0
length = self.size()
while pos < length:
previous = current
current = current.getNext()
pos += 1
new_node = Node(item)
if previous is None:
new_node.setNext(current)
self.head = new_node
else:
previous.setNext(new_node)
def printList(self):
"""Print the list"""
current = self.head
while current is not None:
print current.getData()
current = current.getNext()
使用功能与内置的列表功能类似。
ll = LinkedList()
ll.add('l')
ll.add('H')
ll.insert(1,'e')
ll.append('l')
ll.append('o')
ll.printList()
H
e
l
l
o
相关推荐
- 前端:从零实现一款可视化图片编辑器
-
背景介绍我们知道,为了提高企业研发效能和对客户需求的快速响应,现在很多企业都在着手数字化转型,不仅仅是大厂(阿里,字节,腾讯,百度)在做低代码可视化这一块,很多中小企业也在做,拥有可视化低代码相关技术...
- 2018年面世 英特尔将打造超级计算机
-
|责编:王冬奇中关村在线消息:据国外媒体报道,近日英特尔宣布将联手Cray公司为美国阿贡国家实验室打造一台性能强大的全新超级计算机——极光(Aurora),运算性能可达到180P-Flops(每秒浮...
- Hyperledger Fabric 2.0安装教程
-
本文介绍如何安装最新的HyperledgerFabric2.0的预编译程序、fabric-samples示例配置和代码以及docker镜像。HyperledgerFabric区块链开发教程:F...
- 一文精通虚拟端口通道vPC,精品文章,爱了
-
今天给大家带来的是虚拟端口通道相关的技术:简介...
- 「数据中心」数据中心脊页架构:思科FabricPath Spine和Leaf网络
-
思科在2010年引入了FabricPath技术。FabricPath提供了新的功能和设计选项,使网络运营商能够创建以太网结构,从而提高带宽可用性,提供设计灵活性,并简化和降低网络和应用程序部署和操作的...
- 51单片机项目:定时宠物喂食系统(含代码)keil、DXP原理图
-
题目要求:一、拟解决的主要问题...
- 基于51单片机的多功能智能语音循迹避障小车(含代码)
-
大家好,今天给大家介绍基于51单片机的多功能智能语音循迹避障小车,下方附有本文涉及的全部资料和源代码的获取方式,可进群免费领取。一.功能介绍及硬件准备这是一款基于51单片机开发的智能小车,通过这篇文章...
- 如何对自己尚不熟悉Angular.js的情况下对代码进行调试
-
【51CTO.com快译】如果大家对AngularJS还不熟悉,那么可能会在初步创建Web应用时对很多问题感到担心。而且尽管这可能已经是我们所能用到的上手难度最低的Web开发框架之一,但大家仍然需要了...
- 拿代码量算KPI跟程序员们来这套?(下)
-
嘿嘿,一个美丽的周末又这么过来了~小伙伴们都做了些啥呢?加班了咩?改bug了咩?催需求了咩?小编也如约更新“拿代码量算KPI……跟程序员们来这套?(下)”前情回顾请点击下方菜单栏的“精彩文章”,找到7...
- 哆啦A梦彩色版第5卷第51章,胖虎的料理
-
重温童年经典动漫,哆啦A梦彩色版第5卷第51章,胖虎的料理...
- 51单片机项目设计:基于51单片机时钟万年历(含代码、原理图)
-
大家好,今天给大家介绍基于单片机stm32的多功能氛围灯、手机控制ws2812和MCU升级程序,文章末尾附有本毕业设计的论文和源码的获取方式,也可现在直接进群免费领取。...
- 重构代码,真没有银弹
-
译者|布加迪我的一位同事在大型项目代码重构方面有丰富的经验,他真诚地与我分享了他如何处理这些繁杂的任务。虽然他做的大部分事情只是坚持不懈地努力,就像在健身房锻炼那样,但这对我来说很有意义。本文分享...
- 11个奇奇怪怪的微信隐藏玩法(含撩妹教程)
-
最近,我在微信发现了一个好玩的东西用它可以扒到好友的“黑料”...
- 程序员没转发公司朋友圈,被罚款500,半个月后3行代码让领导懵了
-
现在在职场,也确实存在着许多的身不由己,很多事情都不是自己想做的,但是为了工作也不得不做。就比如说公司经常会要求员工们发一些朋友圈,很多人都不愿意把工作上的东西发到朋友圈去,但是如果不发又要挨领导的批...