python散装笔记——24: 链表(链表数据结构python)
haoteby 2025-01-23 17:20 9 浏览
链表是一个节点集合,每个节点由一个引用和一个值组成。节点通过引用串成序列。链接表可用于实现更复杂的数据结构,如列表、堆栈、队列和关联数组。
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
相关推荐
- 二次面试终拿到offer,百度Android面试真题解析我整理出来了
-
找工作的大潮来临了,这边给大家分享一下面试会遇到的问题。Android开发了5年,之前一直都是在小公司码着代码,对大厂一直有着憧憬,我是在去年年初的时候通过朋友的内推面试了百度,结果被怼的没话说,那叫...
- Android 开发中文引导-应用小部件
-
应用小部件是可以嵌入其它应用(例如主屏幕)并收到定期更新的微型应用视图。这些视图在用户界面中被叫做小部件,并可以用应用小部件提供者发布。可以容纳其他应用部件的应用组件叫做应用部件的宿主(1)。下面的截...
- Win10桌面/手机版最深层次开发功能挖掘
-
IT之家讯Win10开发者预览版为我们提供了一个Win10大框架的早期概览,使开发者与热心用户都可以提前感受Win10带来的新特性,尝试新工具,而作为开发者,最关心的莫过于Windows多平台通用应...
- Android TabLayout + ViewPager2使用
-
1、xml文件<!--明细列表--><com.google.android.material.tabs.TabLayoutandroid:id="@+id/ty_...
- android培训学习的大纲_android软件开发培训
-
第一阶段android基础:1.基础javaJava概述,进制,数据类型,常量变量,运算符,表达式关系运算符,逻辑运算符,if语句,switch语句while循环,do...while循环,for循环...
- 背了几十份面经还是连挂6个面试,最终拿下腾讯后总结了这些坑点
-
刚开始面试的时候我真的是处处碰壁,面一家挂一家,面完之后怀疑自我,是不是自己真的太菜了找不到工作。工作本身就是双向选择,一家不行再换一家,总有合适的,千万不要因为别人的一句话就全盘否定自己,一定要自信...
- webview 渲染机制:硬件加速方式渲染的Android Web
-
webview渲染是什么?webview渲染是用于展现web页面的控件;webview可以内嵌在移动端,实现前端的混合式开发,大多数混合式开发框架都是基于webview模式进行二次开发的...
- ExpandListView 的一种巧妙写法_仿写丁香结的写法写一种花梅花
-
ExpandListView大家估计也用的不少了,一般有需要展开的需求的时候,大家不约而同的都想到了它然后以前自己留过记录的一般都会找找以前自己的代码,没有记录习惯的就会百度、谷歌,这里吐槽一下,好几...
- Android监听滚动视图_滚动监听代码
-
AndroidUILibs之Android-ObservableScrollView1.说明Android-ObservableScrollView,顾名思义,Android上观察滚动的视图,可...
- Flutter 之 ListView_flutter开发
-
在Flutter中,ListView可以沿一个方向(垂直或水平方向)来排列其所有子Widget,常被用于需要展示一组连续视图元素的场景ListView构造方法ListView:仅适用于列表中...
- Android之自定义ListView(一)_android 自定义listview
-
PS:自定义View是Android中高手进阶的路线.因此我也打算一步一步的学习.看了鸿洋和郭霖这两位大牛的博客,决定一步一步的学习,循序渐进.学习内容:1.自定义View实现ListView的Ite...
- ListView 使用详解_listview在哪里
-
阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android本篇文章主要介绍Android开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:ListView主要使用方法使用androi...
- 穿裙子的李宇春,需要向谁解释吗?
-
...
- 明星们“不想占用”的公共资源,到底是个啥?
-
近日,社交媒体上可谓“一瓜未平一瓜又起”,明星们隔三差五地在热搜上道歉、澄清,好不热闹。道歉原因千万种,道歉话术却雷同——通常都以“无意占用公共资源”开头,不管是澄清、官宣,还是回应、声明,都会带上这...
- 选择女人的模板,模板不同,生活方式不同
-
文/高阳人生在世,选择伴侣成了头等大事。尤其是对于男人来说,“选择女人的模板”直接决定了你未来几十年的生活方式,有时简直像组装拼图,每一块都有代价。莫言曾说过:“你选择能干的女人,就得接受她的强势;你...