數據結構(北大公開課)
線性結構
無序表
1、知識結構
2、無序表的實現
(1)節點
#鏈表實現:節點(Node)
class Node:
def __init__(self,initdata):
self.data=initdata
self.next=None
def getData(self):
return self.data
def getNext(self):
return seflf.next
def setData(self,newdata):
self.data=newdata
def setNext(self,newnext):
self.next=newnext
(2)鏈表
#無序表
class UnorderedList:
def __init__(self):
self.head=None
def add(self,item):
temp=Node(item) #新的節點
temp.setNext(self.head) #指向head所指
self.head=temp #head指向新增的節點
def size(self):
current=self.head
count=0
while current!=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())