[英]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. 我期待前4行(0~3)不显示任何内容,第5行显示为4,6第1行为5 4 ....依此类推。
At the end I want 9 8 7 6 5 4 最后我想要9 8 7 6 5 4
How do I fix the code? 我该如何修复代码?
Part of the problem is in the first part of your if statement in the for loop: 部分问题出在for循环中if语句的第一部分:
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. 设置
i=self.__size
不会退出循环。 Use break
instead. 请改用
break
。
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. 这使得当你找到你继续遍历循环的项目时,
current
不是你要删除的项目。 Instead current
is the value of the last node that you look at in the for loop. 相反,
current
是您在for循环中查看的最后一个节点的值。
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. 我想你在
if(found==True):
块中做了几次。 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
. 如果你有一个布尔变量(如
found
),则不需要检查found==True
因为它的计算结果与刚刚found
值相同。 ie: 即:
if(found==True):
...
is the same as: 是相同的:
if(found):
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.