码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-04-05 23:52:57      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:rem   bash   test   not   set   __init__   coding   .exe   get   

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

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

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

标签:rem   bash   test   not   set   __init__   coding   .exe   get   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!