制作没有内置函数的链表

时间:2016-10-31 17:09:19

标签: python list class linked-list nodes

我有一个项目在python中创建一个链表。

我的程序需要添加到列表中,从中删除并获取元素。听起来很容易吗?错误!我们不允许使用普通列表或内置函数(基本print除外,str ...)

我的代码有一个问题,我必须初始化一个空白列表然后添加元素,1乘1.其他一切正常。

我的问题:

  

这是普通python列表的工作原理吗?

     

是否可以通过循环将项目添加到链接列表? (没有其他清单)

以下是代码:

class Node: # the node class
    def __init__(self, cargo = None, next = None): # __init__ stands for initialize
        self.cargo = cargo # e.g. steve
        self.next = next # represents the next node or None if its the last

    def __str__(self): # __str__ is called when the node is printed or converted to a string
        return str(self.cargo) # return a string

class List: # the main list class
    def __init__(self): # default is to initialize an empty list
        self.first_node = None      
        self.last_node = None
        self.length = 0

    def get(self, position, length): # function for efficiency
        if position == "end":
            position = length - 1 # last
        if position > length - 1: # can't go beyond last
            raise ValueError("List index out of range")

        prv_node = self.first_node
        node = self.first_node # start at the first
        num = 0
        while num < position: # go up to position
            prv_node = node # remember the previous node
            node = node.next # next node!
            num = num + 1

        return prv_node, node, position

    def add_node(self, cargo, position = "end"): # adds a node
        prv_node, node, position = self.get(position, self.length + 1) # +1 because the length is being increased

        print("adding node at "+str(position)+": "+str(cargo))            
        if position == 0: # first
            self.first_node = Node(cargo, next = self.first_node) # the first node is the new node
            if self.length == 0: # first node to be added
                self.last_node = self.first_node # there is only one node
        elif position == self.length: # last
            self.last_node.next = Node(cargo, next = None) # last_node.next was None, it is now a new node
            self.last_node = self.last_node.next # last node is now the new last node
        else: # normal
            prv_node.next = Node(cargo, next = node) # stick it in between
        self.length = self.length + 1 # length is now + 1

    def get_node(self, position): # gets a node
        ...

    def remove_node(self, position): # removes a node 
        ...

    def __str__(self): # when the list is printed
        node = self.first_node # start from the first
        string = ""
        while node != self.last_node: # go to the end
            string = string + str(node) + ", "  # print each node
            node = node.next
        string = string + str(self.last_node) # last node hasn't been added yet
        return string

# initialize    
mylist = List()
mylist.add_node("steve")
mylist.add_node("james")
mylist.add_node("tom")
mylist.add_node("david")
mylist.add_node("hoe-yin")
mylist.add_node("daniel")
print(mylist)

[编辑]第二个问题重新措辞

1 个答案:

答案 0 :(得分:0)

这里是如何在CPython中实现Python列表:http://www.laurentluce.com/posts/python-list-implementation/

如果你有其他可迭代的值,那么是:

for item in list_of_items:
    mylist.add_node(item)