Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with implementing a doubly-linked list

I'm making doubly-linked list class.

def remove(self, item):
    current=self.__head
    prev=None
    found=False
    if(self.__size>=1):
        for i in range (0,self.__size):
            if(current.getData()==item):
                found=True
                pos=i#save the position to pop
                i=self.__size#leave the loop
            else:
                prev=current
                current=current.getNext()

        if(found==True):#excute only if the item is found
            if(prev==None):#first item found
                if(self.__size==1):
                    self.__head==None
                    self.__tail==None
                    current.setNext(None)
                    current.setPrevious(None)
                else:#size bigger than 2
                    self.__head==current.getNext()
                    current.setNext(None)
            else:
                if(current.getNext()==None):#last item found
                    self.__tail==prev
                    current.setPrevious(None)
                    prev.setNext(None)
                else:
                    Next=current.getNext()
                    current.setNext(None)
                    current.setPrevious(None)
                    Next.setPrevious(prev)
                    prev.setNext(Next)
            self.pop(pos)
            self.__size-=1

This is what I did so far. If I run the code below

 for i in range(0, 10):
    int_list2.add(i)
    int_list2.remove(1)
    int_list2.remove(3)
    int_list2.remove(2)
    int_list2.remove(0)
    print(int_list2)

these are the output that I get

0

1

2

3

4 3

5 4 3

6 5 4 3

7 6 5 4 3

8 7 6 5 4 3

9 8 7 6 5 4 

I'm expecting first 4 rows (0~3) to display nothing and from the fifth one as 4, 6th one as 5 4....and so on.

At the end I want 9 8 7 6 5 4

How do I fix the code?

like image 827
Juho Kim Avatar asked Dec 08 '25 09:12

Juho Kim


1 Answers

Part of the problem is in the first part of your if statement in the for loop:

for i in range (0,self.__size):
        if(current.getData()==item):
            found=True
            pos=i#save the position to pop
            i=self.__size#<--- doesn't leave the loop 
        else:
            prev=current
            current=current.getNext()

Setting i=self.__size does not exit the loop. Use break instead.

This makes it so that when you find the item you keep iterating through the loop and current is not the item that you want to remove. Instead current is the value of the last node that you look at in the for loop.

Also you are using == when I'm sure you mean assignment = so on these lines:

self.__head==None   #should be self.__head=None
self.__tail==None   #should be self.__tail=None
current.setNext(None)
current.setPrevious(None)

change the == to a single =. I think you do it a few times in your if(found==True): block. These lines are just evaluating the boolean expression and throwing the value away.

Change those things, and let me know if that fixes it.

Also, just a little tip:

If you have a boolean variable (like found) you don't need to check found==True because it evaluates to the same value as just found. i.e.:

if(found==True):
   ...

is the same as:

if(found):
   ...
like image 98
Raufio Avatar answered Dec 10 '25 05:12

Raufio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!