Python链表与反链表
# -*- coding:utf8 -*-
#/usr/bin/env python class Node(object):
def __init__(self, data, pnext = None):
self.data = data
self._next = pnext
print('self._next',self._next) def __repr__(self):
return str(self.data) class ChainTable(object):
def __init__(self):
self.head = None
self.length = 0 def isEmpty(self):
return (self.length == 0) def append(self, dataOrNode):
item = None
print('Node',Node)
if isinstance(dataOrNode, Node):
item = dataOrNode
print(123)
else:
item = Node(dataOrNode)
print(item,456)
if not self.head:
self.head = item
self.length += 1
print('self.head',self.head._next)
else:
node = self.head
print('1111node',node)
print('111node._next',node._next)
while node._next:
print(222222222222222222222222,node._next)
node = node._next
print(444444444444444,node)
node._next = item
print(11111111111111111111111,node._next)
self.length += 1 def reverseChainTable(self,chaintable):
if isinstance(chaintable, Node):
item = chaintable
print(123)
else:
print('这不是链表')
return def delete(self, index):
if self.isEmpty():
print ("this chain table is empty.")
return if index < 0 or index >= self.length:
print ('error: out of index')
return if index == 0:
self.head = self.head._next
self.length -= 1
return j = 0
node = self.head
prev = self.head
while node._next and j < index:
prev = node
node = node._next
j += 1 if j == index:
prev._next = node._next
self.length -= 1 def insert(self, index, dataOrNode):
if self.isEmpty():
print ("this chain tabale is empty")
return if index < 0 or index >= self.length:
print ("error: out of index")
return item = None
if isinstance(dataOrNode, Node):
item = dataOrNode
else:
item = Node(dataOrNode) if index == 0:
item._next = self.head
self.head = item
self.length += 1
return j = 0
node = self.head
prev = self.head
while node._next and j < index:
prev = node
node = node._next
j += 1 if j == index:
item._next = node
prev._next = item
self.length += 1 def update(self, index, data):
if self.isEmpty() or index < 0 or index >= self.length:
print ('error: out of index')
return
j = 0
node = self.head
while node._next and j < index:
node = node._next
j += 1 if j == index:
node.data = data def getItem(self, index):
if self.isEmpty() or index < 0 or index >= self.length:
print ("error: out of index")
return
j = 0
node = self.head
while node._next and j < index:
node = node._next
j += 1 return node.data def getIndex(self, data):
j = 0
if self.isEmpty():
print ("this chain table is empty")
return
node = self.head
while node:
if node.data == data:
return j
node = node._next
j += 1 if j == self.length:
print ("%s not found" % str(data))
return def clear(self):
self.head = None
self.length = 0 def __repr__(self):
if self.isEmpty():
return "empty chain table"
node = self.head
nlist = ''
while node:
nlist += str(node.data) + ' '
node = node._next
return nlist def __getitem__(self, ind):
if self.isEmpty() or ind < 0 or ind >= self.length:
print ("error: out of index")
return
return self.getItem(ind) def __setitem__(self, ind, val):
if self.isEmpty() or ind < 0 or ind >= self.length:
print ("error: out of index")
return
self.update(ind, val) def __len__(self):
return self.length chariin = ChainTable()
for i in range(10):
chariin.append(i) print(chariin)
从哪里找的忘了,不过思路已经整的很明白了
下面是链表翻转的:
来源 https://blog.csdn.net/u011608357/article/details/36933337
单链表的反转可以使用循环,也可以使用递归的方式
1.循环反转单链表
循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可。
class ListNode:
def __init__(self,x):
self.val=x;
self.next=None; def nonrecurse(head): #循环的方法反转链表
if head is None or head.next is None:
return head;
pre=None;
cur=head;
h=head;
while cur:
h=cur;
tmp=cur.next;
cur.next=pre;
pre=cur;
cur=tmp;
return h; head=ListNode(1); #测试代码
p1=ListNode(2); #建立链表1->2->3->4->None;
p2=ListNode(3);
p3=ListNode(4);
head.next=p1;
p1.next=p2;
p2.next=p3;
p=nonrecurse(head); #输出链表 4->3->2->1->None
while p:
print p.val;
p=p.next;
反转代码:
class ListNode:
def __init__(self,x):
self.val=x;
self.next=None; def recurse(head,newhead): #递归,head为原链表的头结点,newhead为反转后链表的头结点
if head is None:
return ;
if head.next is None:
newhead=head;
else :
newhead=recurse(head.next,newhead);
head.next.next=head;
head.next=None;
return newhead; head=ListNode(1); #测试代码
p1=ListNode(2); # 建立链表1->2->3->4->None
p2=ListNode(3);
p3=ListNode(4);
head.next=p1;
p1.next=p2;
p2.next=p3;
newhead=None;
p=recurse(head,newhead); #输出链表4->3->2->1->None
while p:
print p.val;
p=p.next;
Python链表与反链表的更多相关文章
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- python数据结构链表之单向链表
单向链表 单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域.这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值. 表元素域ele ...
- Python数据结构应用3——链表
linked list(链表) 建立 Node 链表的基本组成就是一个个Node,每个Node都需要包括两部分内容,一部分是自身的data,另一部分是下一个Node的reference. class ...
- python实现数据结构单链表
#python实现数据结构单链表 # -*- coding: utf-8 -*- class Node(object): """节点""" ...
- Python线性表——单链表
1. 线性表简介 线性表是一种线性结构,它是由零个或多个数据元素构成的有限序列.线性表的特征是在一个序列中,除了头尾元素,每个元素都有且只有一个直接前驱,有且只有一个直接后继,而序列头元素没有直接前驱 ...
- Python数据结构之单链表
Python数据结构之单链表 单链表有后继结点,无前继结点. 以下实现: 创建单链表 打印单链表 获取单链表的长度 判断单链表是否为空 在单链表后插入数据 获取单链表指定位置的数据 获取单链表指定元素 ...
- python 数据结构中的链表操作
链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址信息,所以用一个变量就 ...
- Python与数据结构[0] -> 链表/LinkedList[0] -> 单链表与带表头单链表的 Python 实现
单链表 / Linked List 目录 单链表 带表头单链表 链表是一种基本的线性数据结构,在C语言中,这种数据结构通过指针实现,由于存储空间不要求连续性,因此插入和删除操作将变得十分快速.下面将利 ...
- Python与数据结构[0] -> 链表/LinkedList[1] -> 双链表与循环双链表的 Python 实现
双链表 / Doubly Linked List 目录 双链表 循环双链表 1 双链表 双链表和单链表的不同之处在于,双链表需要多增加一个域(C语言),即在Python中需要多增加一个属性,用于存储指 ...
随机推荐
- node scripts/install.js 停顿解决办法
参考:node-sass 安装卡在 node scripts/install.js 解决办法 在安装hexo的时候,运行: npm install hexo-cli -g 卡死在了 node scri ...
- 常用 Math 属性及方法
Math 对象 Math.PI π 3.141592653589793 Math.ceil('2.5') Math.ceil(2.1) 向上取整 3 Math.floor ...
- android:点击popupwindow以外区域 popupwindow自动消失
方法一(这种方法可以处理popupwindows dimiss的时候一些其他的操作,比如让其他控件的隐藏,消失等): 代码如下popupWindow.setFocusable(false);//foc ...
- Java IO流-合并流
2017-11-05 20:15:28 SequenceinputStream SequenceinputStream:SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有 ...
- C# DataTable Compute方法的使用
在开发中需要对DataTable的数据进行处理,比如累加,求最大最小及平均值等,以前都采用手工对DataTable进行循环并计算的方式,现在发现DataTable的Compute方法可以轻松实现这些功 ...
- java读取和写入浏览器Cookies
首先我们认识下什么是cookies: cookie实际上是一个存在你硬盘里的数据,但是这些数据很特殊,只能由web应用提交给浏览器帮助存储,并且我们还能读取浏览器存在本地的cookie web应用一般 ...
- 143. Long Live the Queen 树形dp 难度:0
143. Long Live the Queen time limit per test: 0.25 sec. memory limit per test: 4096 KB The Queen of ...
- linux下部署tomcat服务器之安装tomcat
下载tomcat压缩包 apache-tomcat-7.0.82.tar.gz 在把包放到linux 的softwore文件夹下 自己选择文件夹 tar -zxvf apache-tomcat-7. ...
- 【python】venv使用
virtualenvwrapper 比 virualenv 好用一些. 准备 export WORKON_HOME=~/venv source /usr/bin/virtualenvwrapper.s ...
- git 常用命令--抓取分支-为自己记录(二)
二:抓取分支: 多人协作时,大家都会往master分支上推送各自的修改.现在我们可以模拟另外一个同事,可以在另一台电脑上(注意要把SSH key添加到github上)或者同一台电脑上另外一个目录克隆, ...