from mynode import Node
class OrderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def length(self):
count = 0
temp = self.head
while temp:
count += 1
temp = temp.getNext()
return count
def add(self,item):
temp = self.head
pre = None
node = Node(item)
if self.isEmpty():
self.head = node
else:
while temp:
if temp.getData() > item:
if not pre == None:
node.setNext(temp)
pre.setNext(node)
break
else:
node.setNext(temp)
self.head = node
break
else:
pre = temp
temp = temp.getNext()
else:
pre.setNext(node)
def remove(self, item):
temp = self.head
pre = None
found = False
while (not found) and temp:
if temp.getData() == item:
found = True
else:
pre = temp
temp = temp.getNext()
if not temp:
raise ValueError(f'{item} not found in list')
else:
if not pre:
self.head = temp.getNext()
else:
pre.setNext(temp.getNext())
def search(self, item):
temp = self.head
while temp:
if temp.getData() == item:
return True
elif temp.getData() > item:
return False
else:
temp = temp.getNext()
else:
return False
def __repr__(self):
s = '['
temp = self.head
while temp:
if not temp.getNext():
s = s + f"{temp}"
temp = temp.getNext()
else:
s = s + f"{temp},"
temp = temp.getNext()
s = s + ']'
return s
def index(self, item):
count = 0
temp = self.head
while temp and (not temp.getData() == item):
count += 1
temp = temp.getNext()
if not temp:
raise ValueError(f'{item} not found in list')
else:
return count
def pop(self, pos=None):
if pos == None:
temp = self.head
pre = None
while temp.getNext():
pre = temp
temp = temp.getNext()
pre.setNext(None)
return temp
else:
if not isinstance(pos, int):
raise TypeError('pos needs int type')
if pos > self.length() - 1:
raise IndexError('pos out of range')
if pos < 0:
pos = self.length() + pos
if pos < 0:
raise IndexError('pos out of range')
temp = self.head
pre = None
if pos == 0:
self.head = temp.getNext()
return temp
else:
while pos:
pre = temp
temp = temp.getNext()
pos -= 1
pre.setNext(temp.getNext())
return temp
def __getitem__(self, index):
if not isinstance(index, int):
raise TypeError('index needs int type')
if index > self.length() - 1:
raise IndexError('index out of range')
if index < 0:
index = self.length() + index
if index < 0:
raise IndexError('index out of range')
temp = self.head
while index:
temp = temp.getNext()
index -= 1
return temp.getData()
def __setitem__(self, key, value):
if not isinstance(key, int):
raise TypeError('key needs int type')
if key > self.length() - 1:
raise IndexError('key out of range')
if key < 0:
index = self.length() + key
if index < 0:
raise IndexError('key out of range')
temp = self.head
while key:
temp = temp.getNext()
key -= 1
temp.setData(value)
有序列表OrderedList
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 题目 给定两个有序列表,大小分别为m和n。给出一个算法,以O(logn+logm)时间找出两个列表合并后的有序列表...
- 一、无序列表<ul> 1. 在无序列表中, 和 是配合使用的, 是 的父元素, 是 的子元素。 2. 的子元素必...
- (2017-11-19-周日 23:33:39) Enumerations are also hard to re...
- 给定两个长度为m和n的有序列表,以O(logm+logn)复杂度找出有序列表第k小的数 思路 logm+logn即...