最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python雙向鏈表實(shí)現(xiàn)實(shí)例代碼

 更新時(shí)間:2013年11月21日 14:20:34   作者:  
python雙向鏈表和單鏈表類(lèi)似,只不過(guò)是增加了一個(gè)指向前面一個(gè)元素的指針,下面的代碼實(shí)例了python雙向鏈表的方法

示意圖:

python雙向鏈表實(shí)現(xiàn)代碼

復(fù)制代碼 代碼如下:

#!/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)

結(jié)果為;

5
6
4
40
5
6

和單鏈表結(jié)果一樣。

相關(guān)文章

  • python爬取各省降水量及可視化詳解

    python爬取各省降水量及可視化詳解

    本文是學(xué)習(xí)python,故選取了python最常用的爬蟲(chóng)作為實(shí)操訓(xùn)練同時(shí),還添加了可視化和GUI入門(mén)的內(nèi)容使爬取的內(nèi)容應(yīng)用更豐富,需要的朋友可以參考下
    2021-04-04
  • python顯示生日是星期幾的方法

    python顯示生日是星期幾的方法

    這篇文章主要介紹了python顯示生日是星期幾的方法,涉及Python使用date模塊操作日期的技巧,需要的朋友可以參考下
    2015-05-05
  • Python實(shí)現(xiàn)簡(jiǎn)單的多任務(wù)mysql轉(zhuǎn)xml的方法

    Python實(shí)現(xiàn)簡(jiǎn)單的多任務(wù)mysql轉(zhuǎn)xml的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單的多任務(wù)mysql轉(zhuǎn)xml的方法,結(jié)合實(shí)例形式分析了Python查詢(xún)mysql結(jié)果集轉(zhuǎn)xml格式數(shù)據(jù)輸出的相關(guān)操作技巧,需要的朋友可以參考下
    2017-02-02
  • 淺談Python3中strip()、lstrip()、rstrip()用法詳解

    淺談Python3中strip()、lstrip()、rstrip()用法詳解

    這篇文章主要介紹了淺談Python3中strip()、lstrip()、rstrip()用法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-04-04
  • python基礎(chǔ)教程之while循環(huán)

    python基礎(chǔ)教程之while循環(huán)

    這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)教程之while循環(huán)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Django中Migrate和Makemigrations實(shí)操詳解

    Django中Migrate和Makemigrations實(shí)操詳解

    這篇文章主要為大家介紹了Django中Migrate和Makemigrations實(shí)操詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Python使用Supervisor來(lái)管理進(jìn)程的方法

    Python使用Supervisor來(lái)管理進(jìn)程的方法

    這篇文章主要介紹了Python使用Supervisor來(lái)管理進(jìn)程的方法,涉及Supervisor的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • 使用numpy和PIL進(jìn)行簡(jiǎn)單的圖像處理方法

    使用numpy和PIL進(jìn)行簡(jiǎn)單的圖像處理方法

    今天小編就為大家分享一篇使用numpy和PIL進(jìn)行簡(jiǎn)單的圖像處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • 一勞永逸徹底解決pip install慢的辦法

    一勞永逸徹底解決pip install慢的辦法

    經(jīng)常在使用Python的時(shí)候需要安裝各種模塊,而pip是很強(qiáng)大的模塊安裝工具,這篇文章主要給大家介紹了一個(gè)可以一勞永逸徹底解決pip install慢的辦法,需要的朋友可以參考下
    2021-05-05
  • PyCharm 2020 激活到 2100 年的教程

    PyCharm 2020 激活到 2100 年的教程

    這篇文章主要介紹了PyCharm 2020 激活到 2100 年,本文圖文并茂給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評(píng)論

贵州省| 黄山市| 西充县| 翁源县| 鹤岗市| 来凤县| 澄江县| 江川县| 南城县| 根河市| 高安市| 丰都县| 新昌县| 仪征市| 宝坻区| 揭西县| 连云港市| 翁牛特旗| 禹城市| 文昌市| 米林县| 凤城市| 邓州市| 苍山县| 吴旗县| 平定县| 平湖市| 太仆寺旗| 萝北县| 朝阳市| 广平县| 盐城市| 乐陵市| 津南区| 肃宁县| 横山县| 舞钢市| 含山县| 石景山区| 建德市| 灵武市|