数据结构(北大公开课)
线性结构
无序表
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())