1. 程式人生 > >單向迴圈連結串列的基本操作

單向迴圈連結串列的基本操作

‘’’
說明:連結串列的本質是列表
連結串列的簡單操作:
基礎方法:
is_empty()—判斷是否為空
length()—測量長度
travel()—遍歷連結串列
增:
add()—頭部插入
1append()—尾部插入
insert()—中間某個位置插入
刪:
remove()—刪除
查:
search()—查詢元素
‘’’
‘’’
實現步驟:1、建立結點類----用類方法
2、建立連結串列類----用物件方法
‘’’

class Node(object):
def init(self,elem):
self.elem = elem
self.next = None

class SingleLinklist(object):
def init

(self,node=None):
‘’‘建立self.__head私有屬性,其作為特殊的引導結構,含有指標功能。
讓其指向一個虛無空結點node,形成連結串列結構,預設此連結串列為空。
同時連結串列上的結點具有結點的elem和next屬性’’’
self.__head = node
if node:
node.next = node

def is_empty(self):
    return self.__head == None
def length(self):
   '''建立一個遊標cur用來移動遍歷結點。
      self__head指向第一個結點,此時的cur也指向第一個結點'''
   if self.is_empty():
       return 0
   else:
        cur = self.__head
        '''用count來計數結點個數'''
        count = 1
        while cur.next != self.__head:
            count +=1
            cur = cur.next
   return count

def travel(self):
    if self.is_empty():
        return None
    cur = self.__head
    while cur.next != self.__head:
       print(cur.elem,end='')
       cur = cur.next
    #尾節點需要打印出來
    print(cur.elem)


def add(self,item):
   '''頭插法:需要考慮self__head的指標指向、首結點指標指向、尾節點指標指向
             分為:空結點、只有一個結點、多結點
   '''
   node = Node(item)
   cur = self.__head
   while self.is_empty():
       self.__head = node
       node.next = node
   else:
        while cur.next != self.__head:
            cur = cur.next
            node.next = self.__head
        self.__head = node


def append(self,item):
    '''空連結串列'''
    node = Node(item)
    if self.is_empty():
       self.__head = node
       node.next = node
    else:
        cur = self.__head
        while cur.next != self.__head:
            cur = cur.next
        node.next = self.__head
        cur.next = node

def insert(self,pos,item):
    node = Node(item)
    '''pos是從0開始索引'''
    pre = self.__head
    count = 0
    if pos <= 0:
        self.add(item)
    while  pos > (self.length()-1):
        self.append(item)
    while count < (pos -1):
        count +=1
        pre = pre.next
    node.next = pre.next
    pre.next = node


def remove(self,item):
    '''要刪除結點分為首結點、中間結點、尾節點。同時判斷鏈上一共有多少個結點'''
    if self.is_empty():
        return False
    node = Node(item)
    cur = self.__head
    pre = None
    while cur.next != self.__head:
        if cur.elem == 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
    '''尾節點'''
    if cur.elem == item:
        if self.__head == cur:
            self.__head = None
        else:
            pre.next = cur.next


def search(self,item):
    node = Node(item)
    cur = self.__head
    if self.is_empty():
        return False
    while cur.next != self.__head:
        if cur.elem == item:
            return  True
        else:
            cur = cur.next
    if cur.elem == item:
        return True
    return False

if name == ‘main’:
ll = SingleLinklist()
print(ll.is_empty())
print(ll.length在這裡插入程式碼片