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

  

原文地址:https://www.cnblogs.com/aguncn/p/10660188.html

时间: 2024-08-12 11:06:45

python---用链表结构实现有序和无序列表的几个功能的相关文章

自学html-four(css初始化及html语义标签 -> h标签 p标签 img标签 有序列表 无序列表 表格 超链接)

一.css初始化 现在我们来做一个简单的测试,测试步骤如下: 1.编写代码如下: 2.把改程序用不同的浏览器打开我们会发现同一份代码在不同的浏览器中的显示会有略微的差别: 360浏览器下显示效果图: 火狐浏览器下显示的效果图: 同一份代码在不同浏览器显示的效果存在差异的原因是:各浏览器对各元素的margin,border,font-size等的初始设置略有不同 解决方法:通过css强制让所有元素的属性值都一样 这里提供一段雅虎工程师css初始化代码,直接拷贝到css位置就可以了,body,div

浏览器兼容性之无序列表dl

浏览器兼容性之无序列表dl 无序列表的一些特定的css属性有list-style-type,list-style-position,和list-style-image.这些属性设置了列表项目符号的类型,标记的位置,以及使用图片代替标记.这三个属性可以使用list-style进行合并缩写. 标记list-style-type,在不同的浏览器中大小不和位置的渲染也是有差异的,故很少使用.list-style-image属性可以给无序列表一个自定义的独一无二的表现,不幸的是,在IE下使用此方法添加项目

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

ol建立有序列表,该列表可以用设置type="A/a" 其语法架构为 <ol> <li></li> <li></li> </ol> Ul建立无序列表, 该列表可以用设置type=" disc/circle/square" 其语法架构为 <ul> <li></li> <li></li> </ul> 创建表格其基本属性有 Cel

python内置数据结构之list

列表简单总结 类和实例之间的理解         人类,哺乳类         实例是某一类中的真实个体 时间复杂度         O(1)         O(n)         O(n**2)         随着处理的数据逐渐增加,程序返回结果的时间变化的描述.             O(1)表示无论程序处理的数据如何增加,返回结果的时间是常数             O(n)随着处理数据的数量n的增加,返回结果的时间线性增长n             O(n)随着处理数据的数量n的

用最简单的方式学Python单链表

Python 实现单链表 在本博客中,我们介绍单链表这种数据结构,链表结构为基于数组的序列提供了另一种选择(例如Python列表). 基于数组的序列和链表都能够对其中的元素保持一定得顺序,但采用的方式截然不同 什么是单链表 单链表 最简单的形式就是由多个节点的集合共同构成一个线性序列.每个节点存储一个对象的引用,这个引用指向序列中的一个元素,即存储指向列表的下一个节点. 其实,上面的术语用生活中的大白话来解释,就是我们现在有三个人--我.你.他.当我用手指指向你,你用手指指向他,这样就形成了一个

【Python】06、python内置数据结构1

一.数据结构与获取帮助信息 1.数据结构 通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其它的数据结构. python的最基本数据结构是序列 序列中的每个元素被分配一个序号(即元素的位置),也称为索引:索引从0开始编号 2.python中如何获取命令帮助 获取对象支持使用的属性和方法:dir(), 某方法的具体使用帮助:help() 获取可调用对象的文档字串:print(obj.__doc__) In [15]: dir(list) Ou

前端学习 -- Css -- 有序列表和无序列表

列表就相当于去超市购物时的那个购物清单, 在HTML也可以创建列表,在网页中一共有三种列表: 1.无序列表 2.有序列表 3.定义列表 无序列表 - 使用ul标签来创建一个无序列表 - 使用li在ul中创建一个一个的列表项, 一个li就是一个列表项 通过type属性可以修改无序列表的项目符号 可选值: disc,默认值,实心的圆点 square,实心的方块 circle,空心的圆 注意:默认的项目符号我们一般都不使用!! 如果需要设置项目符号,则可以采用为li设置背景图片的方式来设置 ul和li

Pjlib中的链表结构

Pjlib的链表结构跟常见的链表结构有所区别,如下图所示: ? ? ? ? ? ? 图1:一般链表结构 ? ? ? ? ? 图2:pjlib中的链表结构 可以看到一般的双向链表是链表节点包含数据域,而pjlib中是数据域包含链表节点.一般的链表指针域是链表结构的指针,而pjlib中是数据结构的指针.这种结构的优势我还没有体会到,可能要慢慢体会吧,但对链表头的理解却造成些许困惑.链表头是一个单独的list,而prev和next指向的是含有list的data结构,这种结构总让我觉得怪怪的. Pjli

线性时间将两个有序链表合成一个有序链表(constant additional space)

description: given two sorted singly list, merge them into one using constant additional space algorithm: we will reference the two linked list as list1 and list2 for convenience, since list1 is sorted,just find the right position for each element in