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

python 实现代码:
#!/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 __getitem__(self, key): if self.is_empty():
print 'linklist is empty.'
return elif key <0 or key > self.getlength():
print 'the given key is error'
return else:
return self.getitem(key) def __setitem__(self, key, value): if self.is_empty():
print 'linklist is empty.'
return elif key <0 or key > self.getlength():
print 'the given key is error'
return else:
self.delete(key)
return self.insert(key) 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): p = self.head
length = 0
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 p = self.head
post = self.head
j = 0
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 p = self.head
post = self.head
j = 0
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 p = self.head
i = 0
while p.next!=0 and not p.data ==value:
p = p.next
i+=1 if p.data == value:
return i
else:
return -1 l = LinkList()
l.initlist([1,2,3,4,5])
print l.getitem(4)
l.append(6)
print l.getitem(5) l.insert(4,40)
print l.getitem(3)
print l.getitem(4)
print l.getitem(5) l.delete(5)
print l.getitem(5) l.index(5)
结果为;
5
6
4
40
5
6
和单链表结果一样。
PS:双向链表就是将链表首尾相接。
python 数据结构之双向链表的实现的更多相关文章
- python——python 数据结构之双向链表的实现
和单链表类似,只不过是增加了一个指向前面一个元素的指针而已. 示意图: python 实现代码: #Personal Python Data Structure--PPDS # #!/usr/bin/ ...
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- python数据结构与算法
最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...
- python数据结构之图的实现
python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
- Python数据结构与算法--算法分析
在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...
- Python数据结构与循环语句
# Python数据结构与循环语句: 首先编程是一项技能,类似跑步,期初不必在意细节,能使用起来就行,等学的游刃有余了再回过头来关注细节问题也不迟. 关于买书: 学会python之后,才需要买书 ...
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...
- python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)
python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...
随机推荐
- 利用开源框架Volley来下载文本和图片。
Android Volley是Android平台上很好用的第三方开源网络通信框架.使用简单,功能强大. 下载连接地址:http://download.csdn.net/detail/zhangphil ...
- js基础之动画(一)
一.让div动起来 var oBtn = document.getElementById('btn1'); var timer='';//设置定时器 oBtn.onclick=function st ...
- warning malformed '#pragma pack(push[, id], n)' - ignored
bmp.c:8: warning: malformed '#pragma pack(push[, id], <n>)' - ignored bmp.c:33: warning: #prag ...
- [开发笔记]-使用bat命令来快速安装和卸载Service服务
一般我们在编写完Service服务程序后,都是通过cmd命令提示窗口来安装或卸载服务,但频繁的在cmd窗口中去“拼”文件的路径着实让人“不能忍”.所以,我们需要一钟“更快捷”的方式来进行安装或者卸载操 ...
- "琳琅满屋"调查问卷 心得体会及结果分析
·关于心得体会 当时小组提出这个校园二手交易市场的时候,就确定了对象范围,仅仅是面向在校大学生,而且在我们之前就已经有了很多成功的商品交易的例子可以让我们去借鉴,再加上我们或多或少的有过网 ...
- The Coco-Cola Store C(Contest #3 )
Once upon a time, there is a special coco-cola store. If you return three empty bottles to the shop, ...
- Android 之 数据存储
在Android操作系统中,提供了5种数据存储方式:SharedPreferences存储,文件存储,SQLite数据库存储,ContentProvider存储和网络存储. 一.SharedPrefe ...
- Android google map 两点之间的距离
在Android google map中,有时候会碰到计算两地的距离,下面的辅助类就可以帮助你计算距离: public class DistanceHelper { /** Names for the ...
- Visual Studio 2013 如何关闭调试而不关闭IIS Express
在VS主面板打开:工具->选项->调试->编辑继续 取消选中[启用"编辑并继续"] 就OK了 (英文版的请对应相应的操作) 不过这是针对所有的调试,如果你想针 ...
- 升级到EntityFramework 6的注意事项
参考: http://social.msdn.microsoft.com/Forums/vstudio/en-US/aa542153-b2a5-4b14-98a3-572f7b028c61/updat ...