简体   繁体   English

从链表中删除索引处的项目?

[英]Removing Item at index from a linked list?

   public class LinkedList
   {
  private Node head;
  private Node tail;
  private int numberOfElements;

  public LinkedList()
  {
      head = null;
      tail = null;
      this.numberOfElements = 0;
  }

  public int length()
  {
      return this.numberOfElements;
  }

  public void removeFront()
  {
      Node currNode = head;
      head = head.getNextNode();
      currNode.setNextNode(null);
      this.numberOfElements--;  
  }

  public void removeLast()
  {
      this.tail = this.tail.prev;

  }

  public void removeAtIndex(int index) throws AmishException 
  {
     if (index == 0) {
           Node q = head;
           head = q.getNextNode();
      }
      else if ((index > numberOfElements - 1) || (index < 0)) 
           System.out.println("Index out bounds.");
      else {
        Node currNode = head;
        for (int i = 0; i < index; i++) {

            currNode = currNode.getNextNode();
        }
        Node temp = currNode;
        currNode = temp.getPrevNode();
        currNode.setNextNode(temp.getNextNode());
        temp = null;
        numberOfElements--;
    }




  }

Node - 节点 -

 public class Node 
 {
private int payload;
private Node nextNode;
public Node prev;
public Node previous;
public Node next;

public Node(int payload)
{
    this.payload = payload;
    nextNode = null;
}

public Node getNextNode()
{
    return this.nextNode;

}

public Node getPrevNode()
{
    return this.getPrevNode();
}
public void setNextNode(Node n)
{
    this.nextNode = n;
}

public void display()
{
     System.out.print("(" + super.toString() + " : " + this.payload + ")");
     if(this.nextNode != null)
     {
         System.out.print(" -> ");
         this.nextNode.display();
     }
     else
     {
         System.out.println("");
     }
}

public String toString()
{
    return "" + super.toString() + " : " + this.payload;
}

public void setPrevNode(Node n) 
{

        previous = n;


}




  }

Driver - 司机 -

    public class Driver 
     {

      public static void main(String[] args) throws Exception
      {
          LinkedList ll = new LinkedList();
          ll.addFront(7);
          ll.addFront(5);
          ll.addEnd(13);
          ll.addEnd(27);
          ll.addFront(2);
          ll.addAtIndex(1, 0);
          ll.addAtIndex(12, 6);
          ll.addAtIndex(9, 2);
          ll.addAtIndex(11, 2);

          //ll.removeFront();

          //ll.removeLast();

          ll.removeAtIndex(1);



          for(int i = 0; i < ll.length(); i++)
          {
              System.out.println(ll.get(i));
          }


       }    
   }

I got what I need to remove the head's index, but how to do anything else is beyond me. 我得到了我需要删除头部索引,但如何做其他事情超出了我。 I have the methods to remove the head and the tail now I just need to know how to remove at an index just Like if I were to add at an index. 我有删除头部和尾部的方法现在我只需要知道如何在索引处删除就像我要在索引处添加一样。

Okay, completely editing this answer. 好的,完全编辑这个答案。 I took your code and went from that. 我拿了你的代码然后离开了。 There were some issues with your Node class, check out mine. 您的Node类存在一些问题,请查看我的。 I created a barebones Node class, add the any other functionality you may need/want. 我创建了一个准系统Node类,添加了您可能需要/想要的任何其他功能。 For the LinkedList class, I created a simple add method that adds to the end of the list. 对于LinkedList类,我创建了一个简单的add方法,它添加到列表的末尾。 Use this as a prototype for your other adds. 将此作为其他添加的原型。 Everything right now is working and I put some println's to help you see where the program is at. 现在一切都正常,我放了一些println来帮助你看看程序在哪里。 I highly recommend using a debugger. 我强烈建议使用调试器。 Also note that this is a doubly linked list. 另请注意,这是一个双重链接列表。

Linked List 链接列表

public class LinkedList {
  private Node head;
  private Node tail;
  private int numberOfElements;

  public LinkedList()
  {
      head = null;
      tail = null;
      numberOfElements = 0;
  }

  public int length()
  {
      return numberOfElements;
  }

  public void removeAtIndex(int index) 
  {
     if (index == 0) {
           Node q = head;
           head = q.getNextNode();
           numberOfElements--;
      }
      else if ((index > numberOfElements - 1) || (index < 0)) 
           System.out.println("Index out of bounds.");
      else {
        Node currNode = head;
        for (int i = 0; i < index; i++) {
            //Node p = currNode;
            System.out.println("At this payload " + currNode.getPayload());
            currNode = currNode.getNextNode();
            System.out.println("Now at this payload " + currNode.getPayload());
        }
        Node temp = currNode;
        System.out.println("Removing the node with payload " + temp.getPayload()); 
        currNode = temp.getPrevNode();
        currNode.setNextNode(temp.getNextNode());
        temp = null;
        numberOfElements--;
    }

  }

  public void add(int num) {
      Node newNode = new Node(num);
      newNode.setNextNode(null);
      if (numberOfElements == 0) {
          newNode.setPrevNode(null);
          head = newNode;
          tail = newNode;
        }
      else if (numberOfElements == 1) {
          head.setNextNode(newNode);
          tail = newNode;
          tail.setPrevNode(head);
        }
      else {
        newNode.setPrevNode(tail);
        tail.setNextNode(newNode);
        tail = newNode;
      }
      System.out.println("Inserted " + num + " into the linked list");
     numberOfElements++;
    }

  public void printList() {
      Node temp = head;
      while (temp != null) {
          System.out.println("Node with payload " + temp.getPayload());
          temp = temp.getNextNode();
        }
    }


  }

Node 节点

public class Node {
    private int payload;
    private Node next;
    private Node prev;

    public Node(int payload) {
        this.payload = payload;
        prev = null;
        next = null;
    }

    public Node getNextNode() {
        return next;
    }

    public Node getPrevNode() {
        return prev;
    }

    public void setNextNode(Node n) {
        next = n;
    }

    public void setPrevNode(Node n) {
        prev = n;
    }

    public int getPayload() {
        return payload;
    }

}

Driver 司机

public class Driver {

      public static void main(String[] args) {
          LinkedList ll = new LinkedList();
          ll.add(7);
          ll.add(5);
          ll.add(13);
          ll.add(27);
          ll.add(2);
          ll.printList();
          ll.removeAtIndex(3);
          ll.printList();
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM