博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python---用链表结构实现有序和无序列表的几个功能
阅读量:5339 次
发布时间:2019-06-15

本文共 4373 字,大约阅读时间需要 14 分钟。

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

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

但其实,在普通使用中,这样实现的性能,并没有python原生的列表性能好。

因为python原生列表的功能,是基于数组作扩展实现的。

# coding: utf-8class 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_nextclass 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==================6True55493177731============OrderedList==================6True51731547793Process finished with exit code 0

  

转载于:https://www.cnblogs.com/aguncn/p/10660188.html

你可能感兴趣的文章
.net webService代理类
查看>>
Code Snippet
查看>>
Node.js Express项目搭建
查看>>
zoj 1232 Adventure of Super Mario
查看>>
1201 网页基础--JavaScript(DOM)
查看>>
组合数学 UVa 11538 Chess Queen
查看>>
oracle job
查看>>
Redis常用命令
查看>>
XML学习笔记(二)-- DTD格式规范
查看>>
IOS开发学习笔记026-UITableView的使用
查看>>
[转载]电脑小绝技
查看>>
windos系统定时执行批处理文件(bat文件)
查看>>
thinkphp如何实现伪静态
查看>>
BZOJ 2243: [SDOI2011]染色( 树链剖分 )
查看>>
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>
c++中的string常用函数用法总结!
查看>>
界面交互之支付宝生活圈pk微信朋友圈
查看>>
[DLX精确覆盖+打表] hdu 2518 Dominoes
查看>>
SuperMap iServerJava 6R扩展领域开发及压力测试---判断点在那个面内(1)
查看>>
Week03-面向对象入门
查看>>