python双向链表实现实例代码


Posted in Python onNovember 21, 2013

示意图:
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

和单链表结果一样。

Python 相关文章推荐
Python实现子类调用父类的方法
Nov 10 Python
使用python绘制3维正态分布图的方法
Dec 29 Python
Python2 Selenium元素定位的实现(8种)
Feb 25 Python
python__name__原理及用法详解
Nov 02 Python
Python之指数与E记法的区别详解
Nov 21 Python
python科学计算之narray对象用法
Nov 25 Python
python 实现矩阵填充0的例子
Nov 29 Python
keras多显卡训练方式
Jun 10 Python
Python中logger日志模块详解
Aug 04 Python
Python如何急速下载第三方库详解
Nov 02 Python
Python 虚拟环境工作原理解析
Dec 24 Python
Python爬虫之爬取某文库文档数据
Apr 21 Python
python二叉树遍历的实现方法
Nov 21 #Python
python二叉树的实现实例
Nov 21 #Python
python冒泡排序算法的实现代码
Nov 21 #Python
python选择排序算法的实现代码
Nov 21 #Python
python插入排序算法的实现代码
Nov 21 #Python
python快速排序代码实例
Nov 21 #Python
python二分法实现实例
Nov 21 #Python
You might like
PHP UTF8编码内的繁简转换类
2009/07/20 PHP
php使用MySQL保存session会话的方法
2015/06/18 PHP
PHP中的switch语句的用法实例详解
2015/10/21 PHP
php引用和拷贝的区别知识点总结
2019/09/23 PHP
PHP safe_mode开启对于PHP系统函数有什么影响
2020/11/10 PHP
js两行代码按指定格式输出日期时间
2011/10/21 Javascript
jquery uploadify 在FF下无效的解决办法
2014/09/26 Javascript
jQuery中die()方法用法实例
2015/01/19 Javascript
JavaScript实现常用二级省市级联下拉列表的方法
2015/03/25 Javascript
详解nodejs中exports和module.exports的区别
2017/02/17 NodeJs
bootstrap精简教程_动力节点Java学院整理
2017/07/14 Javascript
Vue仿手机qq的实例代码(demo)
2017/09/08 Javascript
Vue2.0点击切换类名改变样式的方法
2018/08/22 Javascript
layui表格分页 记录勾选的实例
2019/09/02 Javascript
解决layui动态加载复选框无法选中的问题
2019/09/20 Javascript
[06:11]2014DOTA2国际邀请赛 专访团结一心的VG战队
2014/07/21 DOTA
python列表去重的二种方法
2014/02/14 Python
TensorFlow 滑动平均的示例代码
2018/06/19 Python
python儿童学游戏编程知识点总结
2019/06/03 Python
Python 的字典(Dict)是如何存储的
2019/07/05 Python
Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例
2020/04/11 Python
安装Anaconda3及使用Jupyter的方法
2020/10/27 Python
Django vue前后端分离整合过程解析
2020/11/20 Python
结构和类有什么异同
2012/07/16 面试题
教育专业个人求职信
2013/12/02 职场文书
绿化先进工作者事迹材料
2014/01/30 职场文书
销售经理竞聘书
2014/03/31 职场文书
应聘会计求职信
2014/06/11 职场文书
工地宣传标语
2014/06/18 职场文书
幽默自我介绍演讲稿
2014/08/21 职场文书
致800米运动员广播稿(10篇)
2014/10/17 职场文书
结婚喜宴迎宾词
2015/08/10 职场文书
python函数指定默认值的实例讲解
2021/03/29 Python
Python实现学生管理系统(面向对象版)
2021/06/24 Python
PHP使用QR Code生成二维码实例
2021/07/07 PHP
MySQL数据库如何查看表占用空间大小
2022/06/10 MySQL