user3411693
user3411693

Reputation: 21

What is wrong with my linked list?

I am trying to create a class for nodes (it's suppsoed to be a system for a queue) to import in another program. I know that the other program is working fine since it works when importing another type of queue that uses the same function names(that is based on Pythons regular list). With this queue-type however, I never seem to be able to get it to work. I'm sure that there are MANY problems with my code(Since I've been getting a big variety of error messages) and I would like some input on what i should change.

Here is the code for the class:

class Node():
    def __init__(self, v=None, n=None):
        self.item=v
        self.next=n

    lastnode=__init__

    def put(self, v):
        Node.lastnode.next=self
        self.item=v
        self.next=None
        Node.lastnode=Node.lastnode.next

    def get(self):
        if Node.isempty(self):
            return None
        out=Node.__init__.next
        Node.__init__.next=out.next
        out.next=None
        return ut.item

    def isempty(self):
        if Node.lastnode==Node.__init__:
            return True
        else:
            return False

The put functions role is to put a new item/node last in the queue.

The get functions role is to remove the first node from the queue and return its item.

The isempty function is there to check if the queue is empty.

Please give me some criticism on this.

Upvotes: 1

Views: 148

Answers (1)

Alp
Alp

Reputation: 2826

Your primary problem is that you're trying to use a single class to solve a problem that really requires two classes. Linked lists involve two kinds of items: The nodes and the list structure itself. A node only "knows" two things: the data it contains and the identity of the next node it links to. The list object is what manages access to a group of linked nodes. By trying to combine the two roles in a single class you're making it impossible to realize a list. Think about, e.g., how your class could manage multiple nodes. You're trying to get around such difficulties, it seems, by using class attributes for list-level data (such as the identity of the head node), but even if that could be made to work you'd only be able to work with one list instance at a time.

In addition to this, your code has some basic syntactical issues. E.g., the line lastnode=__init__ does not call __init__; call syntax would be lastnode=__init__(), but you can't do that in the body of the class definition.

Here's a trivial implementation you could study:

class Node(object):

    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

class List(object):

    def __init__(self):
        self.head = None

    def put(self, value):
        new = Node(value)
        new.next = self.head
        self.head = new

    def get(self):
        if self.is_empty():
            return None
        old = self.head
        self.head = old.next
        return old.value

    def is_empty(self):
        return self.head is None

Upvotes: 1

Related Questions