python---用链表结构实现有序和无序列表的几个功能
只是看看套路,没有深入练习。
如果真要自己写,可以基于此类。
但其实,在普通使用中,这样实现的性能,并没有python原生的列表性能好。
因为python原生列表的功能,是基于数组作扩展实现的。
# coding: utf-8
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 = new_data
def set_next(self, new_next):
self.next = new_next
class UnorderedList:
def __init__(self):
self.head = None
def add(self, item):
temp = Node(item)
temp.set_next(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current is not None:
count += 1
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
while current is not None and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous is None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
def show_data(self):
current = self.head
while current is not None:
print(current.get_data())
current = current.get_next()
print('============UnorderedList==================')
my_un_order_list = UnorderedList()
my_un_order_list.add(31)
my_un_order_list.add(77)
my_un_order_list.add(17)
my_un_order_list.add(93)
my_un_order_list.add(26)
my_un_order_list.add(54)
print(my_un_order_list.size())
print(my_un_order_list.search(17))
my_un_order_list.remove(26)
print(my_un_order_list.size())
my_un_order_list.show_data()
class OrderedList:
def __init__(self):
self.head = None
def add(self, item):
current = self.head
previous = None
stop = False
while current is not None and not stop:
if current.get_data() > item:
stop = True
else:
previous = current
current = current.get_next()
temp = Node(item)
if previous is 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 is not None:
count += 1
current = current.get_next()
return count
def search(self, item):
current = self.head
found = False
stop = False
while current is not 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 remove(self, item):
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous is None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
def show_data(self):
current = self.head
while current is not None:
print(current.get_data())
current = current.get_next()
print('============OrderedList==================')
my_order_list = OrderedList()
my_order_list.add(31)
my_order_list.add(77)
my_order_list.add(17)
my_order_list.add(93)
my_order_list.add(26)
my_order_list.add(54)
print(my_order_list.size())
print(my_order_list.search(17))
my_order_list.remove(26)
print(my_order_list.size())
my_order_list.show_data()
C:\Users\Sahara\.virtualenvs\untitled\Scripts\python.exe D:/test/python_list.py ============UnorderedList================== 6 True 5 54 93 17 77 31 ============OrderedList================== 6 True 5 17 31 54 77 93 Process finished with exit code 0
python---用链表结构实现有序和无序列表的几个功能的更多相关文章
- Java设置PDF有序、无序列表
文档中的设置有序或无序列表是一种反应内容上下级关系或者内容相同属性的方式,与单纯的文字叙述相比,它能有效增强文档内容的条理性,突出重点.因此,本文将分享通过Java编程在PDF文档中设置有序或无序列表 ...
- CSS 有序或者无序列表的前面的标记 list-style-type 属性
例子: <html> <head> <style type="text/css"> ul.none{list-style-type:none} ...
- python实现无序列表:链表
介绍链表前我们先了解下什么是列表. 在对基本数据结构的讨论中,我们使用 Python 列表来实现所呈现的抽象数据类型.列表是一个强大但简单的收集机制,为程序员提供了各种各样的操作.然而,不是所有的编程 ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
- Python中将字典转换为有序列表、无序列表的方法
说明:列表不可以转换为字典 1.转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) ...
- python中的可变和不可变对象 有序和无序对象
可变对象和不可变对象的定义:对象存放在地址的值是否可以被改变 不可变对象包括:整形int.浮点型float .字符串str .元祖tuple.布尔型boole 可变对象包括 :列表list.集合set ...
- MySQL 索引结构 hash 有序数组
MySQL 索引结构 hash 有序数组 除了最常见的树形索引结构,Hash索引也有它的独到之处. Hash算法 Hash本身是一种函数,又被称为散列函数. 它的思路很简单:将key放在数组里,用 ...
- 今天学习了无序列表和有序列表和使用HTML5创建表格
ol建立有序列表,该列表可以用设置type="A/a" 其语法架构为 <ol> <li></li> <li></li> ...
- Python 数据结构 链表
什么是时间复杂度 时间频度:一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才知道.但是我们不可能也没有必要对每一个算法都进行上机测试,只需要知道那个算法花费的时间多,那个算法花费得 ...
随机推荐
- HardNet解读
论文:Working hard to know your neighbor’s margins: Local descriptor learning loss 为什么介绍此文:这篇2018cvpr文 ...
- 写markdown博客如何将截图快速上传到图床——记一个工具插件的实现(windows版 开源)
打造一个上传图片到图床利器的插件(Mac版 开源)(2018-06-24 19:44) 更新于2018年2月 做了以下改动: 1.修复了一个bug,把服务器区域做成可配: 七牛有华北,华东,华南以及美 ...
- QPainter
1.QPainter使用save()函数来保存QPainter的状态,并且用restore()函数来使他们回退. void MainWindow::paintEvent(QPaintEvent *ev ...
- $Django Rest Framework-频率组件,解析器
1 频率组件 #自定义组件写频率认证(重点继承BaseThrottle) from rest_framework.throttling import BaseThrottle import time ...
- 如何去掉li标签的重叠边框
当我们的li标签设置了border的时候就会出现重叠边框,如何去掉呢,见代码 html代码 <ul class="friendLink_list"> <li> ...
- jquery中的attr与prop的区别,什么时候用attr,什么时候用prop
只要有 Boolean() 属性的,简单说就是具有true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),(其实这些都是表单类的), ...
- 洛谷P3246 [HNOI2016]序列 [莫队]
传送门 思路 看到可离线.无修改.区间询问,相信一定可以想到莫队. 然而,莫队怎么转移是个大问题. 考虑\([l,r]\rightarrow[l,r+1]\)时答案会怎样变化?(左端点变化时同理) \ ...
- ios中input获取焦点时的问题
1.获取焦点时,input会变大 解决办法是:font-size设置为32px以上 还有就是要在header里面加这一行代码:<meta name="viewport" co ...
- js的页面传值cookie.session
jquery的cookie: 读取所有的cookie: document.cookie: 读取单个cookie:$.cookie('cookiename'); 新增cookie: $.cookie(' ...
- 在XIB 或者 SB 上面 实现 半透明的背景上添加UILabel是文字不透明
设透明背景的时候直接设置RGB值 然后设置Opacity即可