创建一个没有内置函数的链表



我有一个项目,用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)

[EDIT]第二个问题改写

以下是Python列表在CPython中的实现方式:http://www.laurentluce.com/posts/python-list-implementation/

如果你的值在其他可迭代对象中,那么yes:

for item in list_of_items:
    mylist.add_node(item)

相关内容

  • 没有找到相关文章

最新更新