Python Forum
reference in pop function for linked list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reference in pop function for linked list
#1
Hello, I have designed two methods, pop1 and pop2, to remove an element from a linked list. Both functions pop1 and pop2 manages two cases:

1) if the element to be removed is the head of the list
2) if the element to be removed is NOT the head of the list

The difference between pop1 and pop2 functions is how they manage the case 1). The only difference is that in pop1, I store the pointer of 'self.head' in 'current', and use 'current' to change the pointer to the next element, while in pop2 I use directly the pointer 'self.head' to change the pointer to the next element. However, the function pop1 is not working, while the function pop2 is working. Why? From my understanding, in pop1 when I execute the line 'current = self.head', 'current' should be the pointer of 'self.head' and then be able to change the next pointer, but this is not happening. After calling the function pop1 to remove the first element of the list, the list actually does not change.

class CircularLinkedList:
  def __init__ (self):
    self.head = None
  
  def pop1 (self, x):
    if self != None:

      current = self.head
      
      # if the element to be removed is the head of the list
      if current.data == x: 
        current = current.next

      # if the element to be removed is NOT the head of the list
      else:
        prev = self
        while current != None:
          if current.data == x:
            prev.next = current.next
          prev = current
          current = current.next


  def pop2 (self, x):
      if self != None:

        current = self.head
      
        # if the element to be removed is the head of the list
        if self.head.data == x: 
          self.head = self.head.next

        # if the element to be removed is NOT the head of the list
        else:
          prev = self
          while current != None:
            if current.data == x:
              prev.next = current.next
            prev = current
            current = current.next
but if I replace 'self.head' with 'current' it is not working anymore. From my understanding, 'current' contains the pointer to 'self.head', so it should modify the pointer in case the value to be deleted is in the first position of the list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  reference from another function Frankduc 10 2,246 Mar-01-2022, 01:10 PM
Last Post: Frankduc
  About linked lists ManoEl 2 1,570 Oct-17-2021, 03:21 PM
Last Post: ManoEl
  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
  How to create a linked list and call it? loves 12 4,358 Nov-22-2020, 03:50 PM
Last Post: loves
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  Mathplotlib - passing reference to axs to function qmfoam 5 2,903 Aug-17-2020, 09:02 PM
Last Post: qmfoam
  set a new object node in a linked list via reference oloap 2 2,056 Mar-13-2020, 09:45 PM
Last Post: oloap
  Problem with List Reference CH_NoLuck 1 1,657 Feb-22-2020, 03:27 AM
Last Post: CH_NoLuck
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,247 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Deleting the first item in linked list dan789 7 3,885 Mar-05-2019, 06:34 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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