作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的python代码实现:

  

from Node import *

class OrderedList:
def __init__(self):
self.head = None
def prints(self):
tempNode = self.head
while tempNode is not None:
print tempNode.data
tempNode = tempNode.next
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.get_data() == item:
found = True
else:
if current.get_data() > item:
stop = True
else:
current = current.get_next()
return found def add(self,item):
current = self.head
previous = None
stop = False while current != None and not stop:
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next() temp = Node(item) if previous == None:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)
def size(self):
current = self.head
count = 0
while current != None: if previous == None:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count mylist = OrderedList()
print(mylist.add(3))
print(mylist.add(8))
print(mylist.add(53))
print(mylist.add(33))
print(mylist.search(33))
print(mylist.prints())

  Node的代码:

class Node:
def __init__(self,init_data):
self.data = init_data
self.next = None def get_data(self):
return self.data def get_next(self):
return self.next def set_data(self,new_data):
self.data = newdata def set_next(self,new_next):
self.next = new_next temp = Node(99)
print temp.get_data()

  运行结果:

99
None
None
None
None
True
3
8
33
53
None

python中实现排序list的更多相关文章

  1. python中字典排序,列表中的字典排序

    python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...

  2. python中字典排序

    一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...

  3. Python中的排序方法sort(),sorted(),argsort()等

    python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...

  4. Python中经典排序方法

    数据的排序是在解决实际问题时经常用到的步骤,也是数据结构的考点之一,下面介绍10种经典的排序方法. 首先,排序方法可以大体分为插入排序.选择排序.交换排序.归并排序和桶排序四大类,其中,插入排序又分为 ...

  5. python中的排序

    今天在http://www.pythontip.com刷题的时候遇到一个排序的问题:一个列表中既有字符串,又有数字,该怎么排序. list = [1,2,5,4,'d','s','e',45] lis ...

  6. python中自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  7. Python中的排序方法

    1 list.sort list.sort(key=None, reverse=False) 该方法只能用于list.就地排序,原来的list被修改.key的用法见下文.reverse控制降序还是生序 ...

  8. python中列表排序,字典排序,列表中的字典排序

    #-*- encoding=utf-8 -*- # python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1 ...

  9. python中的排序函数

    1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1)  cmp参数 cmp接受一 ...

随机推荐

  1. python3安装xadmin出现 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa4 in position 3444: illegal multibyte sequence

    python3的环境安装xadmin时,直接pip install xadmin出现 Downloading xadmin-0.6.1.tar.gz (1.0MB) 100% |███████████ ...

  2. 导出word文档 通过DocX组件

    根据DocX官方描述如下: In the application development process, it uses COM libraries and requires MS Word or ...

  3. iOS之Safari调试webView/H5页面

    之前做过混合开发,用的是JavaScriptCore+OC+UIWebView. Safari调试功能真的很有用,通过它可以轻松定位问题的所在,下面说说怎么调试. 开启Safari开发菜单 在Mac的 ...

  4. 515. Find Largest Value in Each Tree Row查找一行中的最大值

    [抄题]: You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...

  5. Linux 里的 2>&1含义

    我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下: 首先就是一个nohup:表示当前用户 ...

  6. face_recognition 相关依赖

    centos-v:7 python-v:3.7 IDE:pycharm 安装顺序: boost boost-py cmake numpy opencv-python scipy 安装方法:settin ...

  7. 分布式协议学习笔记(三) Raft 选举自编写代码练习

    由于时间安排上的原因,这次的代码写的稍微有些简略,只能算是自己对RAFT协议的一个巩固. 实现定义2个节点,使用读取配置文件来获取IP和端口以及节点ID 网络使用boost同步流程 一个线程收 一个线 ...

  8. django by example 第四章 扩展 User 模型( model)

    描述: RelatedObjectDoesNotExist at /account/edit/ User has no profile.​ 原因: 注意原书,要求新建一个账户才能使用.

  9. CSS3美化网页!!

    一.span标签:能让某几个文字或者某个词语凸显出来        <p>           今天是11月份的<span>第一天</span>,地铁卡不打折了  ...

  10. quick-cocos2d-x lua框架解析(一)对UI进行操作的UiUtil脚本

    最近一段时间接手了一个cocos游戏项目,由于我是U3D开发入门,所以花了一段时间来钻研cocos2d的使用与项目架构.与U3D相比,cocos2d的开发界面实在做的不咋地.不过在看过源码之后,源码跑 ...