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 含义 在脚本中, 第一行以 #! 开头的代码, 在计算机行 ...
随机推荐
- Springboot2.0访问Redis集群
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作高性能的key-value数据库.缓存和消息中间件,掌握它是程序员的必备技能,下面是一个springboot访问redis的 ...
- iOS报错 -pie can only be used when targeting iOS 4.2 or later
近期,使用师兄的project时.突然报错之前没发现这个错误.信息例如以下: ld: -pie can only be used when targeting iOS 4.2 or later cla ...
- UnrealEngine4针对游戏模式的思考
游戏能够概括为三类:单进程联机(超级玛丽).小规模联机(魔兽争霸.CS),大规模联机(魔兽世界). watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmx1c ...
- Impala的安装(含使用CM安装 和 手动安装)(图文详解)
Impala有两种安装方式: 1)使用CM安装Impala 2)手动安装Impala 注意:Impala不支持在Debian/Ubuntu, SuSE, RHEL/CentOS 6.5系统中安装. 基 ...
- 逐步配置企业版Symantec Norton防病毒服务器
逐步配置企业版Symantec Norton防病毒服务器 配置企业版Symantec Norton NT操作系统,已经安装IIS 安装Symantec Norton 10 安装系统中心 650) th ...
- 基于WebSphere与Domino的电子商务网站构架分析
650) this.width=650;" border="0" alt="174812596.jpg" src="http://img1. ...
- java文件处理 之 读写TXT(比之c++,重置文件头,int转string)
一:c/c++ 处理文件的使用方法.详见博客 c++文件操作 二:java与c++的方便之处: (1) java在读取文件时.能够对字符流进行处理,又一次进行编码,如 InputStreamReade ...
- teamviewer连接不上的原因及解决方法有哪些
teamviewer连接不上的原因及解决方法有哪些 一.总结 一句话总结:这里说的就是版本问题,高版本可以连接低版本,低版本无法连接高版本. 1.TeamViewer官方检测使用环境是否为商用的标准是 ...
- Solr 读数据流程
Solr 读数据流程: 1.用户提供搜索关键词,也就是搜索语句,需要经过分词器处理以及语言处理. 2.对处理之后的关键词,搜索索引找出对应Document 即记录. 3.用户根据需要从找到的Docum ...
- select下拉列表选中后,跳转新链接
1.在当前页面打开新链接 <select onchange="location.href=this.options[this.selectedIndex].value" na ...