python创建一个有序链表_python-数据结构代码 链表(无序链表、有序链表)

classNode:def __init__(self,initdata):

self.data=initdata

self.next=NonedefgetData(self):returnself.datadefgetNext(self):returnself.nextdefsetData(self,newdata):

self.data=newdatadefsetNext(self,newnext):

self.next=newnextclassUnorderedList:def __init__(self):

self.head=NonedefisEmpty(self):return self.head ==Nonedefadd(self,item):

temp=Node(item)

temp.setNext(self.head)

self.head=tempdefsize(self):

current=self.head

count=0while current !=None:

count=count+1current=current.getNext()returncountdefsearch(self,item):

current=self.head

found=Falsewhile current!-None and notfoune:if current.getData() ==item:

found=Trueelse:

current=current.getNext()returnfounddefremove(self,item):

current=self.head

previous=None

found=Falsewhile notfound:if current.getData()==item:

found=Trueelse:

previous=current

current=current.getNext()if previous==None:

self.head=current.getNext()else:

previous.setNext(current.getNext())classOrderedList:def __init__(self):

self.head=NonedefisEmpty(self):return self.head ==Nonedefsize(self):

current=self.head

count=0while current !=None:

count=count+1current=current.getNext()returncountdefremove(self,item):

current=self.head

previous=None

found=Falsewhile notfound:if current.getData()==item:

found=Trueelse:

previous=current

current=current.getNext()if previous==None:

self.head=current.getNext()else:

previous.setNext(current.getNext())defsearch(self,item):

current=self.head

found=False

stop=Falsewhile current!=None and not found and notstop:if current.getData()==item:

found=Trueelse:if current.getData()>item:

stop=Trueelse:

current=current.getNext()returnfounddefadd(self,item):

current=self.head

previous=None

stop=Falsewhile current!=None and notstop:if current.getData()>item:

stop=Trueelse:

previous=current

current=current.getNext()

temp=Node(item)if previous==None:

temp.setNext(self.head)

self.head=tempelse:

temp.setNext(current)

previous.setNext(temp)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值