删除链接列表

时间:2016-12-27 08:52:37

标签: java linked-list position

节点

private Object data;
private Node link;
private Node next;
private Node prev;

public Object getData() {
    return data;
}

public void setData(Object data) {
    this.data = data;
}

public Node getLink() {
    return link;
}

public void setLink(Node link) {
    this.link = link;
}

public Node(Object data) {
    this.data = data;
    this.link = 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;
}

项目

private int id;
private String name;
private String type;
private double price;

public Item(int id, String name, String type, double price) {
    this.id = id;
    this.name = name;
    this.type = type;
    this.price = price;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public String getType() {
    return type;
}

public double getPrice() {
    return price;
}

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setType(String type) {
    this.type = type;
}

public void setPrice(double price) {
    this.price = price;
}

@Override
public String toString() {
    return "Item: " + "ID: " + id + ", Name: " + name + ", Type: " + type + ", Price: " + price;
}

链表

private Node head;  // first node in the linked list
private int count;

public int getCount() {
    return count;
}

public Node getHead() {
    return head;
}

public LinkedList() {
    head = null;    // creates an empty linked list
    count = 0;
}

public void addFront(int n) {
    Node newNode = new Node(n);

    newNode.setLink(head);
    head = newNode;

    count++;
}

public void deleteFront() {
    if (count > 0) {
        head = head.getLink();
        count--;
    }
}

public void AddItemToFront(Item p) {

    Node newNode = new Node(p);
    newNode.setLink(head);
    head = newNode;

    count++;
}

public void DisplayItems() {
    Node temp = head;
    while(temp != null) {
        System.out.println(temp.getData());
        temp = temp.getLink();
    }
}

public void RemoveItemAtPosition(int n) {
    if(n == 1) {
        Node x = head;
        head = x.getLink();
        count--;
    }
    else if (n > count || n < 0) {
        System.out.println("The index you entered is out of bound.");
    }
    else {
     Node x = head;
     for (int i = 1; i < n; i++) {
        x = x.getNextNode();
        }

     Node temp = x;
     x = temp.getPrevNode();
     x.setNextNode(temp.getNextNode());
     temp = null;
     count--;
    }
}

我正在尝试在给定整数n的位置删除节点。 我在发布之前尝试过研究SO,上面是我发布的代码。但是,代码返回了一个错误,在主链上的LinkedList.java:74处有&gt; java.lang.NullPointerException:

节点实际上是一个被添加到LinkedList

的对象

2 个答案:

答案 0 :(得分:0)

我检查了你的代码我发现你有一些错误。

你的错误是:

1 - 您使用链接对象。

2-尝试在没有初始化的情况下访问上一个和下一个。

3-你只关心下一个节点。

我删除了链接表单Node类,并对LinkedList进行了一些更改:

 public class LinkedList {
    private Node head;  // first node in the linked list
    private int count;

    public int getCount() {
        return count;
    }

    public Node getHead() {
        return head;
    }

    public LinkedList() {
        head = null;    // creates an empty linked list
        count = 0;
    }

    public void addFront(int n) {
        Node newNode = new Node(n);

        if (head == null) {
            head = newNode;
        } else {
            Node node = head;
            head = newNode;
            head.setNextNode(node);
            node.setPrevNode(head);
        }

        count++;
    }

    public void deleteFront() {
        if (count > 0) {
            head = head.getNextNode();
            head.setPrevNode(null);
            count--;
        }
    }

    public void AddItemToFront(Item p) {
        Node newNode = new Node(p);

        if (head == null) {
            head = newNode;
        } else {
            Node node = head;
            head = newNode;
            head.setNextNode(node);
            node.setPrevNode(head);
        }

        count++;
    }

    public void DisplayItems() {
        Node temp = head;
        while (temp != null) {
            System.out.println(temp.getData());
            temp = temp.getNextNode();
        }
    }

    public void RemoveItemAtPosition(int n) {
        if (n == 1) {
            deleteFront();
        } else if (n > count || n < 0) {
            System.out.println("The index you entered is out of bound.");
        } else {
            Node x = head;
            for (int i = 1; i < n; i++) {
                x = x.getNextNode();
            }

            Node temp = x;
            temp.getPrevNode().setNextNode(temp.getNextNode());
            temp.getNextNode().setPrevNode(temp.getPrevNode());
            temp = null;
            count--;
        }
     }
   }

答案 1 :(得分:0)

检查提供的源代码时,在函数AddItemToFront(Item p)中,仅使用newNode.setLink(head);管理链接列表。 Node next;Node prev;从未初始化,之前从未在函数removeItemAtPosition(int n)中使用过。

  

警告:您的Linked-List是反向管理的(由于函数void AddItemToFront(Item p))。

解决问题的一种简单方法是在Node link;函数中也只使用removeItemAtPosition(int n)

第1步 - 在RemoveItemAtPosition(int n)中,修改for-loop中第n个节点的搜索

 Node x = head;
 for (int i = 1; i < n; i++) {
    x = x.getLink(); // Use Node link
    }

而不是

 Node x = head;
 for (int i = 1; i < n; i++) {
    x = x.getNextNode();
    }

第2步 - 在RemoveItemAtPosition(int n)中,将下一个节点链接连接到之前的节点

 Node temp = x.getLink();
 x.setLink(temp.getLink());
 count--;

而不是

 Node temp = x;
 x = temp.getPrevNode();
 x.setNextNode(temp.getNextNode());
 temp = null;
 count--;