码迷,mamicode.com
首页 > 其他好文 > 详细

循环链表

时间:2020-04-10 10:28:23      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:循环   param   循环链表   ini   pre   init   长度   append   arch   

  1 # -*- coding=utf-8 -*-
  2 # software: 算法学习
  3 # datetime:2020/4/9 9:14 上午
  4 
  5 
  6 class Node(object):
  7     """
  8     链表的节点对象:包含数据与和指针域
  9     """
 10 
 11     def __init__(self, data=None, next=None):
 12         self._value = data
 13         self._next = next
 14 
 15     def get_value(self):
 16         return self._value
 17 
 18     def get_next(self):
 19         return self._next
 20 
 21     def set_value(self, new_data):
 22         self._value = new_data
 23 
 24     def set_next(self, new_next):
 25         self._next = new_next
 26 
 27 
 28 class CycleLinkList(object):
 29 
 30     def __init__(self):
 31         # 声明一个尾节点
 32         self._tail = Node()
 33         self._tail.set_next(self._tail)
 34         self._length = 0
 35 
 36     def head(self):
 37         """
 38         链表的第一个元素(除去头节点)
 39         :return:
 40         """
 41         return self._tail.get_next()
 42 
 43     def tail(self):
 44         """
 45         链表的最后一个元素
 46         :return:
 47         """
 48         return self._tail
 49 
 50     def is_empty(self):
 51         """
 52         判断链表是否为空
 53         :return:
 54         """
 55         return self._tail.get_next() == self._tail
 56 
 57     def size(self):
 58         """
 59         链表的大小
 60         :return:
 61         """
 62         return self._length
 63 
 64     def add(self, value):
 65         """
 66         从头部插入节点
 67         :param value:
 68         :return:
 69         """
 70         new_node = Node()
 71         new_node.set_value(value)
 72         new_node.set_next(self._tail.get_next())
 73         self._tail.set_next(new_node)
 74         self._length += 1
 75 
 76     def append(self, value):
 77         """
 78         从尾部追加节点
 79         :param value:
 80         :return:
 81         """
 82         new_node = Node()
 83         new_node.set_value(value)
 84         new_node.set_next(self._tail)
 85         head = self._tail.get_next()
 86         tmp = head
 87         while head != self._tail:
 88             tmp = head
 89             head = head.get_next()
 90         tmp.set_next(new_node)
 91         self._length += 1
 92 
 93     def search(self, value):
 94         """
 95         查找数据,返回-1或者节点索引
 96         :param value:
 97         :return:
 98         """
 99         current = self._tail.get_next()
100         count = 0
101         while current != self._tail:
102             if current.get_value() == value:
103                 return count
104             current = current.get_next()
105             count += 1
106         else:
107             return -1
108 
109     def remove(self, value):
110         """
111         删除 返回该数据或者-1
112         :param value:
113         :return:
114         """
115         current = self._tail.get_next()
116         tmp = self._tail
117         while current != self._tail:
118             if current.get_value() == value:
119                 tmp.set_next(current.get_next())
120                 self._length -= 1
121                 return current.get_value()
122             tmp = current
123             current = current.get_next()
124         else:
125             return -1
126 
127     def insert(self, index, value):
128         """
129         插入数据节点
130         :param index:
131         :param value:
132         :return:
133         """
134         if index < 0:
135             return -1
136         new_node = Node()
137         new_node.set_value(value)
138         current = self._tail.get_next()
139         tmp = self._tail
140         count = 0
141         while current != self._tail:
142             if count == index:
143                 tmp.set_next(new_node)
144                 new_node.set_next(current)
145                 self._length += 1
146                 return value
147             count += 1
148             tmp = current
149             current = current.get_next()
150         else:
151             tmp.set_next(new_node)
152             new_node.set_next(current)
153             self._length += 1
154 
155 
156 if __name__ == __main__:
157     arr = [1, 4, 2, 6, 9, 12, 54, 3]
158     cycle_list = CycleLinkList()
159     for i in arr:
160         cycle_list.append(i)
161     current = cycle_list.tail().get_next()
162     tmp = cycle_list.tail()
163     l = ""
164     while current.get_next() != tmp:
165         l += str(current.get_value()) + " "
166         current = current.get_next()
167     l += str(current.get_value())
168     print("初始化遍历链表的值", l)
169     print("初始化链表长度", cycle_list.size())
170     print("删除数据:", cycle_list.remove(4))
171     print("插入数据", cycle_list.insert(1, 10))
172     print("当前链表长度", cycle_list.size())
173     print("查找数据:%s, 结果:%s" % (13, cycle_list.search(13)))
174     current = cycle_list.tail().get_next()
175     tmp = cycle_list.tail()
176     l = ""
177     while current.get_next() != tmp:
178         l += str(current.get_value()) + " "
179         current = current.get_next()
180     l += str(current.get_value())
181     print("当前遍历链表的值", l)

 

循环链表

标签:循环   param   循环链表   ini   pre   init   长度   append   arch   

原文地址:https://www.cnblogs.com/dreamall/p/12671653.html

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