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

  

上一篇:Windows Service的官方描述,抄下来(不写obj就是LocalSystem)


下一篇:Java初学练习答案(循环)