欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python單鏈表實現(xiàn)代碼實例

 更新時間:2013年11月21日 14:25:13   作者:  
這篇文章主要介紹了python單鏈表實現(xiàn)代碼,大家參考使用吧

鏈表的定義:
鏈表(linked list)是由一組被稱為結點的數(shù)據(jù)元素組成的數(shù)據(jù)結構,每個結點都包含結點本身的信息和指向下一個結點的地址。由于每個結點都包含了可以鏈接起來的地址信息,所以用一個變量就能夠訪問整個結點序列。也就是說,結點包含兩部分信息:一部分用于存儲數(shù)據(jù)元素的值,稱為信息域;另一部分用于存儲下一個數(shù)據(jù)元素地址的指針,稱為指針域。鏈表中的第一個結點的地址存儲在一個單獨的結點中,稱為頭結點或首結點。鏈表中的最后一個結點沒有后繼元素,其指針域為空?! ?/P>

python單鏈表實現(xiàn)代碼:

復制代碼 代碼如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

class Node(object):
    def __init__(self,val,p=0):
        self.data = val
        self.next = 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
            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


    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.next = p


    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

    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監(jiān)控主機是否存活并以郵件報警

    Python監(jiān)控主機是否存活并以郵件報警

    本文是利用python腳本寫的簡單測試主機是否存活,此腳本有個缺點不適用線上,由于網(wǎng)絡延遲、丟包現(xiàn)象會造成誤報郵件,感興趣的朋友一起看看Python監(jiān)控主機是否存活并以郵件報警吧
    2015-09-09
  • PyCharm連接遠程服務器的超級詳細教程

    PyCharm連接遠程服務器的超級詳細教程

    Pycharm可以與服務器建立連接,把相應的項目同步到服務器上,下面這篇文章主要給大家介紹了關于PyCharm連接遠程服務器的超級詳細教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • python實現(xiàn)一個簡單的ping工具方法

    python實現(xiàn)一個簡單的ping工具方法

    今天小編就為大家分享一篇python實現(xiàn)一個簡單的ping工具方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python多重繼承新算法C3介紹

    python多重繼承新算法C3介紹

    這篇文章主要介紹了python多重繼承新算法C3介紹,多重繼承需要復雜的算法,本文就詳細講解了新算法C3,需要的朋友可以參考下
    2014-09-09
  • Python制作微信好友背景墻教程(附完整代碼)

    Python制作微信好友背景墻教程(附完整代碼)

    這篇文章主要介紹了Python制作微信好友背景墻教程(附完整代碼),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Python的Twisted框架中使用Deferred對象來管理回調函數(shù)

    Python的Twisted框架中使用Deferred對象來管理回調函數(shù)

    當說起Twisted的異步與非阻塞模式等特性時,回調函數(shù)的使用在其中自然就顯得不可或缺,接下來我們就來看Python的Twisted框架中使用Deferred對象來管理回調函數(shù)的用法.
    2016-05-05
  • 詳解Python中的from..import絕對導入語句

    詳解Python中的from..import絕對導入語句

    絕對導入其實非常簡單,即是用from語句在import前指明頂層package名,下面我們通過兩個例子來詳解Python中的from..import絕對導入語句
    2016-06-06
  • python實現(xiàn)寫數(shù)字文件名的遞增保存文件方法

    python實現(xiàn)寫數(shù)字文件名的遞增保存文件方法

    今天小編就為大家分享一篇python實現(xiàn)寫數(shù)字文件名的遞增保存文件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 使用Djongo模塊在Django中使用MongoDB數(shù)據(jù)庫

    使用Djongo模塊在Django中使用MongoDB數(shù)據(jù)庫

    Django框架為我們提供了簡潔方便的ORM模型供我們對數(shù)據(jù)庫進行各種操作,但是這個“數(shù)據(jù)庫”卻并不包括NoSQL的典型——MongoDB。不少Django初學者也會到處詢問,如何才能在Django中使用MongoDB。本文將介紹使用Djongo來在Django中集成MongoDB數(shù)據(jù)庫
    2021-06-06
  • python通過函數(shù)屬性實現(xiàn)全局變量的方法

    python通過函數(shù)屬性實現(xiàn)全局變量的方法

    這篇文章主要介紹了python通過函數(shù)屬性實現(xiàn)全局變量的方法,實例分析了Python中函數(shù)屬性的相關使用技巧,需要的朋友可以參考下
    2015-05-05

最新評論