1.什么是无序表?
- 一种数据项按照相对位置存放的数据集,特别的,被称为“无序集unordered list”,其中数据项只按照存放位置来索引,如第一个、第二个等等。
2.无序表的定义
- List():创建一个空列表
- add(item):添加一个数据项到列表中,假设item原来不存在于列表中
- remove(item):从列表中移除item,列表被修改,item原先存在应存在表中
- search:在列表中查找item,返回布尔类型值
- is_Empty:返回列表是否为空
- size:返回列表包含了多少数据项
- append(item):添加一个数据项到表末尾,假设item原先不存在列表中
- index(item):返回数据项在表中的位置
- insert(pos,item):将数据项插入到位置pos,假设item原先不存在与列表中,同时原列表具有足够多个数据项,能让item占据位置pos
- pos():从列表末尾移除数据项,假设原列表至少有1个数据项
- pop(pos):移除位置为pos的数据项,假设原列表存在位置pos
3.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 UnorderndList:
def __init__(self):
self.head = None
def is_Empty(self):
"""判断链表是否为空"""
return self.head == None
def add(self,item):
"""添加数据"""
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
"""返回数据项的个数"""
current = self.head
count = 0
while != None:
count = count + 1
current = current.getNext()
return count
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
def remove(self,item):
"""删除数据项"""
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous = current
current = current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
def append(self,item):
"""在链表尾部插入item"""
node = Node(item)
current = self.head
if current is None:
self.head = node
while current.getNext() is not None:
current = current.getNext()
current.setNext(node)
def index(self, item):
"""查询元素item"""
current = self.head
count = 0
while current != None:
if current.getData() == item:
return count
current = current.getNext()
count += 1
return -1
def pop(self, index=None):
"""删除尾部元素"""
previous = None
current = self.head
if index == None:
index = self.size() - 1
if index < 0 or index >= (self.size()):
raise IndexError
while self.index(current.getData()) != index:
previous = current
current = current.getNext()
data = current.getData()
if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext())
return data
def insert(self, index, item):
"""在item插入元素"""
node = Node(item)
if index == 0:
node.setNext(self.head)
self.head = node
else:
if index < 0 or (index >= self.size()):
raise IndexError
current = self.head
previous = None
while self.index(current.getData()) != index:
previous = current
current = current.getNext()
if current is None:
break
previous.setNext(node)
node.setNext(current)
def reverseList(self):
"""链表反向排序"""
previous = None
temp = None
current = self.head
while current != None:
temp = current.getNext()
current.setNext(previous)
previous = current
current = temp
if previous != self.head:
self.add(previous.getData())
return self
|