作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的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. Spring再接触 注入类型

    共有三种注入类型 一种是set注入 一种是构造注入 一种是接口注入 最常用的还是set 现在看一下construct 构造注入 在userservice中加入 package com.bjsxt.se ...

  2. MongoError: no primary found in replicaset

    nodejs连接mongodb时,使用集群方式报错 2017-09-22T01:42:32.115Z - error: db connect failed 2017-09-22T01:42:32.12 ...

  3. VueJs相关学习网址

      麦子学院 http://www.maiziedu.com/course/916/   慕课网-vue.js入门基础 https://www.imooc.com/learn/694   查阅的网址 ...

  4. discuz代码转为html代码

    下面附件是来自discuz的一个函数文件(原来是在source/function/function_discuzcode.php位置),已稍微修改: https://files.cnblogs.com ...

  5. Jstack定位CPU使用最多的线程及代码

    jstack可以定位到线程堆栈,根据堆栈信息我们可以定位到具体代码,所以它在JVM性能调优中使用得非常多.下面我们来一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息,用到的命令有 ...

  6. Head First Servlets & JSP 学习笔记 第十三章 —— 过滤器的威力

    过滤器可能是最强大的Web应用开发工具了! 与Servlet非常类似,过滤器就是Java组件,请求发送到Servlet之前,可以用过滤器截获和处理请求:另外Servlet结束工作之后,但在响应发回给客 ...

  7. 201621123002《JAVA程序设计》第四周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承 多态 覆盖 抽象 重载 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 1.3 可选: ...

  8. Java之IO流总结

    IO流·Java流式输入/输出原理·Java流类的分类·输入/输出流类·常见的节点流和处理流·文件流·缓冲流·转换流·数据流·Print流·Object流 ①Java流式输入/输出原理         ...

  9. [杂谈]杂谈章3 JAVA中如何用自动注入

    PART1 加配置文件 创建自动加载bean的配置文件 <beans xmlns="http://www.springframework.org/schema/beans" ...

  10. 可以用到的XSS跨站语句

    我们常用的测试XSS跨站的语句一般是alert比如: <script>alert(“sex”)</script> <script>alert(/sex/)</ ...