索引前的单链接列表插入

时间:2016-06-16 03:46:22

标签: java linked-list singly-linked-list

我有两个类,DataStructure和LinkedList。我的DataStructure类负责链接列表的插入/删除/替换/更新/显示。我在索引元素之前在链接列表中插入元素时遇到问题。当我调试代码并查看LinkedList类时,很明显链接断开并且不连续。我不确定代码中有什么问题,因为它看起来逻辑合理。如果有人能够看到这个并引导我朝着正确的方向前进,我会非常感激。

这只是整个项目的片段,所以请随意更改类型

DataStructure类:

public class DataStructure {
    //Item LinkedList field
    private LinkedList<Item> itemLinkedList;

    //Constructor
    public DataStructure() {
        itemLinkedList = new LinkedList<>();
    }

    //Add Item
    public boolean addItem(Item item) {
        boolean returnVal = false;
        if (item != null) {
            itemLinkedList.Move(2);
            itemLinkedList.Add(item);
            returnVal = true;
        }
        return returnVal;
    }
}

LinkedList类

public class LinkedList<T> {
    //Fields
    private Node<T> Head;
    private Node<T> Current;
    private Node<T> Tail;
    private int size;

    //Constructor
    public LinkedList() {

    }

    //Methods
    private void AddHead(T data) {
        Node<T> temp = new Node<>(data);
        Tail = Current = Head = temp;
    }

    public void Add(T data) {
        if (data == null) throw new NullPointerException();
        else {
            Node<T> temp = new Node<>(data);
            if (Head == null) AddHead(data);
            else {
                if (Current == Tail) {
                    Tail.setNext(temp);
                    Current = Tail = temp;
                } else if (Current == Head) {
                    temp.setNext(Head);
                    Current = Head = temp;
                } else {
                    Node<T> cu = Current;
                    Current = temp;
                    Current.setNext(cu);
                }
            }
            size++;
        }
    }

    public Node MoveNext() {
        if (Current.getNext() != null)
            return Current = Current.Next;
        else return Current;
    }

    public void MoveLast() {
        if (Tail != null)
            Current = Tail;
    }

    public void MoveFirst() {
        if (Head != null)
            Current = Head;

    }

    public void Move(int index) {
        if (index >= size) MoveLast();
        else {
            MoveFirst();
            for (int i = 0; i < index; i++) MoveNext();
        }
    }

    class Node<T> {
        private T data;
        private Node<T> Next;

        public Node(T data, Node<T> next) {
            this.data = data;
            this.Next = next;
        }

        public Node(T data) {
            this.data = data;
        }

        public T getData() {
            return data;
        }

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

        public Node<T> getNext() {
            return Next;
        }

        public void setNext(Node<T> next) {
            Next = next;
        }

        public boolean hasNext() {
            return Next != null;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我观察到LinkedList类中的Add()方法存在问题。以下是修改后的版本:请尝试一次。 还有一件事,在Move(int index)方法中,循环应该运行(索引-1)次,否则你将无法链接前一个链接。

因此,请尝试使用以下修改后的方法,并告诉我问题是否仍未解决:

  public void Move(int index) {
        if (index >= size) MoveLast();
        else {
            MoveFirst();
            for (int i = 0; i < index-1; i++) MoveNext();  //Run loop until (index - 1) so that you can have reference at the node no. 2 (in case of index 2)
        }
    }

 public void Add(T data) {
        if (data == null) throw new NullPointerException();
        else {
            Node<T> temp = new Node<>(data);
            if (Head == null) AddHead(data);
            else {
                if (Current == Tail) {
                    Tail.setNext(temp);
                    Current = Tail = temp;
                } else if (Current == Head) {
                    temp.setNext(Head);
                    Current = Head = temp;
                } else {
                    Node<T> cu = Current.getNext();
                    Current.setNext(temp);
                    temp.setNext(cu);
                    Current = cu; //Recently added node
                }
            }
            size++;
        }
    }