Python数据结构之单链表

单链表有后继结点,无前继结点。

以下实现:

  • 创建单链表
  • 打印单链表
  • 获取单链表的长度
  • 判断单链表是否为空
  • 在单链表后插入数据
  • 获取单链表指定位置的数据
  • 获取单链表指定元素的索引
  • 删除单链表指定位置的元素
  • 更新单链表指定位置的元素
  • 清空单链表
class Node(object):
"""定义类来描述指针"""
def __init__(self, data, p=None):
self.data = data
self.next = p class LinkList(object):
"""单链表""" def __init__(self):
self.head = None # 初始化单链表
def create(self, data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
p.next = Node(i)
p = p.next # 打印单链表
def print(self):
p = self.head
while p != None:
print(p.data)
p = p.next # 获取单链表的长度
def len(self):
p = self.head
length = 0
while p != None:
length += 1
p = p.next
return length # 判断单链表是否为空
def is_empty(self):
return self.len() == 0 # 在单链表后插入数据
def append(self, item):
if self.is_empty():
self.head = Node(item)
else:
p = self.head
while p.next != None:
p = p.next
p.next = Node(item) # 获取单链表指定位置的数据
def getItem(self, index):
if self.is_empty():
print("单链表为空")
return
if index >= self.len() or index < 0:
print("索引超过单链表长度")
return
p = self.head
count = 0
while count != index:
p = p.next
count += 1
return p.data # 获取单链表指定元素的索引
def find(self, item):
p = self.head
index = 0
while p != None:
if p.data == item:
return index
p = p.next
index += 1 print("单链表中不存在" + repr(item)) # 在单链表指定位置插入元素
def insert(self, index, item):
if self.is_empty():
print("单链表为空")
return
if index >= self.len() or index < 0:
print("索引超过单链表长度")
return
if index == 0:
self.head = Node(item, self.head)
else:
p = self.head
count = 0
while count < index-1:
p = p.next
count += 1
p.next = Node(item, p.next) # 删除单链表指定位置的元素
def delete(self, index):
if self.is_empty():
print("单链表为空")
return
if index >= self.len() or index < 0:
print("索引超过单链表长度")
return
if index == 0:
self.head = self.head.next
else:
p = self.head
count = 0
while count < index-1:
p = p.next
count += 1
p.next = p.next.next # 更新单链表指定位置的元素
def update(self, index, data):
if self.is_empty():
print("单链表为空")
return
if index > self.len() or index < 0:
print("索引超过单链表长度")
return
p = self.head
count = -1
while count < index-1:
p = p.next
count += 1
p.data = data # 清空单链表
def clear(self):
self.head = None L = LinkList()
L.create([1, 2, 3])
print("打印单链表:")
L.print()
print("获取单链表的长度:")
print(L.len())
print("单链表是否为空")
print(L.is_empty())
print("在单链表后插入数据")
L.append(4)
L.print()
index = 1
print("获取第" + repr(index) + "个位置的数据")
print(L.getItem(index))
item = 3
print("获取单链表中元素" + repr(item) + "的索引")
print(L.find(item)) index = 2
item = 10
print("在单链表的" + repr(index) + "位置插入数据" + repr(item))
L.insert(index, item)
L.print()
index = 2
print("删除单链表"+repr(index)+"位置的元素")
L.delete(index)
L.print()
index = 2
item = 100
print("更新单链表"+repr(index)+"位置的元素为"+repr(item))
L.update(index, item)
L.print()
print("清空单链表")
L.clear()
L.print()

程序输出结果:

打印单链表:
1
2
3
获取单链表的长度:
3
单链表是否为空
False
在单链表后插入数据
1
2
3
4
获取第1个位置的数据
2
获取单链表中元素3的索引
2
在单链表的2位置插入数据10
1
2
10
3
4
删除单链表2位置的元素
1
2
3
4
更新单链表2位置的元素为100
1
2
100
4
清空单链表

Python数据结构之单链表的更多相关文章

  1. python 数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...

  2. python——数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址 信息,所以用一个变量 ...

  3. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  4. javascript数据结构之单链表

    下面是用javascript实现的单链表,但是在输出的时候insert方法中存在问题,chrome的console报错说不能读取空的属性,调试了很久都没有通过,先在这里存着,以后再来修改一下. //数 ...

  5. 数据结构之单链表的实现-java

    一.单链表基本概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元(一般是非连续存储单元)存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点的构成:元素data + 指针next ...

  6. 【数据结构】单链表介绍及leetcode206题反转单链表python实现

    题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...

  7. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  8. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

  9. 数据结构:单链表结构字符串(python版)

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

随机推荐

  1. RabbitMQ消费者抛异常日志持续打印的问题

    场景 消费者接受消息,进行一系列处理,但是由于某些原因处理过程中该消费者的抛出了异常,并且不捕获(直接 throws IOException 抛出去): 由于抛出了IOException,那么这条消息 ...

  2. iframe刷新

    <div title="基本信息" style="padding:2px; ">       <iframe id="newsrc& ...

  3. html入门第一天(知识总结)。

    一文本标签:----------双标签.<b>标签 <strong>标签,文本呈现粗体(Html5中建议用strong,strong语义更强)<i>标签 <e ...

  4. findbugs插件使用

    本文以idea的插件举例子 介绍 Findbugs是一个静态分析工具,它检查类或者JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题. idea安装 自此,插件安装完毕,需要重启idea才生 ...

  5. step_by_step_记录一个javascript字符串处理问题

    记录一个javascript字符串处理的问题 这一天下班,技术QQ群里的大神提出了一个问题,带着问题去思考. ? '---9890.999008-555555-55555555----' 对于这样的字 ...

  6. mysql学习笔记--数据库预处理

    一.概念 1. 预编译一次,可以多次执行.用来解决一条sql语句频繁执行的问题 2. 语法 a. 预处理语句:preapre 预处理名字 from 'sql语句' b. 执行预处理:execute 预 ...

  7. innodb_flush_log_at_trx_commit与sync_binlog理解

    innodb_flush_log_at_trx_commit该参数控制重做日志写入磁盘的过程.我们知道 InnoDB 使用“Write Ahead Log”策略来避免数据丢失问题,即依靠重做日志来保证 ...

  8. 编写一个lambda,接受两个int,返回它们的和

    void counter(int i, int b) { auto count = [i,b]{ return i + b; }; cout<< count(); } int main() ...

  9. 【c # 数据库】存储过程

    可理解存储过程是方法,快速调用,方便使用. 数据库建立新的存储过程: CREATE PROCEDURE myProc -- Add the parameters for the stored proc ...

  10. paloalto防火墙安装内容和软件更新

    1.为了确保您始终不会受到最新威胁(包括尚未发现的威胁)的攻击,您必须确保防火墙始终具有 Palo Alto Networks 发布的最新更新内容及软件. • Antivirus(防病毒)— 包括新的 ...