1. 程式人生 > >python數據結構與算法(9)

python數據結構與算法(9)

text fff one nag true mar 單鏈表 while 循環鏈表

指定位置插?節點
技術分享圖片

def insert(self,    pos,    item):                              """在指定位置添加節點"""                             if  pos <=   0:                                              self.add(item)                              elif    pos >    (self.length()-1):                                              self.append(item)                               else:                                               node    =   Node(item)                                              cur =   self.__head                                             count   =   0                                               #   移動到指定位置的前?個位置                                               while   count   <    (pos-1):                                                                count   +=  1                                                               cur =   cur.next                                                #   將node的prev指向cur                                             node.prev   =   cur                                             #   將node的next指向cur的下?個節點                                               node.next   =   cur.next                                                #   將cur的下?個節點的prev指向node                                               cur.next.prev   =   node                                                #   將cur的next指向node                                             cur.next    =   node

刪除元素

技術分享圖片

def remove(self, item): """刪除元素""" cur = self.__head while cur != None: # 找到了要刪除的元素 if cur.item == item: # 先判斷此結點是否是頭節點 # 頭節點 if cur == self.__head: self.__head = cur.next # 如果存在下?個結點,則設置下?個結點 if cur.next: # 判斷鏈表是否只有?個結點 cur.next.prev = None else: cur.prev.next = cur.next # 如果存在下?個結點,則設置下?個結點 if cur.next: cur.next.prev = cur.prev break else: cur = cur.next

測試


if  __name__    ==  "__main__":             ll  =   DLinkList()             ll.add(1)               ll.add(2)               ll.append(3)
ll.insert(2,    4)              ll.insert(4,    5)              ll.insert(0,    6)              print   "length:",ll.length()               ll.travel()             print   ll.search(3)                print   ll.search(4)                ll.remove(1)                print   "length:",ll.length()
    ll.travel()

單向循環鏈表
單鏈表的?個變形是單向循環鏈表,鏈表中最後?個節點的next域不再為 None,?是指向鏈表的頭節點。

技術分享圖片
操作
is_empty() 判斷鏈表是否為空 length() 返回鏈表的?度 travel() 遍歷 add(item) 在頭部添加?個節點 append(item) 在尾部添加?個節點 insert(pos, item) 在指定位置pos添加節點 remove(item) 刪除?個節點 search(item) 查找節點是否存在
實現

class   Node(object):               """節點"""                def __init__(self,  item):                              self.item   =   item                                self.next   =   None
class   SinCycLinkedlist(object):               """單向循環鏈表"""                def __init__(self):                             self.__head =   None
    def is_empty(self):                             """判斷鏈表是否為空"""                              return  self.__head ==  None
                def length(self):                               """返回鏈表的?度"""                               #   如果鏈表為空,返回?度0                                if  self.is_empty():                                                return  0                               count   =   1                               cur =   self.__head                             while   cur.next    !=  self.__head:                                                count   +=  1                                               cur =   cur.next                                return  count
                def travel(self):                               """遍歷鏈表"""                              if  self.is_empty():                                                return                              cur =   self.__head                             print   cur.item,                               while   cur.next    !=  self.__head:                                                cur =   cur.next                                                print   cur.item,                               print   ""
                def add(self,   item):                              """頭部添加節點"""                                node    =   Node(item)                              if  self.is_empty():                                                self.__head =   node                                                node.next   =   self.__head                             else:                                               #添加的節點指向_head                                               node.next   =   self.__head #   移到鏈表尾部,將尾部節點的next指向node                                             cur =   self.__head                                             while   cur.next    !=  self.__head:                                                                cur =   cur.next                                                cur.next    =   node                                                #_head指向添加node的                                             self.__head =   node
                def append(self,    item):                              """尾部添加節點"""                                node    =   Node(item)                              if  self.is_empty():                                                self.__head =   node                                                node.next   =   self.__head                             else:                                               #   移到鏈表尾部                                              cur =   self.__head                                             while   cur.next    !=  self.__head:                                                                cur =   cur.next                                                #   將尾節點指向node                                              cur.next    =   node                                                #   將node指向頭節點_head                                             node.next   =   self.__head
                def insert(self,    pos,    item):                              """在指定位置添加節點"""                             if  pos <=   0:                                              self.add(item)                              elif    pos >    (self.length()-1):                                              self.append(item)                               else:                                               node    =   Node(item)                                              cur =   self.__head                                             count   =   0                                               #   移動到指定位置的前?個位置                                               while   count   <    (pos-1):                                                                count   +=  1                                                               cur =   cur.next
    node.next   =   cur.next                                                cur.next    =   node
                def remove(self,    item):                              """刪除?個節點"""                                #   若鏈表為空,則直接返回                             if  self.is_empty():                                                return                              #   將cur指向頭節點                               cur =   self.__head                             pre =   None                                while   cur.next    !=  self.__head:                                                if  cur.item    ==  item:                                                               #   先判斷此結點是否是頭節點                                                                if  cur ==  self.__head:                                                                                #   頭節點的情況                                                                              #   找尾節點                                                                                rear    =   self.__head                                                                             while   rear.next   !=  self.__head:                                                                                                rear    =   rear.next                                                                               self.__head =   cur.next                                                                                rear.next   =   self.__head                                                             else:                                                                               #   中間節點                                                                                pre.next    =   cur.next                                                                return                                              else:                                                               pre =   cur                                                             cur =   cur.next                                #   退出循環,cur指向尾節點                               if  cur.item    ==  item:                                               if  cur ==  self.__head:                                                                #   鏈表只有?個節點                                                                self.__head =   None                                                else:                                                               #   pre.next    =   cur.next                                                                pre.next    =   self.__head
    def search(self,    item):                              """查找節點是否存在"""                              if  self.is_empty():                                                return  False                               cur =   self.__head                             if  cur.item    ==  item:                                               return  True                                while   cur.next    !=  self.__head:                                                cur =   cur.next                                                if  cur.item    ==  item:                                                               return  True                                return  False
if  __name__    ==  "__main__":             ll  =   SinCycLinkedlist()              ll.add(1)               ll.add(2)               ll.append(3)                ll.insert(2,    4)              ll.insert(4,    5)              ll.insert(0,    6)              print   "length:",ll.length()               ll.travel()             print   ll.search(3)                print   ll.search(7)                ll.remove(1)                print   "length:",ll.length()               ll.travel()

python數據結構與算法(9)