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接受一 ...
随机推荐
- thinkphp5中使用excel导出数据表格(包涵图片)
首先使用composer require phpoffice/phpexcel下载安装phpexcel包. 将包放入extend下面. 不附加图片的导出 /** * 导出excel(不带图片) * @ ...
- GitHub下载子目录
背景 整个Github目录太大,国内网速不好,且其他部分也不需要. 方法 把 /tree/master 改成 trunk. svn checkout https://github.com/lodash ...
- 加NONCLUSTERED INDEX索引,在ON了之后还要INCLUDE
之前加了索引,但效果不大 SET STATISTICS TIME ON --执行时间 SET STATISTICS IO ON --IO读取 DBCC DROPCLEANBUFFERS --清除缓冲区 ...
- [leetcode]42. Trapping Rain Water雨水积水问题
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 30.Mysql常见问题和应用技巧
30.Mysql常见问题和应用技巧30.1 忘记Mysql的root密码30.2 如何处理MyISAM存储引擎的表损坏 30.2.1 方法一:使用myisamchk工具 30.2.2 方法二:使用SQ ...
- GUI设计和UI设计有什么区别?
首先从技术的角度分析两者处于包含与被包含的关系. GUI=Graphical User Interface,是指在计算机出现后,在屏幕上使用图形界面来帮助(User)与机器打交道用的界面接口,泛指在计 ...
- Java多线程系列2 线程常见方法介绍
守护线程 执行一些非业务方法,比如gc.当全部都是守护线程的时候,jvm退出 线程优先级 设置线程优先级:setPriority(int priorityLevel).参数priorityLevel ...
- solr7.7.0搜索引擎使用(四)(搜索语法)
solr搜索语法 参数defType 指定用于处理查询语句(参数q的内容)的查询解析器,eg:defType=lucenesort 指定响应的排序方式:升序asc或降序desc.同时需要指定 ...
- JavaScript 高阶函数
高阶函数的英文叫Higher-order function ,什么是高阶函数呢>? JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接 ...
- Java:Hashtable
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...