列表是元素的集合,其中每一个元素都有一个相对于其他元素的位置。更具体地说,这种列表称为无序列表,可以称列表地第一个元素为列表的起点,称最后一个元素为列表的终点。在有序列表中,元素的相对位置取决于它们的基本特征。它们通常以升序或降序排列(前提是元素之间能进行有意义的比较)。
实现无序列表
为了实现无序列表,需要构建链表。无序列表需要维持元素之间的相对位置,但是并不需要在连续的内存空间中维护这些位置信息。Python 实现无序列表:
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, newdata):
self.data = newdata
def setNext(self, newnext):
self.next = newnext
class UnorderedList:
def __init__(self):
self.head = None
# 检查列表是否为空
def isEmpty(self):
return self.head == None
# 返回列表中元素的个数
def length(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
# 假设元素 item 之前不在列表中,并向其中添加 item,无返回值
def add(self, item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
# 假设元素 item 之前不在列表中,并在列表的最后位置添加 item,无返回值
def append(self, item):
current = self.head
previous = None
while current != None:
previous = current
current = current.getNext()
temp = Node(item)
temp.setNext(current)
previous.setNext(temp)
# 假设元素 item 之前不在列表中,在位置 pos 处添加元素 item,无返回值
def insert(self, pos, item):
current = self.head
previous = None
index = 0
temp = Node(item)
if 0 == pos:
temp.setNext(current.getNext())
self.head = temp
else:
while current != None:
previous = current
current = current.next
index = index + 1
if index == pos:
temp.setNext(current)
previous.setNext(temp)
break
# 在列表中搜索元素 item,返回布尔值
def search(self, item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
# 假设元素 item 已经在列表中,返回该元素的下标
def index(self, item):
current = self.head
found = False
count = -1
while current != None and not found:
count = count + 1
if current.getData() == item:
found = True
else:
current = current.getNext()
if found:
return count
else:
return -1
# 假设元素 item 已经在列表中,并向其中添加 item,无返回值
def remove(self, item):
current = self.head
previous = None
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
if current.getNext() != None:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
if current != None:
previous.setNext(current.getNext())
else:
previous.setNext(current)
# 若接受 pos 参数,则移除该位置的元素,若不接受 pos 参数,则移除最后一个元素
def pop(self, pos = None):
if pos == None:
# print("pop()")
current = self.head
previous = None
while current.getNext() != None:
previous = current
current = current.getNext()
if previous == None:
self.head = None
else:
previous.next = current.getNext()
return current.getData()
else:
# print("pop(pos)")
current = self.head
previous = None
index = 0
if 0 == pos:
self.head = current.getNext()
else:
while current != None:
previous = current
current = current.next
index = index + 1
if index == pos:
previous.setNext(current.getNext())
break
return current.getData()
# 打印列表元素
def printlist(self):
current = self.head
while current != None:
print(current.getData(), end=' ')
current = current.getNext()
print()
实现有序列表
Python 实现有序列表:
class Node:
def __init__(self, initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self, newdata):
self.data = newdata
def setNext(self, newnext):
self.next = newnext
class OrderedList:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def length(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def add(self, item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)
def search(self, item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:
found = True
else:
if current.getData() > item:
stop = True
else:
current = current.getNext()
return found
def index(self, item):
current = self.head
found = False
count = -1
while current != None and not found:
count = count + 1
if current.getData() == item:
found = True
else:
current = current.getNext()
if found:
return count
else:
return -1
def remove(self, item):
current = self.head
previous = None
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
if current.getNext() != None:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
if current != None:
previous.setNext(current.getNext())
else:
previous.setNext(current)
def pop(self, pos = None):
if pos == None:
# print("pop()")
current = self.head
previous = None
while current.getNext() != None:
previous = current
current = current.getNext()
if previous == None:
self.head = None
else:
previous.next = current.getNext()
return current.getData()
else:
# print("pop(pos)")
current = self.head
previous = None
index = 0
if 0 == pos:
self.head = current.getNext()
else:
while current != None:
previous = current
current = current.next
index = index + 1
if index == pos:
previous.setNext(current.getNext())
break
return current.getData()
def printlist(self):
current = self.head
while current != None:
print(current.getData(), end=' ')
current = current.getNext()
print()