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

时间:2014-02-19 05:16:49

标签: java 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--;
    }




  }

节点 -

 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;


}




  }

司机 -

    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));
          }


       }    
   }

我得到了删除头部索引所需的内容,但如何做其他事情超出了我的范围。 我有删除头部和尾部的方法现在我只需要知道如何在索引处删除就像我要在索引处添加一样。

1 个答案:

答案 0 :(得分:2)

好的,完全编辑这个答案。我拿了你的代码然后离开了。您的Node类存在一些问题,请查看我的。我创建了一个准系统Node类,添加了您可能需要/想要的任何其他功能。对于LinkedList类,我创建了一个简单的add方法,它添加到列表的末尾。将此作为其他添加的原型。现在一切都正常,我放了一些println来帮助你看看程序在哪里。我强烈建议使用调试器。另请注意,这是一个双重链接列表。

链接列表

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();
        }
    }


  }

节点

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;
    }

}

驱动程序

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();
      }
}