Swapping between two nodes using the given elements which are present in these nodes. Python-3.0 is used for writing code

Refresh

6 days ago

Views

2 time

0

I have written code but errors are there when I am compiling the code. One error is I am using push function to add new nodes in the list and last_pointer node is working as local node. So every time when I call function, last_pointer is considered as fresh or unnamed node. Another error is in swapnodes function which I am not getting exactly. The error occurs at condition of "If elements or keys are not present in the list"

Error messages are:

Linked list before any swapping

AttributeError                            Traceback (most recent call last)
<ipython-input-10-809c289bb317> in <module>
     83 print("Linked list before any swapping")
     84 llist.display()
---> 85 llist.swapnodes(22,102)
     86 llist.swapnodes(22,-20)
     87 llist.swapnodes(22,13)

<ipython-input-10-809c289bb317> in swapnodes(self, key1, key2)
     36             node2=node2.next
     37         #if elements are not present in the list
---> 38         if(node1.next==None and node1.data!=key1):
     39             print("key1 is not present in the list")
     40             return

AttributeError: 'NoneType' object has no attribute 'next'

if I used alternate push function which is correct then error is

AttributeError                            Traceback (most recent call last)
<ipython-input-20-5ade422f1375> in <module>
     78 print("Linked list before any swapping")
     79 llist.display()
---> 80 llist.swapnodes(22,102)
     81 llist.swapnodes(22,-20)
     82 llist.swapnodes(22,13)

<ipython-input-20-5ade422f1375> in swapnodes(self, key1, key2)
     31             node2=node2.next
     32         #if elements are not present in the list
---> 33         if(node1.next==None and node1.data!=key1):
     34             print("key1 is not present in the list")
     35             return

AttributeError: 'NoneType' object has no attribute 'next'

# Swapping between two nodes present in linkedlist

class node:
    def __init__(self,data):
        self.data=data
        self.next=None
class linkedlist:
    def __init__(self):
        self.head=None
    def push(self,data):
        newnode=node(data)
        newnode.next=self.head
        self.head=newnode 
    def swapnodes(self,key1,key2):
        node1=self.head
        node2=self.head
        #if elements are similar
        if(key1==key2):
            print("keys are similar")
            return
        #find previous of element and element location in the list
        while(node1):
            if(node1==key1):
                break
            prevofnode1=node1
            node1=node1.next
        while(node2):
            if(node2==key2):
                break
            prevofnode2=node2
            node2=node2.next
        #if elements are not present in the list
        if(node1.next==None and node1.data!=key1):
            print("key1 is not present in the list")
            return
        if(node1.next==None and node2.data!=key2):
            print("key2 is not present in the list")
            return
        #if node present at beginning or at mid or at end
        temp1=node2.next
        temp2=node1.next
        if(prevofnode1==None):
            self.head=node2
            node2.next=node1.next
            prevofnode2=node1
            node1.next=temp1
            temp1=None
        else:
            prevofnode1=node2
            node2.next=node1.next
            prevofnode2=node1
            node1.next=temp1
            temp1=None
        if(prevofnode2==None):
            self.head=node1
            node1.next=node2.next
            prevofnode1=node2
            node2.next=temp2
            temp2=None
        else:
            prevofnode2=node1
            node1.next=node2.next
            prevofnode1=node2
            node2.next=temp2
            temp2=None
    def display(self):
        temp=self.head
        while(temp):
            print(temp.data)
            temp=temp.next
llist=linkedlist()
llist.push(22)
llist.push(92)
llist.push(-20)
llist.push(2)
llist.push(23)
llist.push(102)
print("Linked list before any swapping")
llist.display()
llist.swapnodes(22,102)
llist.swapnodes(22,-20)
llist.swapnodes(22,13)
llist.display()

0 answers