只是看看套路,没有深入练习。

如果真要自己写,可以基于此类。

但其实,在普通使用中,这样实现的性能,并没有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---用链表结构实现有序和无序列表的几个功能的更多相关文章

  1. Java设置PDF有序、无序列表

    文档中的设置有序或无序列表是一种反应内容上下级关系或者内容相同属性的方式,与单纯的文字叙述相比,它能有效增强文档内容的条理性,突出重点.因此,本文将分享通过Java编程在PDF文档中设置有序或无序列表 ...

  2. CSS 有序或者无序列表的前面的标记 list-style-type 属性

    例子: <html> <head> <style type="text/css"> ul.none{list-style-type:none} ...

  3. python实现无序列表:链表

    介绍链表前我们先了解下什么是列表. 在对基本数据结构的讨论中,我们使用 Python 列表来实现所呈现的抽象数据类型.列表是一个强大但简单的收集机制,为程序员提供了各种各样的操作.然而,不是所有的编程 ...

  4. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

  5. Python中将字典转换为有序列表、无序列表的方法

    说明:列表不可以转换为字典 1.转换后的列表为无序列表 a = {'a' : 1, 'b': 2, 'c' : 3} #字典中的key转换为列表 key_value = list(a.keys()) ...

  6. python中的可变和不可变对象 有序和无序对象

    可变对象和不可变对象的定义:对象存放在地址的值是否可以被改变 不可变对象包括:整形int.浮点型float .字符串str .元祖tuple.布尔型boole 可变对象包括 :列表list.集合set ...

  7. MySQL 索引结构 hash 有序数组

    MySQL 索引结构 hash 有序数组 除了最常见的树形索引结构,Hash索引也有它的独到之处.   Hash算法 Hash本身是一种函数,又被称为散列函数. 它的思路很简单:将key放在数组里,用 ...

  8. 今天学习了无序列表和有序列表和使用HTML5创建表格

    ol建立有序列表,该列表可以用设置type="A/a" 其语法架构为 <ol> <li></li> <li></li> ...

  9. Python 数据结构 链表

    什么是时间复杂度 时间频度:一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才知道.但是我们不可能也没有必要对每一个算法都进行上机测试,只需要知道那个算法花费的时间多,那个算法花费得 ...

随机推荐

  1. requests库入门01-环境安装

    最近在项目中写了一个接口自动化测试的脚本,想要写一些文章来一下,方便自己回头来温习,感兴趣的可以跟着看,先写关于requests库的一些基本操作,然后再写整个框架的搭建.使用的是Python3+req ...

  2. cocos开发学习记录

    场景的创建和切换 https://blog.csdn.net/lin453701006/article/details/56334578

  3. python用类装饰函数的一个有趣实现

    class RunningLog: def __init__(self,func): self._func=func self._func_name = func.__name__ def __cal ...

  4. linux中模糊查找文件

    1.在当前目录下搜索指定文件: find . -name test.txt 2.在当前目录下模糊搜索文件: find . -name '*.txt' 3.在当前目录下搜索特定属性的文件: find . ...

  5. 【转】C++对成员访问运算符->的重载

    运算符->的重载比较特别,它只能是非静态的成员函数形式,而且没有参数. 1.如果返回值是一个原始指针,那么就将运算符的右操作数当作这个原始指针所指向类型的成员进行访问: 2.如果返回值是另一个类 ...

  6. <pre>标签的基本样式设置

    断行 在html中,换行符无法在一般标签内作为布局控制显示,包括xml实体 和 均表现为white-space,仅用于断字[1]. 一般情形下,可使用<br>标签断行:但需要从原始xml文 ...

  7. zabbix3.0.4使用percona-monitoring-plugins插件来监控mysql5.6的详细实现过程

    zabbix3.0.4使用percona-monitoring-plugins插件来监控mysql5.6的详细实现过程 因为Zabbix自带的MySQL监控没有提供可以直接使用的Key,所以一般不采用 ...

  8. Select2日常操作集合

    1.获得多选值 var arraySelected = $('#carTypes').select2("data"); var carTypesDesc = ''; for (va ...

  9. mysqlfrm初步使用

    这个工具也就是读取frm文件生成建表语句默认的模式是再生个实例,使用--basedir选项或指定--server选项来连接到已经安装的实例.这种过程不会改变原始的.frm文件.该模式也需要指定--po ...

  10. 自动化工具 Sikuli-Script 使用

    Sikuli-IDE用起来方便,但是用到实际项目中还是有局限性的,Sikuli提供了Sikuli-Script的jar包,在Sikuli-X的安装目录下,这样就可以在 eclipse中使用JAVA编写 ...