Я пытаюсь удалить узлы с заданным ключом, а также хочу отображать обновленные значения хвостового и головного узлов. Я могу удалить первый узел (Head) и не может удалить Tail узел Пожалуйста, проверьте мой код нижеПопытка удалить узел в одиночном круговом связанном списке
public void delete(int key){
Node current = head;
while(current.next.data != key){
current = current.next;
}
if(current.next.data == key){ //deleting current node
current.next = current.next.next;
if(current.next == head)
tail = current;
else if(current == head)
head = current.next;
}
}
Мой главный метод:
public class Caller {
public static void main(String args[]){
Linklist theList = new Linklist();
theList.insertFirst(22);
theList.insertFirst(44);
theList.insertFirst(55);
theList.insertFirst(66);
theList.delete(22);
System.out.println("deleting 22");
theList.display();
theList.delete(66);
System.out.println("Deleting 66");
theList.insertLast(99);
theList.insertLast(11);
theList.display();
}
}
мой метод insertLast:
public void insertLast(int data){
Node newNode = new Node(data);
Node current = head;
while(current.next != head){
current = current.next;
}
current.next = newNode;
newNode.next = head;
tail = newNode;
}
и мой выпуск:
deleting 22
Displaying list first ----> last
{ 66 }
{ 55 }
{ 44 }
Head : 66 Tail: 44
Deleting 66
Ничего не происходит после этого кода
Не могли бы вы поделиться своим методом 'insertLast()'? Вот где ваш код, кажется, застрял - не внутри 'delete()'. –
updated @MickMnemonic – nitishpisal
Примечание: «Ничего не происходит» очень неопределенно ... программа просто выходит из игры тихо или просто вращается, пока вы не решите ее прекратить? Это важное различие. – Lars