python实现单链表的建立、访问和排序
来源:互联网 发布:网络暴力 恐怖片 编辑:程序博客网 时间:2024/05/22 00:20
第一个比较完整的python程序,排序效率不是很好,待改进!
采用类的方式实现,包含两个文件
list2:定义listNode类及createList、scanList和sortList方法
test2:对listNode类的调用
class listNode: def __init__(self,x): self.val = x self.next = None def createList(self,a): if a is None: print 'no elements' return head=listNode(a[0]) p=head i=1 n=len(a) while i<n: t=listNode(a[i]) p.next=t p=t i=i+1 return head def scanList(self,head): if head is None: print "no elements" return print head.val while head.next: p=head.next print p.val head=p def sortList(self, head): if head is None or head.next is None: return head mid = (head.val + head.next.val) / 2 if head.val > head.next.val: lhead, rhead = head.next, head else: lhead, rhead = head, head.next lit, rit = lhead, rhead it = head.next.next while it is not None: if it.val > mid: rit.next = it rit = it else: lit.next = it lit = it it = it.next lit.next, rit.next = None, None lhead = self.sortList(lhead) rhead = self.sortList(rhead) it = lhead while it.next is not None: it = it.next it.next = rhead return lhead
test2.py
import list2l=list2.listNode(0)a=[2,5,9,3,6,1,0,7,4,19]head=l.createList(a)print 'old list:'l.scanList(head)newhead=l.sortList(head)print 'sorted list:'l.scanList(newhead)
0 0
- python实现单链表的建立、访问和排序
- socket服务器和客户端的建立步骤。Python实现
- 队的建立和排序
- 建立排序的单链表
- Dictionary和IDicionary的建立和访问
- 单链表的建立,实现和操作
- python排序算法和实现
- python实现插入排序和选择排序
- python实现快速排序和归并排序
- 单链表的建立、排序、打印
- 《算法图解》的选择排序的python和java实现
- 最大堆的建立和堆排序
- 堆的建立,插入和自动排序
- kd树建立的python实现
- 经典算法: 堆排序的原理和实现, 建立堆,调整堆
- 冒泡排序(升序和降序)的python代码实现
- python实现插入排序算法的顺序和倒序
- python 实现Hadoop的partitioner和二次排序
- Search for a Range 两种解法
- Linux之删除操作
- windows下如何实现磁盘满额自动邮件报警
- 解决jquery操作checkbox火狐下第二次无法勾选问题
- 为什么计算机用补码存储数据?
- python实现单链表的建立、访问和排序
- Oracle SQL性能优化
- 书单:产品经理必读书籍推荐二
- Linux内核【链表】整理笔记(1)
- 天行健咨询简述精益生产与六西格玛管理的异同
- 工艺路线标准工时报表
- 中间人攻击原理
- Shell命令
- Centos 7 学习之添加用户