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

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

但其实,在普通使用中,这样实现的性能,并没有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. Go语言程序开发初涉

    由于工作原因,现在开始学习Go语言.这也是本人第一篇关于Go的博客.本文将通过一些基本概念的说明和实际案例,使得大家能快速对Go程序的开发有个了解. 一. Go的安装 :     在 https:// ...

  2. DUILIB入门简明教程

      电子书下载: DUILIB入门简明教程.chm 文章作者:  Alberl 电子书制作: 邓学彬 目录: 2013 duilib入门简明教程 -- 前言(1) 2013 duilib入门简明教程 ...

  3. linux系统的三种网络连接模式

    VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...

  4. linux学习之硬盘的存储原理和内部架构

    原文地址:https://blog.csdn.net/tanggao1314/article/details/52074735 首先,让我们看一下硬盘的发展史: 1956年9月13日,IBM的IBM ...

  5. ansible结合zabbix_api批量添加主机

    批量添加zabbix监控 .使用ansible配置zabbix客户端 ①修改服务器的IP(域名),为了方便使用ansible来批量操作 等同于如下sed语句 sed -i 's#Server=1.1. ...

  6. const成员函数和mutable关键字

    一.const成员函数 class MyClass { public: void fun() const { } private: int m_nValue; } const成员函数内不能修改成员变量 ...

  7. Light OJ 1214

    简单大数模拟题: #include<bits/stdc++.h> using namespace std; typedef long long ll; string Num; vector ...

  8. iOS Core Data 数据库的加密(待研究)

    https://github.com/project-imas/encrypted-core-data 使用起来很方便,底层还是使用了SQLCipher,有时间要研究一下! 数据库的密码不能用固定字符 ...

  9. mtu简单说明

    总结:本地的mtu值==网络设备的mtu值是最优的,一般本地和网络设备的默认值都是1500(字节),没什么特殊需求,尽量不要修改 一.什么是 MTU 值   1 从字面上来说,MTU 是英文 Maxi ...

  10. Confluence 6 SQL Server 创建一个数据库和数据库用户

    一旦你成功安装了 SQL Server 服务器,请按照下面的方法为你的 Confluence 创建数据库用户和数据库: 使用你的 SQL 管理员权限,创建一个新的数据库(例如 confluence). ...