python链表的实现,有注释
class Node(): #node实现,每个node分为两部分:一部分含有链表元素,成数据域;另一部分为指针,指向下一个
__slots__=['_item','_next'] #限定node实例的属性??? 这个元素就是有两部分组成,,,一个指针一个数据,,,但是链表头就不是这样了
def __init__(self,item):
self._item=item
self._next=None #node的指针部分默认指向none,这个是单链表,所以每个元素只有一个指针
def getItem(self):
return self._item
def getNext(self):
return self._next
def setItem(self,newitem):
self._item=newitem
def setNext(self,newnext):
self._next=newnext
class SingleLinkedList(): #单链表,,,,python支持在函数中间定义变量,而且还不用定义类型,随用随取,所以你看见一个变量的时候,就表明此刻就有这个变量了
def __init__(self):
self._head=None #初始化为空链表,空链表实际上不包含node,,只有一个空的头 头里面有head指针(用于指向第一个node),有current指针(用于遍历node)???
def isEmpty(self): #检测链表是否为空,
return self._head==None
def size(self):
current=self._head
count=0
while current!=None:
count+=1
current=current.getNext()
return count
def travel(self): #输出每个node的值
current=self._head
while current!=None:
print current.getItem()
current=current.getNext()
def add(self,item): #在链表端段添加元素
temp=Node(item) #创建一个node,
temp.setNext(self._head) #新node的指针,self不再是node而是SingleLinkedList,,
self._head=temp #岂不是自己指向自己????搞不懂self???,,这里的self不再是node了,而是SingleLinkedList,,,即单链表是另一个类了,,,这里是单链表类是由node类组成的
def append(self,item): #在链表尾部添加元素
temp=Node(item) #生成一个node(链条上的一个链子)
if self.isEmpty():
self._head=temp #若为空表,将添加的元素设为第一个元素,,,,self的head指针指向了新建的元素temp
else:
current=self._head #current是什么东西???
while current.getNext()!=None:
current=current.getNext() #遍历链表
current.setNext(temp) #此时current为链表最后的元素
def search(self,item): #检索元素是否在链表中
current=self._head
founditem=False
while current!=None and not founditem: #如果我想在a>0或者b>0且a,b不同时大于0的情况下返回True:(a>0 or b>0) and not (a>0 and b>0)
if current.getItem()==item:
founditem=True
else:
current=current.getNext()
return founditem
def index(self,item): #索引元素在链表中的位置
current=self._head
count=0
found=None
while current!=None and not found: #not优先级大于and大于or and两个都为真才是真
count+=1
if current.getItem()==item:
found=True
else:
current=current.getNext()
if found:
return count
else:
raise ValueError,'%s is not in linkedlist'%item
def remove(self,item): #删除链表中的某项元素
current=self._head
pre=None
while current!=None:
if current.getItem()==item:
if not pre:
self._head=current.getNext()
else:
pre.setNext(current.getNext())
break
else:
pre=current
current=current.getNext()
def insert(self,pos,item): #链表中插入元素
if pos<=1:
self.add(item)
elif pos>self.size():
self.append(item)
else:
temp=Node(item)
count=1
pre=None
current=self._head
while count<pos:
count+=1
pre=current
current=current.getNext()
pre.setNext(temp)
temp.setNext(current)
if __name__=='__main__':
a=SingleLinkedList() #建立类的对象,,,实际上就是用 SingleLinkedList类扣出了一个蛋糕a,,,就是建链表头的过程
for i in range(1,10):
a.append(i)
print a.size()
a.travel()
print a.search(6)
print a.index(5)
a.remove(4)
a.travel()
a.insert(4,100)
a.travel()
///////////////////////////////////////////////////////////////////////////
运行结果
:
runfile('E:/pythonprogram/GXTon/main.py', wdir='E:/pythonprogram/GXTon')
9
1
2
3
4
5
6
7
8
9
True
5
1
2
3
5
6
7
8
9
1
2
3
100
5
6
7
8
9
python链表的实现,有注释的更多相关文章
- Python链表的实现与使用(单向链表与双向链表)
参考[易百教程]用Python实现链表及其功能 """ python链表的基本操作:节点.链表.增删改查 """ import sys cl ...
- Python学习2——Python单行注释、整段注释使用方法
Python中的注释有多种,有单行注释,多行注释,批量注释,中文注释也是常用的. python注释也有自己的规范,在文章中会介绍到. 注释可以起到一个备注的作用,团队合作的时候,个人编写的代码经常会被 ...
- Python链表操作(实现)
Python链表操作 在Python开发的面试中,我们经常会遇到关于链表操作的问题.链表作为一个非常经典的无序列表结构,也是一个开发工程师必须掌握的数据结构之一.在本文中,我将针对链表本身的数据结构特 ...
- Python 为什么用 # 号作注释符?
关于编程语言中的注释,其重要性基本上已为大家所共识. 然而关于注释的规范,这个话题就像我们之前聊过的缩进.终止符和命名方式一样,众口难调. 注释符通常可分为两种,即行注释与块注释(inline/blo ...
- Python用户输入和代码注释
一.用户输入 若你安装的是Python3.x版本,当你在Python IDLE(编辑器) 中输入以下代码: name = input('用户名:') print('Hello',name) 保存并执行 ...
- python 链表表达式 map、filter易读版
链表推导式 [x for x in x] 链表推导式提供了一个创建链表的简单途径,无需使用 map(), filter() 以及 lambda.返回链表的定义通常要比创建这些链表更清晰.每一个链表推导 ...
- 统计python文件中的代码,注释,空白对应的行数
其实代码和空白行很好统计,难点是注释行 python中的注释分为以#开头的单行注释 或者以'''开头以'''结尾 或以"""开头以"""结尾 ...
- Python链表与反链表
# -*- coding:utf8 -*- #/usr/bin/env python class Node(object): def __init__(self, data, pnext = None ...
- Python文件中执行脚本注释和编码声明
在 Python 脚本的第一行经常见到这样的注释: #!/usr/bin/env python3 或者 #!/usr/bin/python3 含义 在脚本中, 第一行以 #! 开头的代码, 在计算机行 ...
随机推荐
- 【Pycharm】【HTML/jQuery】代码换行问题
问题:从网上下载jQuery文件后发现代码太长,不利于阅读:Pycharm没有预先设置好js文件的自动换行设置 问题 解决办法 解决后
- 今日 SGU 5.6
SGU 106 题意:问你有多少个<x,y>,满足ax+by+c=0,x1<=x<=x2,y1<=y<=y2 收货:拓展欧几里得求解的是这种方程,ax+by=1,g ...
- 【2017 Multi-University Training Contest - Team 4】Counting Divisors
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6069 [Description] 定义d(i)为数字i的因子个数; 求∑rld(ik) 其中l,r ...
- ArcGIS Engine检索要素集、要素类和要素
转自原文 ArcGIS Engine检索要素集.要素类和要素 /// <summary> /// 获取所有要素集 /// </summary> /// <param na ...
- UML中的用例图
用例图构成:參与者(actor).用例(use case).子系统(subsystem) 关联(Association) 泛化(Inheritance) 就是通常理解的继承关系,子用例和父用例类似,但 ...
- js---25桥模式
桥接模式是一种既能把两个对象连接在一起,又能避免二者间的强耦合的方法.通过“桥”把彼此联系起来,同时又允许他们各自独立变化. 主要作用表现为将抽象与其实现隔离开来,以便二者独立化. <!DOCT ...
- VBS 脚本调用
https://my.oschina.net/Tsybius2014/blog/602641
- Spring入门--控制反转(IOC)与依赖注入(DI)
1.控制反转(Inversion of Control)与依赖注入(Dependency Injection) 控制反转即IoC (Inversion of Control).它把传统上由程序 ...
- curl如何发起DELETE/PUT请求
curl如何发起DELETE/PUT请求 DELETE: curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); PUT: curl_setopt($ch ...
- cwRsync 同步时报错 STATUS_ACCESS_VIOLATION
cwRsync 同步时报错 STATUS_ACCESS_VIOLATION windows XP 执行 cwRsync 同步时报错: 2 [main] rsync 3044 _cygtls::h ...