单向链表
单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。

- 表元素域elem用来存放具体的数据。
- 链接域next用来存放下一个节点的位置(python中的标识)
- 变量p指向链表的头节点(首节点)的位置,从p出发能找到表中的任意节点。
单链表的操作
- is_empty() 链表是否为空
- length() 链表长度
- travel() 遍历整个链表
- add(item) 链表头部添加元素
- append(item) 链表尾部添加元素
- insert(pos, item) 指定位置添加元素
- remove(item) 删除节点
- search(item) 查找节点是否存在



1 # ------------节点实现------------ 2 class SingleNode(): 3 """单链表的结点""" 4 def __init__(self,elem): 5 # _item存放数据元素 6 self.elem=elem 7 # _next是下一个节点的标识 8 self.next=None 9 10 11 # ------------单链表的实现------------ 12 class SingleLinkList(): 13 """单链表""" 14 def __init__(self,node=None): 15 self.__head=node 16 17 def is_empty(self): 18 """判断链表是否为空""" 19 return self.__head==None 20 21 def length(self): 22 """链表长度""" 23 # cur初始时指向头节点 24 cur=self.__head 25 count=0 26 # 尾节点指向None,当未到达尾部时 27 while cur!=None: 28 count+=1 29 # 将cur后移一个节点 30 cur=cur.next 31 return count 32 33 def travle(self): 34 """遍历链表""" 35 cur=self.__head 36 while cur!=None: 37 print(cur.elem,end=' ') 38 cur=cur.next 39 print('') 40 41 def add(self,item): 42 """头部添加元素---头插法""" 43 node=SingleNode(item) 44 node.next=self.__head 45 self.__head=node 46 47 def append(self,item): 48 """尾部添加元素---尾插法""" 49 node=SingleNode(item) 50 # 先判断链表是否为空,若是空链表,则将_head指向新节点 51 if self.is_empty(): 52 self.__head=node 53 # 若不为空,则找到尾部,将尾节点的next指向新节点 54 else: 55 cur=self.__head 56 while cur.next!=None: 57 cur=cur.next 58 cur.next=node 59 60 def insert(self,pos,item): 61 """指定位置添加元素""" 62 # 若指定位置pos为第一个元素之前,则执行头部插入 63 if pos<=0: 64 self.add(item) 65 # 若指定位置超过链表尾部,则执行尾部插入 66 elif pos>(self.length()-1): 67 self.append(item) 68 # 找到指定位置 69 else: 70 node=SingleNode(item) 71 count=0 72 # pre用来指向指定位置pos的前一个位置pos-1,初始从头节点开始移动到指定位置 73 pre=self.__head 74 while count<(pos-1): 75 count+=1 76 pre=pre.next 77 # 先将新节点node的next指向插入位置的节点 78 node.next=pre.next 79 # 将插入位置的前一个节点的next指向新节点 80 pre.next=node 81 82 def search(self,item): 83 """链表查找节点是否存在,并返回True或者False""" 84 cur=self.__head 85 while cur!=None: 86 if cur.elem==item: 87 return True 88 else: 89 cur=cur.next 90 return False 91 92 def remove(self,item): 93 """删除节点""" 94 cur=self.__head 95 pre=None 96 while cur!=None: 97 # 找到了指定元素 98 if cur.elem==item: 99 # 如果第一个就是删除的节点 100 if cur==self.__head: 101 # 将头指针指向头节点的后一个节点 102 self.__head=cur.next 103 else: 104 # 将删除位置前一个节点的next指向删除位置的后一个节点 105 pre.next=cur.next 106 break 107 else: 108 # 继续按链表后移节点 109 pre=cur 110 cur=cur.next 111 112 if __name__ == '__main__': 113 li=SingleLinkList() 114 print(li.is_empty()) 115 print(li.length()) 116 117 li.append(1) 118 li.add(4) 119 li.append(2) 120 li.append(3) 121 # print(li.is_empty()) 122 # print(li.length()) 123 li.travle() 124 li.insert(-1,5) 125 li.travle() 126 li.insert(2,6) 127 li.travle() 128 li.insert(8,7) 129 li.travle() 130 print(li.search(5)) 131 li.remove(5) 132 li.travle() 133 li.remove(1) 134 li.travle() 135 li.remove(20) 136 li.travle()
1 #执行结果 2 True 3 0 4 4 1 2 3 5 5 4 1 2 3 6 5 4 6 1 2 3 7 5 4 6 1 2 3 7 8 True 9 4 6 1 2 3 7 10 4 6 2 3 7 11 4 6 2 3 7
posted on 2019-09-14 14:13 cherry_ning 阅读(167) 评论(0) 收藏 举报
浙公网安备 33010602011771号