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中需要多增加一个属性,用于存储指 ...
随机推荐
- 分布式缓存--系列1 -- Hash环/一致性Hash原理
当前,Memcached.Redis这类分布式kv缓存已经非常普遍.从本篇开始,本系列将分析分布式缓存相关的原理.使用策略和最佳实践. 我们知道Memcached的分布式其实是一种“伪分布式”,也就是 ...
- UVa 11889 最小公倍数
https://vjudge.net/problem/UVA-11889 题意: 输入两个整数A和C,求最小的整数B使得lcm(A,B)=C. 思路: 首先C是A的公倍数,如果C%A不为0肯定是无解的 ...
- Android 引用资源
比如在 strings.xml 中找到的 Hello world!字符串,我们有两种方式可以引用它: 1. 在代码中通过 R.string.hello_world 可以获得该字符串的引用: 2. 在 ...
- UVA-11903 Just Finish it up
题目大意:一个环形跑道上有n个加油站,每个加油站可加a[i]加仑油,走到下一站需要w[i]加仑油,初始油箱为空,问能否绕跑道一圈,起点任选,若有多个起点,找出编号最小的. 题目分析:如果从1号加油站开 ...
- 基于Oracle的SQL优化(崔华著)-整理笔记-第2章“Oracle里的执行计划”
详细介绍了Oracle数据里与执行计划有关的各个方面的内容,包括执行计划的含义,加何查看执行计划,如何得到目标SQL真实的执行计划,如何查看执行计划的执行顺序,Oracle数据库里各种常见的执行计划的 ...
- web 移动端事件总结
1.https://www.jianshu.com/p/6f85e957a725 (web 移动端事件总结)
- OATable中column栏位数据居中的实现方法及代码
// Table column中显示居中的实现 // QpriceResultTable1 为table的名称 // noPrice 为table中的列 OATableBean table = (OA ...
- jspf、jsp小记
jsp页面:
- 从0开始springboot
http://412887952-qq-com.iteye.com/blog/2291500
- Ubuntu下XTerm乱码问题的解决及XTerm的简单配置
本人比较喜欢Ubuntu这个Linux的发行版,主要是安装程序插件什么的都比较方便,推荐新手使用,可以免去很多麻烦的配置,将注意力放在编程的学习上,当然如果是想专门学Linux的,还是推荐在Cento ...