Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Modifying the value in a singly linked list at specified index

I’m trying to implement a method in my linked list called set_value() where the value gets modified at that index. However, my code does not reflect the change whenever the function is called. I’m not able to understand why it is not updating the value at that index. I’m getting an AttributeError and I cannot resolve it.

I’ll be attaching the python code, along with the output and error:

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

class LinkedList:
    def __init__(self,value):
        newnode = Node(value)
        self.head = newnode
        self.tail = newnode
        self.length = 1 
    
    def printList(self):
        temp = self.head
        while temp is not None:
            print(temp.value)
            temp = temp.next
    
    def append(self,value):
        newnode = Node(value)
        if self.length == 0:
            self.head = newnode
            self.tail = newnode 
        else:
            self.tail.next = newnode
            self.tail = newnode 
            self.length += 1
        return True 
    
    def prepend(self,value):
        newnode = Node(value)
        if self.length == 0:
            self.head = newnode
            self.tail = newnode
        newnode.next = self.head 
        self.head = newnode
        self.length += 1 
        return True
    
    def pop(self):
        if self.length == 0:
            return None
        temp = self.head
        prev = self.head
        while temp.next is not None:
            prev = temp 
            temp  = temp.next
        self.tail = prev
        self.tail.next = None
        self.length -= 1 
        if self.length == 0:
            self.head = None
            self.tail = None
        return temp.value
    
    def popFirst(self):
        if self.length == 0:
            return None 
        temp = self.head 
        self.head = self.head.next
        temp.next = None
        self.length -= 1
        if self.length == 0:
            self.tail = None
        return temp.value
    
    def get(self,index):
        if index < 0 or index >= self.length:
            return None
        temp = self.head
        for _  in range(index):
            temp = temp.next
        return temp.value
        
    def set_value(self, index, value):
        temp = self.get(index)
        if temp is not None:
            temp.value = value
            return True
        return False

    
        

ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.prepend(4)
print('linkedlist:')
ll.printList()
print(f"popped element:{ll.pop()}")
print('linkedlist:')
ll.printList()
print(f"popped element:{ll.popFirst()}")
print('linkedlist:')
ll.printList()
print(f"element at index 0: {ll.get(0)}")
ll.set_value(0,10)
print('linkedlist:')
ll.printList()

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

linkedlist:
4
1
2
3
popped element:3
linkedlist:
4
1
2
popped element:4
linkedlist:
1
2
element at index 0: 1

Error:

AttributeError                            Traceback (most recent call last)
Cell In[17], line 98
     96 ll.printList()
     97 print(f"element at index 0: {ll.get(0)}")
---> 98 ll.set_value(0,10)
     99 print('linkedlist:')
    100 ll.printList()

Cell In[17], line 78, in LinkedList.set_value(self, index, value)
     76 temp = self.get(index)
     77 if temp is not None:
---> 78     temp.value = value
     79     return True
     80 return False

AttributeError: 'int' object has no attribute 'value'

Please let me know how I can resolve this error.

>Solution :

You are returining temp.value in get(self,index)

You probably want to return temp instead

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading