數據結構(北大公開課)
線性結構
有序表
1、有序表知識結構
2、有序表算法分析
3、有序表實現
#有序表
class OrderedList:
def __init__(self):
self.head=None
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 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)