數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)--雙向鏈表(python)

概念

雙向鏈表(Double_linked_list)也叫雙鏈表,是鏈表的一種,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有
兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。所以,從雙向鏈表中的任意一個(gè)結(jié)點(diǎn)開始,都可
以很方便地訪問它的前驅(qū)結(jié)點(diǎn)和后繼結(jié)點(diǎn)。

實(shí)現(xiàn)

class Node:
    def __init__(self, data):
        self.data = data    # 數(shù)據(jù)域
        self.next = None    # 指針域(直接后繼)
        self.prev = None    # 指針域(直接前驅(qū))


class DoubleLinkedList:
    """
    雙向鏈表
    """

    def __init__(self):
        self._head = None

    # 判斷鏈表是否為空
    def is_empty(self):
        return bool(self._head)

    # 返回鏈表長度
    @property
    def size(self):
        current = self._head
        count = 0
        while current is not None:
            count += 1
            current = current.next
        return current

    # 遍歷鏈表
    def travel(self):
        current = self._head
        while current is not None:
            print(current.data)
            current = current.next

    # 在鏈表頭部插入元素
    def add(self, value):
        new_node = Node(value)
        if self.is_empty():
            self._head = new_node
        else:
            new_node.next, self._head.prev = self._head, new_node
            self._head = new_node

    # 在鏈表尾部插入元素
    def append(self, value):
        new_node = Node(value)
        if self.is_empty():
            self._head = new_node
        else:
            _current = self._head
            while _current.next is not None:
                _current = _current.next
            _current.next, new_node.prev = new_node, _current

    # 查找元素是否存在
    def search(self, value):
        _current = self._head
        while _current is not None:
            if _current.data == value:
                return True
            _current = _current.next
        return False

    # 在指定位置插入節(jié)點(diǎn)
    def insert(self, position, value):
        if position < 0 or position > self.size:
            raise IndexError("Position out of range.")
        if position == 0:
            self.add(value)
        else:
            _node = Node(value)
            _current = self._head
            i = 0
            while i != position:
                i += 1
                _current = _current.next
            _prev = _current.prev
            _prev.next, _node.prev = _node, _prev
            _node.next, _current.prev = _current, _node

    # 刪除指定位置的節(jié)點(diǎn)
    def remove(self, position):
        if self.is_empty():
            return None
        if position < 0 or position > self.size - 1:
            raise IndexError("Position out of range.")
        _current = self._head
        i = 0
        while i != position:
            i += 1
            _current = _current.next
        _prev = _current.prev
        _next = _current.next
        _prev.next, _next.prev = _next, _prev
        _current.next, _current.prev = None, None
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一些概念 數(shù)據(jù)結(jié)構(gòu)就是研究數(shù)據(jù)的邏輯結(jié)構(gòu)和物理結(jié)構(gòu)以及它們之間相互關(guān)系,并對(duì)這種結(jié)構(gòu)定義相應(yīng)的運(yùn)算,而且確保經(jīng)過這...
    Winterfell_Z閱讀 6,601評(píng)論 0 13
  • 基本概念 鏈表的含義: 鏈表是一種用于存儲(chǔ)數(shù)據(jù)集合的數(shù)據(jù)結(jié)構(gòu),具有以下屬性 相鄰元素之間通過指針相連 最后一個(gè)元素...
    古劍誅仙閱讀 1,055評(píng)論 0 3
  • 目錄 1、屬性 2、鏈表和數(shù)組的區(qū)別 2.1、數(shù)組概述 2.2、數(shù)組和鏈表優(yōu)缺點(diǎn) 2.3、鏈表和數(shù)組的比較 3、單...
    我哈啊哈啊哈閱讀 2,936評(píng)論 1 41
  • 鏈表是線性表的鏈?zhǔn)酱鎯?chǔ)方式,邏輯上相鄰的數(shù)據(jù)在計(jì)算機(jī)內(nèi)的存儲(chǔ)位置不一定相鄰,那么怎么表示邏輯上的相鄰關(guān)系呢? 可以...
    rainchxy閱讀 2,250評(píng)論 0 6
  • 轉(zhuǎn)自:http://blog.csdn.net/oreo_go/article/details/52116214 ...
    YYT1992閱讀 1,278評(píng)論 0 4

友情鏈接更多精彩內(nèi)容