Python Forum
set a new object node in a linked list via reference
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set a new object node in a linked list via reference
#1
Hello,

I have written a script to create a circular list. The problem is that, when I add a node to the list using the push function, it is not actually added. I guess this is a problem of assigning the value via reference. Is there a way of doing this without returning the list from the function push? It looks like that the problem is in the line current_pointer = new_node. Here is the code

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

class CircularLinkedList:
  def __init__ (self):
    self.head = None

  def push (self, data):
    new_node = Node(data)

    if self.head == None:
      self.head = new_node
    else:
      current_pointer = self.head
      while current_pointer != None:
        current_pointer = current_pointer.next
      current_pointer = new_node
      

  def printCircularLinkedList(self):
    current_head = self.head
    while current_head != None:
      print("printing:", current_head.data)
      current_head = current_head.next


cllist = CircularLinkedList()

# Created linked list will be 11->2->56->12 
cllist.push(12) 
cllist.push(56) 
cllist.push(2) 
cllist.push(11) 
cllist.printCircularLinkedList() 
Reply
#2
I don't see where you're ever setting a value for next.
After first push, next = None
I think line 17 should be:
current_pointer = self.head.next
Reply
#3
Got it. This is how it should be implemented:

  def push (self, data):
    new_node = Node(data)
    if self.head == None:
      self.head = new_node
    else:
      new_node.next = self.head
      self.head = new_node
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to return a reference to an object? Skaperen 8 1,110 Jun-07-2023, 05:30 PM
Last Post: Skaperen
  add object and name in list 3lnyn0 4 1,226 Nov-24-2022, 07:33 PM
Last Post: buran
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,721 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 14,781 Jan-19-2022, 08:33 AM
Last Post: menator01
  About linked lists ManoEl 2 1,570 Oct-17-2021, 03:21 PM
Last Post: ManoEl
Exclamation win32com: How to pass a reference object into a COM server class Alfalfa 3 4,802 Jul-26-2021, 06:25 PM
Last Post: Alfalfa
  AttributeError: 'Node' object has no attribute 'insert' Anldra12 4 6,384 May-11-2021, 10:12 AM
Last Post: Anldra12
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 2,596 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  AttributeError class object has no attribute list object scttfnch 5 3,345 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  'list' object not callable sidra 5 5,442 Nov-29-2020, 04:56 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020