class Node:
def __init__(self,init_data):
self.data=init_data
self.next=None
def get_data(self):
return self.data
def get_next(self):
return self.next
def set_next(self,new_next):
self.next=new_next
class List:
def __init__(self):
self.head=None
def is_empty(self):
return self.head==None
def add(self,item):
temp=Node(item)
temp.set_next(self.head)
self.head=temp
def size(self):
current=self.head
count=0
while current!=None:
count+=1
current=current.get_next()
return count
def search(self,item):
current=self.head
found=False
while current!=None and not found:
if current.get_data()==item:
found=True
else:
current=current.get_next()
return found
def remove(self,item):
current=self.head
previous=None
found=False
while not found:
if current.get_data()==item:
found=True
else:
previous,current=current,current.get_next()
if previous==None:
self.head=current.get_next()
else:
previous.set_next(current.get_next())
class OrderList:
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.get_data()==item:
found=True
else:
if current.get_data()>item:
stop=True
else:
current=current.get_next()
return found
def add(self,item):
current=self.head
previous=None
stop=False
while current!=None and not stop:
if current.get_data()>item:
stop=True
else:
previous=current
current=current.get_next()
temp=Node(item)
if previous==None:
temp.set_next(self.head)
self.head=temp
else:
temp.set_next(current)
previous.set_next(temp)
实现有序列表和无序列表
最新推荐文章于 2023-02-11 19:28:35 发布