在python中

时间:2017-02-24 22:06:19

标签: python data-structures linked-list nodes

我正在尝试创建一个函数,该函数接收链表和值的头部,创建具有该值的节点,将node.next设置为head,然后将新节点更新为head。我得到了它的工作,但我觉得我通过返回值非常低效地做这个,所以我想知道是否有更优雅的方法来做到这一点。

这是我正在使用的代码:

#!/usr/bin/env python3
"""
This module is a linked list implementation.
"""

class Node:
    """
    Node structure to be used in our linked list.
    """
    def __init__(self, value):
        self.value = value
        self.next = None

def linked_insert(head, value):
    """
    Insert new node to the tail of the linked list.
    Time Complexity: O(n)
    """
    current = head
    while current.next is not None:
        current = current.next
    current.next = Node(value)

def linked_insert2(head, value):
    """
    Insert new node to the head of the linked list, making sure to update head.
    Time Complexity: O(1)
    """
    to_insert = Node(value)
    to_insert.next = head
    head = to_insert
    return head


def linked_extract(head):
    """
    Extract the last element in the linked list
    """
    current = head

    while current.next.next is not None:
        current = current.next

    tail = current.next
    current.next = None

    return tail


def linked_display(head):
    """
    Print all node values in the linked list
    """
    current = head
    while current is not None:
        print(current.value)
        current = current.next


# Test Program
head = Node(5)
# linked_insert(head, 1)
# linked_insert(head, 2)
# linked_insert(head, 3)
#
# print(linked_extract(head).value)

head = linked_insert2(head, 1)
head = linked_insert2(head, 2)
head = linked_insert2(head, 3)
linked_display(head)

有没有办法在不必返回head的值并在测试程序中设置head =返回值的情况下执行此操作?

有问题的函数是linked_insert2()。整个程序是python中的链表实现。

使用链表类实现:

#!/usr/bin/env python3

"""
This module is a linked list implementation.
"""

class Node:
    """
    Node class to be used in our linked list.
    """
    def __init__(self, value):
        self.value = value
        self.next = None


class Linkedlist:
    """
    Linked list implementation that supports functions including inserting
    at head, inserting at tail, extracting elements, and displaying all
    elements in the ist
    """
    def __init__(self, head):
        self.head = Node(head)

    def insert(self, value):
        """
        Insert new node to the tail of the linked list.
        Time Complexity: O(n)
        """
        current = self.head
        while current.next is not None:
            current = current.next
        current.next = Node(value)

    def insert2(self, value):
        """
        Insert new node to the head of the linked list, making sure to update head.
        Time Complexity: O(1)
        """
        to_insert = Node(value)
        to_insert.next = self.head
        self.head = to_insert


    def extract(self):
        """
        Extract the last element in the linked list
        """
        current = self.head

        while current.next.next is not None:
            current = current.next

        tail = current.next
        current.next = None

        return tail


    def display(self):
        """
        Print all node values in the linked list
        """
        current = self.head
        while current is not None:
            print(current.value)
            current = current.next


# Test Program
head = Linkedlist(5)
# linked_insert(head, 1)
# linked_insert(head, 2)
# linked_insert(head, 3)

head.insert2(1)
head.insert2(2)
head.insert2(3)
head.display()

0 个答案:

没有答案