#java #data-structures
Вопрос:
public void DeleteLine(int linenum) { NodeLine newLine = new NodeLine(); if (linenum lt; 1) { System.out.print("nposition should be gt;= 1."); return; } if (firstLine == newLine) { firstLine = newLine.getNextLine(); if (newLine.getNextLine() != null) newLine.getNextLine().setPrevLine(newLine.getPrevLine()); if (newLine.getPrevLine() != null) newLine.getPrevLine().setNextLine(newLine.getNextLine()); } }
удалить в двусвязном списке
удалить в двусвязном списке
Комментарии:
1. Можете ли вы добавить дополнительную информацию, такую как класс NodeLine, ограничения, что вы уже пробовали и т.д.
Ответ №1:
во-первых , вы не можете инициализировать глава связанного списка в функцию удаления, так как заявление NodeLine newLine = new NodeLine();
должно быть в конструкторе, а не в функцию удаления, так как эта линия будет создать новый пустой список каждый раз, когда вы удаляете , есть много изменений в этом связанном списке , я один я думаю, что это будет работать, и вы можете сравнить свой код к вам-и если вы не поняли что-то из моего кода , Вы можете задать мне, обратите внимание , этот код был написан с использованием IntelliJ идея :
package com.example.test; class Node { private int Val; private Node next; private Node previous; public Node(int val) { Val = val; next = null; previous = null; } public int getVal() { return Val; } public void setVal(int val) { Val = val; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public Node getPrevious() { return previous; } public void setPrevious(Node previous) { this.previous = previous; } } class DoublelinkedList { private Node head; private int size; public DoublelinkedList() { this.head = null; size = 0; } public Node getHead() { return head; } public void setHead(Node head) { this.head = head; } public void insertAtEnd(int val) { Node temp = new Node(val); size ; if (head == null) { this.head = temp; } else { Node t = this.head; while (t.getNext() != null){ t = t.getNext(); } t.setNext(temp); temp.setPrevious(t); } } public void DeleteLine(int nodeNum) { if (nodeNum lt; 1) { System.out.print("nposition should be gt;= 1."); return; } //make sure the node number is already present if(nodeNum gt; size) { System.out.println("index is out of range"); } else { size--; //corner case when deleting head node if(nodeNum == 1){ Node t = head; head = head.getNext(); head.setPrevious(null); t.setNext(null); return; } //get the node that you want to delete Node tobeDeleted = head; for(int i = 1; i lt; nodeNum ;i ) { tobeDeleted = tobeDeleted.getNext(); } //storing the address of next and previous node Node next = tobeDeleted.getNext(); Node previous = tobeDeleted.getPrevious(); //deleting the node previous.setNext(tobeDeleted.getNext()); //corner case when deleting last node if(next != null) next.setPrevious(tobeDeleted.getPrevious()); tobeDeleted.setPrevious(null); tobeDeleted.setNext(null); } } public void printList(){ Node t = head; while (t != null){ System.out.print(t.getVal() " "); t = t.getNext(); } } } public class Main { public static void main(String[] args) { DoublelinkedList l = new DoublelinkedList(); l.insertAtEnd(1); l.insertAtEnd(2); l.insertAtEnd(3); l.insertAtEnd(4); l.insertAtEnd(5); l.insertAtEnd(6); l.DeleteLine(3); l.printList(); } }