python——python 数据结构之双向链表的实现
和单链表类似,只不过是增加了一个指向前面一个元素的指针而已。
示意图:

python 实现代码:
#Personal Python Data Structure--PPDS
#
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Node(object): def __init__(self,val,p=0):
self.data = val
self.next = p
self.prev = p class LinkList(object): def __init__(self):
self.head = 0 def initlist(self,data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
node = Node(i)
p.next = node
node.prev = p
p = p.next def getlength(self):
length = 0
p = self.head
while p != 0:
length+=1
p = p.next
return length def is_empty(self):
if self.getlength() == 0:
return True
else:
return False def clear(self):
self.head = 0 def append(self,item):
q = Node(item)
if self.head == 0:
self.head = q
else:
p = self.head
while p.next != 0:
p = p.next
p.next = q
q.prev = p def getitem(self,index):
if self.is_empty():
print ('Linklist is empty.')
return
j = 0
p = self.head
while p.next != 0 and j < index:
p = p.next
j+=1
if j == index:
return p.data
else:
print ('target is not exist!') def insert(self,index,item):
if self.is_empty() or index < 0 or index > self.getlength():
print ('Linklist is empty.')
return
if index == 0:
q = Node(item,self.head)
self.head = q
j = 0
p = self.head
post = self.head
while p.next != 0 and j < index:
post = p
p = p.next
j+=1
if index == j:
q = Node(item,p)
post.next = q
q.prev = post
q.next = p
p.prev = q def delete(self,index):
if self.is_empty() or index < 0 or index > self.getlength():
print ('Linklist is empty.')
return
if index ==0:
q = Node(item,self.head)
self.head = q
j = 0
p = self.head
post = self.head
while p.next != 0 and j < index:
post = p
p = p.next
j+=1
if index ==j:
post.next = p.next
p.next.prev = post def index(self,value):
if self.is_empty():
print ('Linklist is empty.')
return
i = 0
p = self.head
while p.next != 0 and not p.data == value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1 if __name__ == '__main__':
l=LinkList()
llist=[7,3,10,4,5,]
l.initlist(llist) print(l.getlength())
print(l.is_empty()) # l.append(11)
# l.insert(2,100) print(l.getlength())
print(l.getitem(0))
for i in range(l.getlength()):
print(l.index(llist[i]))
l.clear()
print(l.getlength())
python——python 数据结构之双向链表的实现的更多相关文章
- python 与数据结构
在上面的文章中,我写了python中的一些特性,主要是简单为主,主要是因为一些其他复杂的东西可以通过简单的知识演变而来,比如装饰器还可以带参数,可以使用装饰类,在类中不同的方法中调用,不想写的太复杂, ...
- [0x00 用Python讲解数据结构与算法] 概览
自从工作后就没什么时间更新博客了,最近抽空学了点Python,觉得Python真的是很强大呀.想来在大学中没有学好数据结构和算法,自己的意志力一直不够坚定,这次想好好看一本书,认真把基本的数据结构和算 ...
- Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET
Python -- 堆数据结构 heapq - I love this game! - 博客频道 - CSDN.NET Python -- 堆数据结构 heapq 分类: Python 2012-09 ...
- python实现数据结构单链表
#python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...
- 《用Python解决数据结构与算法问题》在线阅读
源于经典 数据结构作为计算机从业人员的必备基础,Java, c 之类的语言有很多这方面的书籍,Python 相对较少, 其中比较著名的一本 problem-solving-with-algorithm ...
- 用Python实现数据结构之二叉搜索树
二叉搜索树 二叉搜索树是一种特殊的二叉树,它的特点是: 对于任意一个节点p,存储在p的左子树的中的所有节点中的值都小于p中的值 对于任意一个节点p,存储在p的右子树的中的所有节点中的值都大于p中的值 ...
- (python数据分析)第03章 Python的数据结构、函数和文件
本章讨论Python的内置功能,这些功能本书会用到很多.虽然扩展库,比如pandas和Numpy,使处理大数据集很方便,但它们是和Python的内置数据处理工具一同使用的. 我们会从Python最基础 ...
- python的数据结构分类,以及数字的处理函数,类型判断
python的数据结构分类: 数值型 int:python3中都是长整形,没有大小限制,受限内存区域的大小 float:只有双精度型 complex:实数和虚数部分都是浮点型,1+1.2J bool: ...
- Python 基本数据结构
Python基本数据结构 数据结构:通俗点儿说,就是存储数据的容器.这里主要介绍Python的4种基本数据结构:列表.元组.字典.集合: 格式如下: 列表:list = [val1, val2, va ...
- 转 Python常见数据结构整理
http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...
随机推荐
- CodeForces 738A Interview with Oleg
模拟. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...
- 并发系列5-大白话聊聊Java并发面试问题之微服务注册中心的读写锁优化【石杉的架构笔记】
- 洛谷——P1724 东风谷早苗
P1724 东风谷早苗 题目描述 在幻想乡,东风谷早苗是以高达控闻名的高中生宅巫女.某一天,早苗终于入手了最新款的钢达姆模型.作为最新的钢达姆,当然有了与以往不同的功能了,那就是它能够自动行走,厉害吧 ...
- linux shell date 用当天时间做备份文件名
#!/bin/bash #date 显示时间,我们可以用时间的不同做为备份文件的名字,这样以前的备份就不会被覆盖 datename=$(date +%Y%m%d-%H%M%S) ...
- [BZOJ 1857] 传送带
Link: BZOJ 1857 传送门 Solution: 首先中间的两个拐点$C,D$肯定都在传送带$A,B$上 接下来感性发现固定点A/C,另一个点C/D时间随位置的变化为单峰函数 这样就是三分套 ...
- 【数位dp】【二分】Gym - 101411H - Hotel in Ves Lagos
数位dp预处理之后,可以容易得到f(x),代表小于等于x的数中,有多少个不含13的.然后就能二分答案啦. #include<cstdio> #include<iostream> ...
- JDK源码学习笔记——Iterable/Iterator实现原理
Collection接口继承java.lang.Iterable接口,集合类重写了iterator()方法 public interface Iterable<T> { Iterator& ...
- HDU 4632 Palindrome subsequence (2013多校4 1001 DP)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- NHibernate 操作视图 第十三篇
在NHibernate中,可以把视图当表一样操作,只需要记住一点就是,视图是只读的,因此映射实体的setter应该改为protected. 新建一个视图如下: 持久化类: public class C ...
- 常用 U-boot命令详解
转:http://www.360doc.com/content/10/0827/13/496343_49168699.shtml 获取帮助环境变量与相关指令U-boot的使用网络命令Nand Flas ...