Java双链表removeLast()
我有一个类分配,它要求我创建一个实现ListADT的类DoubleList 我已经到了代码几乎可以正常工作的地步。代码的输出应为: 1 3 7 9 13 14 16 17 23 24 391316 我的输出是: 1 3 7 9 13 14 16 17 23 24 39131623 第一个removeLast似乎删除了24,但由于某些原因,23在调用第二个removeLast之后仍然保留。请帮忙 编辑:如果我下次调用removeLast,它将删除23Java双链表removeLast(),java,Java,我有一个类分配,它要求我创建一个实现ListADT的类DoubleList 我已经到了代码几乎可以正常工作的地步。代码的输出应为: 1 3 7 9 13 14 16 17 23 24 391316 我的输出是: 1 3 7 9 13 14 16 17 23 24 39131623 第一个removeLast似乎删除了24,但由于某些原因,23在调用第二个removeLast之后仍然保留。请帮忙 编辑:如果我下次调用removeLast,它将删除23 class DoubleList<T&g
class DoubleList<T> implements ListADT<T>{
private int _size;
private DoubleNode _head;
private DoubleNode _tail;
public DoubleList() {
_size = 0;
_head = null;
_tail = null;
}
public T removeFirst(){
if(_size == 0){
return null;
}
DoubleNode tmp = _head;
_head = _head._next;
_head._previous = null;
_size--;
return tmp._value;
}
public T removeLast(){
if(_size == 0) {
return null;
}
T temp = _tail._value;
_tail = _tail._previous;
_tail._next = null;
_size--;
return temp;
}
public T remove(T element){
if(_size == 0){
return null;
}
DoubleNode current = _head;
DoubleNode previous = null;
T temp = null;
do{
if(current._value == element){
temp = current._value;
if(previous == null){
_head = _head._next;
_head._previous = null;
}
else{
previous._next = current._next;
}
}
previous = current;
current = current._next;
}while(current != null);
return temp;
}
public T first(){
return _head._value;
}
public T last(){
return _tail._value;
}
public boolean contains(T target){
if(_size == 0){
return false;
}
DoubleNode temp = _head;
do{
if(temp._value == target){
return true;
}
temp = temp._next;
}while(temp != null);
return false;
}
public boolean isEmpty(){
if(_size == 0){
return true;
}
return false;
}
public int size(){
return _size;
}
public void add(T element)
{
int add = 0;
DoubleNode temp = new DoubleNode();
temp._value = element;
DoubleNode point = _head;
DoubleNode placeHolder;
if(_head == null) {
_head = temp;
_tail = temp;
_size++;
return;
}
else if((Integer)element <= (Integer)_head._value){
temp._next = _head;
_head._previous = temp;
_head = temp;
_size++;
return;
}
do {
if(point._next == null){
point._next = temp;
temp._previous = point;
_tail = temp;
_size++;
return;
}
else if((Integer)point._next._value >= (Integer)element && (Integer)point._value < (Integer)element){
placeHolder = point._next;
point._next = temp;
placeHolder._previous = temp;
temp._next = placeHolder;
temp._previous = point;
_size++;
return;
}
point = point._next;
} while (point != null);
_size++;
}
public String toString(){
String returnString = "";
if(_size == 0){
return returnString;
}
DoubleNode temp = _head;
do{
returnString += temp._value + " ";
temp = temp._next;
}while(temp != null);
return returnString;
}
private class DoubleNode {
private DoubleNode _previous;
private DoubleNode _next;
private T _value;
public DoubleNode() {
_previous = null;
_next = null;
_value = null;
}
public DoubleNode(T value){
_previous = null;
_next = null;
_value = value;
}
}
}
/**
* DoubleOrderedList testing area.
*
* @author (your name), Acuna
* @version (version)
*/
class Driver {
public static void main(String [] args) {
DoubleList<Integer> list = new DoubleList<>();
//RA: These are _extremely_ simple tests - do not use them when doing
// your writeup.
list.add(23);
list.add(24);
list.add(16);
list.add(3);
list.add(7);
list.add(17);
list.add(9);
list.add(13);
list.add(14);
list.add(1);
System.out.println("\nsize = " + list.size());
System.out.println(list);
list.remove(7);
System.out.println(list);
list.removeFirst();
System.out.println(list);
list.remove(17);
System.out.println(list);
list.removeLast();
System.out.println(list);
list.remove(14);
System.out.println(list);
list.removeLast();
System.out.println(list);
我认为你的臭虫没有被清除。它在remove函数中,它会破坏列表,导致对列表的进一步操作行为不正确
从双链接列表的中间删除项目时,需要在删除项目之前缝合项目,在删除项目之后缝合项目。您正在执行第一个操作,但不是第二个操作。例如,假设列表中有三个元素:L X R。我想删除X。我必须设置L._next=R,如果你这样做,还必须设置R._previous=L,如果你不这样做。此时,您的列表将被损坏,因为反向链接已关闭。请查看删除函数中的循环
do{
if(current._value == element){
temp = current._value;
if(previous == null){
_head = _head._next;
_head._previous = null;
}
else{
previous._next = current._next;
//next line is missing
previous._next._previous = previous;
}
}
previous = current;
current = current._next;
}while(current != null);
此外,在循环电流时也没有效率!=空,考虑一下!找到当前文件(&C)=null或break语句。请解释程序的意图,即给定输入如何返回预期输出。另外,您在这里指的是哪个ListADT类?对于您的特定问题,您的示例相当长。我想这是一个很好的主题,但我相信如果这是你想要的,你会从你的整个代码中得到更多的反馈。这个程序是一个双链接列表,可以存储元素。为了便于使用,我们正在使用整数。作为任务的一部分,我们必须自己实施ADT。删除最后一项的逻辑似乎无法正常运行。编辑:我可以只发布我遇到问题的部分,但我认为如果你看不到整个逻辑,那会更加混乱。调试这个问题的一个简单方法是编写一个从列表开头开始并遍历列表的方法,打印每个元素。您可以在每次修改列表后调用该方法,以确保您希望发生的事情发生在您实际完成此操作之后。必须调用removeLast一次才能删除24个,但两次才能从末尾删除23个,我不确定是什么导致了这种行为。完美。第一段有足够的信息让我找到问题所在。非常感谢。