#! /usr/bin/env python 

###
### Linked List python implementation
###
### @reference Data Structures and Algorithm Analysis in C:Second Edition : Mark Allen Weiss
### @date Tue Sep 29 20:51:55 CST 2015 #node structure
class Node(object): def __init__(self, value, p=None):
self.element = value
self.pNext = p class LinkedList(object): def __init__(self):
self.head = None def makeEmpty(self):
self.head = None def isEmpty(self):
return self.head == None def find(self, value):
if self.isEmpty():
print 'the linked list is empty !'
return
p = self.head
while p != None:
if p.element == value:
#the index of the target in linkedlist
return p
p = p.pNext
return -1 def insert(self, value):
item = Node(value)
if self.isEmpty():
self.head = item
else:
p = self.head
while p.pNext != None:
p = p.pNext
p.pNext = item def deleteList(self):
if self.isEmpty():
print 'the linked list is empty !'
else:
p = self.head.pNext
self.head = None
while p != None:
tmp = p.pNext
p = None
p=tmp def delete(self, target):
if self.isEmpty():
print 'the linked list is empty !'
else:
p = self.findPrevious(target)
if not self.isLast(p):
tmpNode = p.pNext
p.pNext = tmpNode.pNext
tmpNode = None
else:
p.pNext = None def isLast(self,p):
return p.pNext == None def findPrevious(self, target):
if self.isEmpty():
print 'the linked list is empty !'
else:
p = self.head
while p != None and p.pNext.element != target:
p = p.pNext
return p def debug(self):
if self.isEmpty():
print 'the linked list is empty !'
else:
p = self.head
while p != None:
print p.element
p = p.pNext
if p == None:
print '-------------' def initLinkedList(self,lists):
for item in lists:
self.insert(item) obj=LinkedList()
lists=[1,2,3,4,5,6,10,17]
obj.initLinkedList(lists)
#rs=obj.isEmpty()
#print rs
#rs=obj.find(17)
#print rs
#rs=obj.isLast(rs)
#print rs
#obj.debug()
#rs=obj.find(17)
#rs=obj.find(14)
#rs=obj.findPrevious(10)
#print rs
#print rs.element
#obj.delete(10)
obj.deleteList()
obj.debug()

python 实现单链表的更多相关文章

  1. Python实现单链表数据的添加、删除、插入操作

    Python实现单链表数据的添加.删除.插入操作 链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结 ...

  2. python实现单链表反转(经典笔试题)

    https://blog.csdn.net/su_bao/article/details/81072849 0.说在前面的话 链表结构,说难不难,说易不易,一定要亲自编程实现一下.其次就是一定要耐心, ...

  3. 数据结构与算法-python描述-单链表

    # coding:utf-8 # 单链表的相关操作: # is_empty() 链表是否为空 # length() 链表长度 # travel() 遍历整个链表 # add(item) 链表头部添加元 ...

  4. python实现单链表及链表常用功能

    单链表及增删实现 单链表高级功能实现:反序,找中间结点,检测环等 参考: https://github.com/wangzheng0822/algo

  5. 基于python实现单链表代码

    1 """ 2 linklist.py 3 单链表的构建与功能操作 4 重点代码 5 """ 6 7 class Node: 8 " ...

  6. python实现单链表的翻转

    #!/usr/bin/env python #coding = utf-8 class Node:     def __init__(self,data=None,next = None):      ...

  7. python实现单链表的反转

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #!/usr/bin/env python #coding = utf-8 ...

  8. 【算法编程 C++ python】单链表反序输出

    题目描述 输入一个链表,从尾到头打印链表每个节点的值.   以下方法仅仅实现了功能,未必最佳.在牛客网测试, C++:3ms 480k Python:23ms 5732k /** * struct L ...

  9. python实现 单链表的翻转

    #!/usr/bin/env python #coding = utf-8 class Node: def __init__(self,data=None,next = None): self.dat ...

随机推荐

  1. 提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit

    this bundle is invalid . new apps and app updates submitted to the app store must be built with publ ...

  2. 执行CMD代码

    /// <summary> /// 发送CMD命令(执行命令行) /// </summary> public static void SendCMD(string pwd) { ...

  3. jsp相对路径和绝对路径小谈

    很长一段时间纠结过JSP中的相对路径和绝对路径,也研究过一段时间,今天趁着有点时间,记下来,也有大家分享一下. 1)我们先来理解一下相对路径 首先还是我们的开始,建一个WEB项目,只是测试一下而已,名 ...

  4. MariaDB忘记root密码

    在MariaDB配置文件/etc/my.cnf  [mysqld]中加入skip-grant-tables一行: [Richard@localhost ~]$ sudo vi /etc/my.cnf[ ...

  5. graph使泳道图的label横向显示

    1.如果需要将label靠左边对齐,则必须重写底层源码 新增mxText的一个构造器,主要是增加了一个参数:x(代表当前的cell) function mxText(a, b, c, d, e, f, ...

  6. oFixed() 方法

    oFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 在本例中,我们将把数字舍入为仅有一位小数的数字: Show the number 13.37 with one decimal ...

  7. Windows 去掉启动时的放大镜

    控制面板-轻松访问中心-使计算机更易于显示不勾选 启用放大镜

  8. [poco] HttpRequest之post方法

    转自 http://www.cnblogs.com/yuanxiaoping_21cn_com/archive/2012/06/10/2544032.html #import <iostream ...

  9. 如何实现select组件的选择输入过滤作用

    实现select组件的选择输入过滤作用的js代码如下: /** *其中//******之间的部分显示的是在没有选择输入过滤功能的代码上加入的功能代码 ** / (function ( $ ) { $. ...

  10. LeakCanary,检测安卓,java内存泄漏

    官方中文API地址:http://www.liaohuqiu.net/cn/posts/leak-canary-read-me/