python 实现单链表

#! /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()
时间: 2024-08-10 23:28:35

python 实现单链表的相关文章

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 class Node:     def __init__(self,data=None,next = None):         self.data = data         self.next = next def rev(link):     pre = link     cur = l

python实现单链表

#encoding:utf-8 import sys class Lnode():     def __init__(self,elem,next=None):         self.elem = elem    #节点的值         self.next = next    #指向下一个节点     def getelem(self):         return self.elem     def getnext(self):         return self.next cl

Python 使用单链表实现堆栈 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-1-28 @author: beyondzhou @name: test_linkliststack.py ''' def test_linkliststack(): # import linkListStack from mystack import LinkListStack print '#Init a stack named smith using push' sm

Python 使用单链表实现队列 (基于class, 包含迭代器)

#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015-2-3 @author: beyondzhou @name: test_linklistqueue.py ''' def test_linklistqueue(): # import pyListQueue from myqueue import linkListQueue print '#Init a queue named smith using enqueue' sm

用最简单的方式学Python单链表

Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式截然不同 什么是单链表 单链表 最简单的形式就是由多个节点的集合共同构成一个线性序列.每个节点存储一个对象的引用,这个引用指向序列中的一个元素,即存储指向列表的下一个节点. 其实,上面的术语用生活中的大白话来解释,就是我们现在有三个人--我.你.他.当我用手指指向你,你用手指指向他,这样就形成了一个

数据结构:单链表结构字符串(python版)

1 #!/urs/bin/env python 2 # -*- coding:utf-8 -*- 3 4 #异常类 5 class stringTypeError(TypeError): 6 pass 7 8 #节点类 9 class Node(object): 10 def __init__(self, elem, next_ = None): 11 self.elem = elem 12 self.next = next_ 13 #单链表类 14 class single_list(obje

python单链表实例分享

有关python单链表的实现代码. 链表的定义:链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就能够访问整个结点序列.也就是说,结点包含两部分信息:一部分用于存储数据元素的值,称为信息域:另一部分用于存储下一个数据元素地址的指针,称为指针域.链表中的第一个结点的地址存储在一个单独的结点中,称为头结点或首结点.链表中的最后一个结点没有后继元素,其指针域为空. p

单链表反转python实现

单链表的反转可以使用循环,也可以使用递归的方式 1.循环反转单链表 循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可. 代码: class ListNode: def __init__(self,x): self.val=x; self.next=None; def nonrecurse(head): #循环的方法反转链表 if head is None or head.next is None: return head; pre=None; c

数据结构:单链表结构字符串(python版)添加了三个新功能

1 #!/urs/bin/env python 2 # -*- coding:utf-8 -*- 3 4 #异常类 5 class stringTypeError(TypeError): 6 pass 7 8 #节点类 9 class Node(object): 10 def __init__(self, elem, next_ = None): 11 self.elem = elem 12 self.next = next_ 13 #单链表类 14 class single_list(obje