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. /etc/security/limits.conf的相关说明

    今天遇到root账户登录不了的情况,很是郁闷,即使单用户修改了root密码也不能登录. 所以就特意看了一下/etc/security/limits.conf,发现是下面这样的.感觉呗坑了许久.(标红线 ...

  2. MySQL添加用户并授权

    一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用户名 host:指定该用户 ...

  3. winform窗体运行时的大小和设计时不一致

    窗体设置的尺寸为1946*850,而电脑分辨率是1920*1280 按说宽度已经超过屏幕大小很多了,应该显示占满屏幕宽度才对,但是运行时宽度只有设计时的一半 高度最多只能是1946像素,再拉大也不管用 ...

  4. 编写一个 rpc

    手动编写一个 RPC 调用 package com.alibaba.study.rpc.framework; import java.io.ObjectInputStream; import java ...

  5. Linux - Linux 终端命令格式

    Linux 终端命令格式 目标 了解终端命令格式 知道如何查阅终端命令帮助信息 01. 终端命令格式 command [-options] [parameter] 说明: command:命令名,相应 ...

  6. 从 PC 卸载 Office

    https://support.office.com/zh-cn/article/%E4%BB%8E-PC-%E5%8D%B8%E8%BD%BD-Office-9dd49b83-264a-477a-8 ...

  7. 【CSS】Sticky Footer 布局

    什么是 Sticky Footer 布局? Sticky Footer 布局是一种将 footer 吸附在底部的CSS布局. footer 可以是任意的元素,该布局会形成一种当内容不足,footer ...

  8. nginx访问502 gateway,*1 connect() failed (111: Connection refused) while connecting to upstream

    安装好nginx,php环境后,配置虚拟主机,结果访问后就报502 gateway,查看日志文件后,显示错误如下: 2019/04/29 16:24:39 [error] 19433#19433: * ...

  9. Memcached和Memcache安装(64位win7)[z]

    http://www.cnblogs.com/lucky-man/p/6126667.html 一.Memcached和Memcache的区别: 网上关于Memcached和Memcache的区别的理 ...

  10. java maven compiler设置默认1.8

    方法一: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupI ...