简体   繁体   中英

Linked list in Python with single class (node)

Linked list in Python with single class (node). Is this possible?

I'm trying the following example that I found.

Since each node has a "next" I'm assuming that creates the list.

The result I'm getting is as follows:

$ python linked.py
Traceback (most recent call last):
  File "linked.py", line 40, in <module>
    insertAtBeginning("test1")
TypeError: insertAtBeginning() takes exactly 2 arguments (1 given)


#Node of a Singly Linked List
class Node:
  #constructor
  def __init__(self):
    self.data=None
    self.next=None

  #method for setting the data field of the node
  def setData(self,data):
    self.data=data

  #method for getting the data field of the node
  def getData(self,data):
    return self.data

  #method for setting the next field of the node
  def setNext(self,next):
    self.next=next

  #method for getting the next field of the node
  def getNext(self,next):
    return self.next

  #returns true if the node points to another node
  def hasNext(self):
    return self.next != None

def insertAtBeginning(self,data):
  newNode=Node()
  newNode.setData(data)

  if self.length==0:
    self.head=newNode
  else:
    newNode.setNext(self.head)
    self.head=newNode

  self.length+=1

insertAtBeginning("test1")
insertAtBeginning("test2")
def insertAtBeginning(self,data):

method declaration is missing tabs, that's why self doesn't resolve to object instance.

Also the head of your list should be held outside your node class, otherwise, every element of the list should be updated with the new head

You don't need to keep track of length in order to add a element at head position.

The algorithm is rather simple:

if myListHead == none:
    myListHead = new Node()
else:
    myNewHead = new Node()
    myNewHead.next = myListHead
    myListHead = myNewHead

this is pseudo python code...

the "self" here just like "this" in other language which stands for "current instance I'm in", not a arbitrary type of class,

so first, if the function was defined inside a class, it should called like self.insertAtBegin("bala")

second, if this is another function jusr trying to manage a sequence of node instance / or data structure, you should not use "self" as the input parameter, it's preserved keyword, maybe "node" or something else.

It was missing a class for the LinkedList, the author did not include this.

This works find, and the functions go in the LinkedList class, rather than the node class.

class LinkedList:
  #constructor
  def __init__(self):
    self.head=None
    self.length=0

  def insertAtBeginning(self,data):
    newNode=Node()
    newNode.setData(data)

    if self.length==0:
      self.head=newNode
    else:
      newNode.setNext(self.head)
      self.head=newNode

    self.length+=1

  def printList(self):
    current=self.head
    while current.getNext() != None:
      print current.data
      current=current.getNext()

    print current.data

newList=LinkedList()
newList.insertAtBeginning("test")
newList.insertAtBeginning("test123")
newList.printList()

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