ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 单向循环列表

2019-04-24 13:40:46  阅读:215  来源: 互联网

标签:head .__ cur python self 单向 next 列表 data


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

 

标签:head,.__,cur,python,self,单向,next,列表,data
来源: https://www.cnblogs.com/tele-share/p/10761864.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有