python中实现排序list
作为一个非常实用的一种数据结构,排序链表用在很多方面,下面是它的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的更多相关文章
- python中字典排序,列表中的字典排序
python中字典排序,列表中的字典排序 一.使用python模块:operator import operator #首先要导入模块operator x = {1:2, 3:4, 4:3, 2:1, ...
- python中字典排序
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- Python中的排序方法sort(),sorted(),argsort()等
python 列表排序方法sort.sorted技巧篇 转自https://www.cnblogs.com/whaben/p/6495702.html,学习参考. Python list内置sort( ...
- Python中经典排序方法
数据的排序是在解决实际问题时经常用到的步骤,也是数据结构的考点之一,下面介绍10种经典的排序方法. 首先,排序方法可以大体分为插入排序.选择排序.交换排序.归并排序和桶排序四大类,其中,插入排序又分为 ...
- python中的排序
今天在http://www.pythontip.com刷题的时候遇到一个排序的问题:一个列表中既有字符串,又有数字,该怎么排序. list = [1,2,5,4,'d','s','e',45] lis ...
- python中自定义排序函数
Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...
- Python中的排序方法
1 list.sort list.sort(key=None, reverse=False) 该方法只能用于list.就地排序,原来的list被修改.key的用法见下文.reverse控制降序还是生序 ...
- python中列表排序,字典排序,列表中的字典排序
#-*- encoding=utf-8 -*- # python3代码 import operator 一. 按字典值排序(默认为升序) x = {1:2, 3:4, 4:3, 2:1, 0:0} 1 ...
- python中的排序函数
1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1) cmp参数 cmp接受一 ...
随机推荐
- hdu 1277 AC自动机入门(指针版和数组版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...
- jdbcTemplate的简单介绍
Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...
- 微信公众号的分享接口,分享提示config:fail,invalid signature的解决办法(2017年12月)
微信中打开网页,使用微信右上角菜单中自带的分享功能的经历及总结: 最开始,微信分享页面时,直接读取页面的标题(title)和页面中的第一张符合条件的图片[此种方式在2017-03-29之前管用,这一天 ...
- CentOS7中firewall防火墙详解和配置,.xml服务配置详解
修改防火墙配置文件之前,需要对之前防火墙做好备份 重启防火墙后,需要确认防火墙状态和防火墙规则是否加载,若重启失败或规则加载失败,则所有请求都会被防火墙 1. firewall-cmd --state ...
- 探索未知种族之osg类生物---呼吸分解之更新循环三
补充 当然细心的你会发现,_scene->updateSceneGraph(*_updateVisitor)中还有一个imagePager::UpdateSceneGraph()还没有进行讲解, ...
- mybatis电子商务平台b2b2c
技术解决方案 开发语言: java.j2ee 数据库:mysql JDK支持版本: JDK1.6.JDK1.7.JDK1.8版本 核心技术:分布式.云服务.微服务.服务编排等. 核心架构: 使用Spr ...
- 模板学习实践二 pointer
c++ template学习记录 使用模板将实际类型的指针进行封装 当变量退出作用域 自动delete // 1111.cpp : 定义控制台应用程序的入口点. // #include "s ...
- docker安装radis
sudo docker search redis sudo docker pull redis sudo docker run --name redis6379 -p 6379:6379 -v /op ...
- 2019.02.21 bzoj2829: 信用卡凸包(凸包)
传送门 题意:给nnn个A∗BA*BA∗B的矩形,其中每个矩形的四个角被改造成了半径为rrr的四分之一 圆,问这些矩形的凸包周长. 思路:考虑求出圆心的凸包周长然后加上一个整圆的周长,证明很简单,略掉 ...
- Linux 第十三天
十五.shell编程 1.Shell是什么 1)Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一 ...