Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在双链接列表中创建自己的删除方法_Java_Linked List - Fatal编程技术网

Java 在双链接列表中创建自己的删除方法

Java 在双链接列表中创建自己的删除方法,java,linked-list,Java,Linked List,我需要在双链接列表中创建一个delete方法。我有麻烦,因为我认为我需要有4个案例 如果列表为空 如果要删除的节点是头 如果节点是尾部 如果节点位于列表的中间位置 这是我到目前为止掌握的代码 public void delete(Node n) { if (head == null) { System.out.println("the list is empty"); } else if (head != null) { head = n;

我需要在双链接列表中创建一个delete方法。我有麻烦,因为我认为我需要有4个案例

  • 如果列表为空
  • 如果要删除的节点是头
  • 如果节点是尾部
  • 如果节点位于列表
  • 的中间位置 这是我到目前为止掌握的代码

    public void delete(Node n) {
        if (head == null) {
            System.out.println("the list is empty");
        } else if (head != null) {
            head = n;
            Node newHead = n.next;
            newHead = head;
        } else if (n.next == null) {
            Node beforeTail = n.previous;
            beforeTail.next = null;
        } else if (n.next != null || n.previous != null) {
            Node inFront = n.previous;
            Node inBack = n.next;
            inFront.next = inBack;
            inBack.previous = inFront;
        } else {
            System.out.println("error");
        }
    }
    
    以下是测试程序:

     public class TestLL {
     public static void main(String[] args){ 
         /*Create a bunch of free standing nodes */ 
         Node n1= new Node(new Integer(11)); 
         Node n2= new Node(new Integer(12)); 
         Node n3= new Node(new Integer(13)); 
         Node n4= new Node(new Integer(14)); 
         Node n5= new Node(new Integer(15)); 
         Node n6= new Node(new Integer(16)); 
         Node n7= new Node(new Integer(17)); 
    
         /* link them */ 
         LL myLL =new LL(); 
         myLL.printList(); // prints "empty list"
         myLL.add(n1); //11 
         myLL.add(n3); //13 
         myLL.add(n5); //15 
         myLL.add(n2); //12 
         myLL.add(n7); //17 
         myLL.printList(); //should print 11, 13, 15, 12, 17; one per line 
         System.out.println(); 
         myLL.delete(n3); 
         myLL.addAfter(n4,n1); 
         myLL.printList(); //should print 11,14,15,12,17 one per line 
         System.out.println(); 
         myLL.delete(n7); 
         myLL.delete(n2); 
         myLL.printList();//should print 11,14,15 one per line 
             } 
         }
    

    我真的不知道该怎么办。而且我不能使用Java中已有的任何方法

    很难说,但LL方法看起来是错误的。通常,使用LL不需要任何“节点”意识,因为处理所有链接细节取决于实现。调用的代码应该传入它选择的对象

    我希望LL的用法是这样的

    LL myLL = new LL();
    myLL.add(new Integer(1));
    myLL.add(new Integer(2));
    // etc
    
    myLL.remove(new Integer(1));
    
    在添加新节点(LL的内部类)的调用期间,将创建一个新节点并将其追加到末尾。Remove/delete将搜索LL并删除与传入对象匹配的第一个实例

    e、 g.LL实施的草图

    class LL
    {
       private Node headNode = null;
    
       public void add(Object item)
       {
          // construct a new node
          // iterate to the last node and add new node to the end
       }
    
       public boolean remove(Object item)
       {
          // starting at the head node search the list until a node with the matching item is found
          // update the node pointers to "remove" the node
       }
    
       class Node
       {
           Node nextNode;
           Node prevNode;
           Object item;
       }
    }
    
    我不会填写实现,因为这是家庭作业;)但你是对的,有几个案子要处理

    • 列表为空
    • 项目不存在
    • 项目位于head节点中
    • 项目位于另一个节点中

    祝你好运

    我建议您看看LinkedList已经采用的方法。您可能会发现代码要简单得多此外,我们不知道发生了什么,也不知道发生的事情与您的预期相比如何。一个明显的问题是,如果
    head
    不是
    null
    ,那么
    if
    语句将运行,而不包括其他所有内容。在第一个之后,
    head!=空值
    总是
    真值
    -为什么要检查?接下来,不再删除
    节点n
    ,而是开始移除头部,插入
    节点。然后,将
    n.next
    保存到一个变量,并立即覆盖它。是否确实需要这样做?这个列表可以继续下去,但我相信你必须在餐巾纸上画一个双链接列表,并模拟这种情况;当你知道哪些指针指向哪里时,你就可以很好地重写这个方法了。我试着把它画出来看看。非常感谢谢谢,这很有帮助,这也是家庭作业,但是测试课,创建列表的那门课实际上已经由教授给出了它的方式。