简体   繁体   中英

Double linked list without class

I am still learning python and have the following code:

class Node:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

    def traverse(self):
        node = self
        while node is not None:
            print(node.val)
            node = node.next


def LinkedListCreator(non_linked_list):
    linked_list = []
    for element in non_linked_list:
        node = Node(element)
        if(len(linked_list)) > 0:
            linked_list[-1].next = node
        linked_list.append(node)
        if(len(linked_list)) > 1:
            prev_node = linked_list[-2]
            linked_list[-1].prev = prev_node
    linked_list[0].prev = linked_list[-1]
    linked_list[-1].next = linked_list[0]
    return linked_list

abc = [1, 2, 3]
deg = LinkedListCreator(abc)

deg[0].traverse()

Now, given that this is a doubly linked list (but not really, since the list itself is not a class), how do I traverse it without going into an endless loop? The obvious choice would be to save the first element and stop the iteration when it reaches it again. Is this also a correct implementation of a doubly linked list with a function, or should linked lists strictly be implemented as a class with their own class methods?

In the future I'd suggest posting this sort of question to https://codereview.stackexchange.com/

With respect to:

how do I traverse it without going into an endless loop? The obvious choice would be to save the first element and stop the iteration when it reaches it again.

I'd do:

class Node:
    …

    def iter_vals(self):
        yield self.val
        node = self.next
        while node is not self:
            yield node.val
            node = node.next

I yield elements so the caller can decide what they want to do with them, rather than always printing them. There are various ways of arranging this while loop, but none seems particularly better, eg:

    def iter_vals(self):
        node = self
        while True:
            yield node.val
            node = node.next
            if node is self:
                break

would also work, but seems less clear. With respect to your second question:

Is this also a correct implementation of a doubly linked list with a function, or should linked lists strictly be implemented as a class with their own class methods?

I've not run your code, but it looks about right. Whether it's worth creating a whole separate class for it depends on various engineering tradeoffs that you've not given any information about. For example:

  • what makes more sense to the callers
  • does it need to hold any more state
  • how are you going to be testing
  • what other methods will you want to package with it

one thing I'd suggest would be naming conventions… as you can see from how your code was colored, only class/type names tend to be CamelCased while other identifiers are lowercase. I'd therefore name your LinkedListCreator something like create_linked_list .

You don't really have to "save" the first element, as that is what your self Variable already does. The minimal addition to make your code work would be to break if the nodes sucessor equals the traversing node:

    def traverse(self):
        node = self
        while node is not None:
            print(node.val)
            node = node.next
            if node == self:
                break

or equally but maybe a bit more intuitive to read:

    def traverse(self):
        node = self
        while node is not None:
            print(node.val)
            if node.next == self:
                break
            node = node.next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM