Introduction and list data structure
Also known as singly linked lists single linked list, the table is the simplest form, that each node contains two fields, an information field (field element) and a link field. The link points to the next node in the linked list, and the link field points to the last node is a null value.
Abstract data type definitions way linked list:
. Is_empty (): the list is empty
length ():. Chain length of
travel ():. Traverse the entire list
add (item):. Additive element list head
append (item).: the tail of the list add an element
insert (pos, item):. specify the location to add an element
remove (item):. delete node
search (item):. look for node exists
List of definitions
1 class Node(object): 2 def __init__(self, item): 3 self.item = item 4 self.next = None 5 6 7 class Link(object): 8 def __init__(self): 9 self._head = None 10 11 def isEmpty(self): 12 return self._head is None 13 14 @property 15 def length(self): 16 node = self._head 17 count = 0 18 while node: 19 count += 1 20 node = node.next 21 return count 22 23 def travel(self): 24 if self._head: 25 cur_node = self._head 26 while cur_node: 27 print(cur_node.item) 28 cur_node = cur_node.next 29 else: 30 raise ("<The object is empty!>") 31 32 def add(self, item): 33 node = Node(item) 34 node.next = self._head 35 self._head = node 36 37 def append(self, item): 38 node = Node(item) 39 if self.isEmpty(): 40 self._head = node 41 else: 42 cur_node = self._head 43 while cur_node: 44 pre_node, cur_node =cur_node, cur_node.next 45 pre_node.next = Node 46 is 47 DEF INSERT (Self, Item, index): 48 IF index <= 0: # insertion position index smaller than or equal to 0 is inserted in the head 49 self.add (Item) 50 elif index> = self.length: # insertion position is greater than the index is inserted in the list is equal to the length of the tail 51 is self.append (Item) 52 is the else : # insertion position index 1 to index between 1-chain length 53 is Node = the Node (Item) 54 is cur_node = self._head.next 55 pre = self._head 56 count = 1 57 while cur_node: 58 if count == index: 59 pre.next, node.next = node, cur_node 60 break 61 pre, cur_node = cur_node, cur_node.next 62 count += 1 63 64 def remove(self, item): 65 if self.isEmpty(): # 空提示 66 return "Failed because of Empty!" 67 cur_node = self._head 68 = pre_node None 69 the while cur_node: 70 IF cur_node.item == Item: 71 is IF pre_node == None: # deleted index 0 72 self._head = cur_node.next 73 is the else : 74 pre_node.next = cur_node.next 75 return Item 76 pre_node, cur_node = cur_node, cur_node.next 77 The raise ( " not found! " ) # finally given not found 78 79 def search(self, item): 80 if self.isEmpty(): 81 raise ("<The object is empty!>") 82 else: 83 cur_node = self._head 84 index = 0 85 while cur_node: 86 if cur_node.item == item: 87 return index 88 index += 1 89 cur_node = cur_node.next 90 return -1
Use the list
1 link = Link() 2 link.append(2) 3 link.append(3) 4 link.add(4) 5 link.insert(11, 1) 6 link.remove(4) 7 link.travel() 8 print(link.length) 9 print(link.search(11))