和萝莉做的电影网站,微信投票小程序怎么做,wordpress 企业主体,wordpress简约文艺1 问题 用python实现单链表的基础操作#xff1a;插入#xff0c;删除#xff0c;遍历#xff0c;判空#xff0c;清空链表#xff0c;求长度#xff0c;获取元素#xff0c;判断元素是否存在。 2 方法 解决问题的步骤采用如下方式#xff1a; 使用函数和类的方法来实… 1 问题 用python实现单链表的基础操作插入删除遍历判空清空链表求长度获取元素判断元素是否存在。 2 方法 解决问题的步骤采用如下方式 使用函数和类的方法来实现单链表的基本操作插入操作时使用头插法删除操作时删除头节点一行代码即可其他位置的需要判断遍历 通过实验、实践等证明提出的方法是有效的是能够解决开头提出的问题。 代码清单 1 class Node: 链表节点初始化 def __init__(self, item): self.item item self._next Noneclass LinkList: 链表及其相关操作 def __init__(self): self._head None def is_empty(self): 判断是否为空链表头节点为None则是空 return self._head is None def length(self): 求链表的长度 p self._head count 0 while p: count 1 p p._next return count def append(self, item): 向链表尾部添加元素, 考虑是否是空链表 node Node(item) p self._head if not p: self._head node else: while p._next: p p._next p._next node def add(self, item): 向链表头部插入元素 node Node(item) node._next self._head self._head node def insert(self, position, item): 向链表中插入元素 # 头插 or 尾插 or 中间插入 if position 0: self.add(item) elif position self.length(): self.append(item) else: pre self._head count 0 while count position - 1: count 1 pre pre._next node Node(item) node._next pre._next pre._next node def get_item(self, position): 获取某位置的元素 if position 0 or position self.length(): return None p self._head count 0 while count ! position: p p._next count 1 return p.item def exixt_value(self, item): 某个值是否存在 p self._head while p: if p.item item: return True else: p p._next return False def remove(self, item): 删除元素 p self._head pre None while p: if p.item item: # 是否头节点 if not pre: self._head p._next else: pre._next p._next break else: pre p p p._next def clear(self): 删除链表 self._head None def travel(self): 列表遍历 p self._head while p: print(p.item, end ) p p._next print()if __name__ __main__: linklist LinkList() linklist.append(2) linklist.append(3) linklist.append(4) linklist.append(5) print(linklist.length()) linklist.travel() linklist.add(1) linklist.add(0) linklist.travel() linklist.insert(2, 8) linklist.insert(2, 9) linklist.travel() print(linklist.get_item(2), linklist.get_item(12), linklist.get_item(4)) print(linklist.exixt_value(9), linklist.exixt_value(20)) linklist.remove(9) linklist.remove(5) linklist.travel() linklist.clear() linklist.travel() 3 结语 针对用python实现单链表的基础操作通过python运行实验证明该方法是有效的这种设置方法代码较多因此未来还需继续改善这种方法以适应更多场景。
文章转载自: http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.tzjqm.cn.gov.cn.tzjqm.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.dkbsq.cn.gov.cn.dkbsq.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.gmmxh.cn.gov.cn.gmmxh.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.eviap.com.gov.cn.eviap.com http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn http://www.morning.zmyhn.cn.gov.cn.zmyhn.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.ngcbd.cn.gov.cn.ngcbd.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.jmmzt.cn.gov.cn.jmmzt.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.hxbjt.cn.gov.cn.hxbjt.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.wzwpz.cn.gov.cn.wzwpz.cn http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.kdpal.cn.gov.cn.kdpal.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.fnywn.cn.gov.cn.fnywn.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.tjjkn.cn.gov.cn.tjjkn.cn http://www.morning.ydzly.cn.gov.cn.ydzly.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.sjpbh.cn.gov.cn.sjpbh.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.vjdofuj.cn.gov.cn.vjdofuj.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.gkgr.cn.gov.cn.gkgr.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.nwllb.cn.gov.cn.nwllb.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn