码迷,mamicode.com
首页 > 编程语言 > 详细

python 双向链表

时间:2019-04-23 17:13:16      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:头部   print   coding   font   插入   app   link   move   访问   

  1 # -*- coding: utf-8 -*-
  2 # @author: Tele
  3 # @Time  : 2019/04/23 下午 3:19
  4 # 双向链表的实现
  5 # 双向链表的每个节点包含三个部分:上一个节点的链接区,数据区,下一个节点的链接区
  6 # 通过双向链表中的任意节点都能访问全部节点
  7 class DoubleNode:
  8     def __init__(self, data):
  9         self.data = data
 10         self.prev = None
 11         self.next = None
 12 
 13 
 14 class DoubleLinkedList:
 15     def __init__(self, data=None):
 16         node = DoubleNode(data)
 17         self.__head = node if node.data else None
 18 
 19     def is_empty(self):
 20         return self.__head == None
 21 
 22     def length(self):
 23         count = 0
 24         cur = self.__head
 25         while cur:
 26             count += 1
 27             cur = cur.next
 28         return count
 29 
 30     # 头部添加元素
 31     def add(self, data):
 32         node = DoubleNode(data)
 33         # 如果列表是空的,也可以用is_empty
 34         if not self.__head:
 35             self.__head = node
 36         else:
 37             self.__head.prev = node
 38             node.next = self.__head
 39             self.__head = node
 40 
 41     # 尾部添加元素
 42     def append(self, data):
 43         node = DoubleNode(data)
 44         if not self.__head:
 45             self.__head = node
 46         else:
 47             cur = self.__head
 48             # 最后一个节点的next为None
 49             while cur.next:
 50                 cur = cur.next
 51             cur.next = node
 52             node.prev = cur
 53 
 54     # 指定位置插入
 55     def insert(self, pos, data):
 56         node = DoubleNode(data)
 57         cur = self.__head
 58         count = 0
 59         if self.length() >= pos >= 0:
 60             while cur:
 61                 if count + 1 == pos:
 62                     node.next = cur.next
 63                     node.prev = cur
 64                     cur.next = node
 65                     break
 66                 # pos为0
 67                 elif count == pos:
 68                     self.add(data)
 69                     break
 70                 count += 1
 71                 cur = cur.next
 72         elif pos < 0:
 73             self.add(data)
 74         else:
 75             self.append(data)
 76         # 如果列表中插入时没有元素
 77         if not self.__head:
 78             self.append(data)
 79 
 80     # 遍历
 81     def travel(self):
 82         cur = self.__head
 83         while cur:
 84             print(cur.data)
 85             cur = cur.next
 86 
 87     def remove(self, data):
 88         count = 0
 89         cur = self.__head
 90         while cur:
 91             # 头节点
 92             if cur.data == data:
 93                 self.__head = cur.next
 94                 break
 95             elif cur.next.data == data:
 96                 if cur.next.next:
 97                     cur.next = cur.next.next
 98                     cur.next.next.prev = cur
 99                 # 尾节点
100                 else:
101                     cur.next = None
102                 break
103             count += 1
104             cur = cur.next
105 
106     # 移除出现的第一个元素
107     # def remove(self, data):
108     #     node = self.__find(data)
109     #     count = 0
110     #     cur = self.__head
111     #     while cur:
112     #         # 如果要移除的元素是头节点
113     #         if cur.data == node.data:
114     #             self.__head = cur.next
115     #             break
116     #         elif cur.next.data == node.data:
117     #             cur.next = node.next
118     #             # 如果node不是最后一个节点
119     #             if node.next:
120     #                 node.next.prev = cur
121     #             break
122     #         count += 1
123     #         cur = cur.next
124     # 私有方法,用于查找节点
125     # def __find(self, data):
126     #     index = -1
127     #     cur = self.__head
128     #     count = 0
129     #     node = DoubleNode(data)
130     #     while cur:
131     #         if cur.data == data:
132     #             index = count
133     #             node.next = cur.next
134     #             break
135     #         count += 1
136     #         cur = cur.next
137     #     return node
138 
139     # 查找,找不到返回-1,找到则返回索引
140     def search(self, data):
141         index = -1
142         cur = self.__head
143         count = 0
144         while cur:
145             if cur.data == data:
146                 index = count
147                 break
148             count += 1
149             cur = cur.next
150         return index
151 
152 
153 def main():
154     dll = DoubleLinkedList()
155     print(dll.is_empty())
156     print(dll.length())
157     dll.add(0)
158     dll.add(1)
159     dll.append(1)
160     dll.append(2)
161     print(dll.is_empty())
162     print(dll.length())
163     # print("*" * 50)
164     # dll.travel()
165     print("*" * 50)
166     dll.insert(-1, 10000)
167     print(dll.search(10000))  # 0
168     dll.travel()
169     dll.remove(1)
170     print("*" * 50)
171     dll.travel()
172 
173 
174 if __name__ == __main__:
175     main()

 

python 双向链表

标签:头部   print   coding   font   插入   app   link   move   访问   

原文地址:https://www.cnblogs.com/tele-share/p/10757409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!