我如何修复我的代码(singlylinkedlist),以便该函数添加适当的索引?

时间:2017-10-26 10:52:01

标签: java eclipse

我试图添加"你好"在索引0但它在索引1处添加,并且在此之后还打印美国时它不应该。我的添加方法有什么问题?代码中是否存在导致此问题的其他错误,或者只是添加方法?任何帮助表示赞赏......单一链接列表与实现的代码如下......

 class SinglyLinkedList<E> { //---------------- nested Node class
 private static class Node<E> { private E element;
 private Node<E> next;
 public Node(E e, Node<E> n) {
 element = e;
next = n; }
 public E getElement() {
 return element;
 }
 public Node<E> getNext() {
return next;
 }
 public void setNext(Node<E> n) {
next = n;
} }
 private Node<E> head = null;
 private Node<E> tail = null;
 private int size = 0;
 public SinglyLinkedList() { }
 public int size() { return size; }
 public boolean isEmpty() { return size == 0; }
 public E first() {
 if (isEmpty()) return null; return head.getElement();
 }
 public E last() {
 if (isEmpty()) return null;
 return tail.getElement(); }
 // update methods
 public void addFirst(E e) {
 head = new Node<>(e, head);
 if (size == 0)
 tail = head;
 size++;
 }
 public void addLast(E e) {
 Node<E> newest = new Node<>(e, null);
 if (isEmpty( ))
 head = newest;
 else
 tail.setNext(newest);
 tail = newest;
size++;
 }
 public E removeFirst() {
 if (isEmpty())
 return null;
 E answer = head.getElement();
 head = head.getNext();
 size--;
 if (size == 0)
 tail = null;
 return answer;
 }
 public void printLinkedList() {
     Node<E> temp = head;
     for(int i = 0; i< size; i++) {
         System.out.println(temp.getElement());
         temp = temp.next;
     }
 } 
 public void removeLast() {
     tail = null;
     size--;
 }


 public void remove(int index) {
 //Node<E> newest = new Node(e, null);
 Node<E> current = head;
 if (index == 0) {
 removeFirst();
 }
 else {
 for (int i = 0; i<index -1; i++) {
 //current = current.next;
 current = current.getNext();
}
 current.next = current.next.next;
 }
 size--;
}
 public void add(int index, E e) {
     Node<E> current = head;
     Node newNode = new Node(e, null);
        for(int i = 0; i< index; i++) {
            current = current.next;
        }
     current.next = newNode;
     newNode.next = current;
     size++;
 } 

 }
public class SinglyLinkedListTest {
    public static void main(String args[]) {
        SinglyLinkedList<String>  list = new SinglyLinkedList();
        list.addLast("USA");
        list.addLast("America");
        list.addLast("portugal");
        System.out.println(" ");
        list.printLinkedList();
        //list.remove(2);
        System.out.println(" ");
        list.printLinkedList();
        list.removeLast();
        System.out.println(" ");
        list.printLinkedList();
        list.add(0, "hello");
        System.out.println(" ");
        list.printLinkedList();
    }
}

1 个答案:

答案 0 :(得分:0)

有两个错误:

  1. 在索引0处您需要替换head
  2. current指向newNodenewNode指向current
  3. 固定代码:

    public void add(int index, E e) {
        Node<E> newNode = new Node<>(e, null);
        if(index == 0) {
            newNode.next = head;
            head = newNode;
        } else {
            Node<E> current = head;
            for(int i = 1; i< index; i++) {
                current = current.next;
            }
            Node<E> tmp = current.next;
            current.next = newNode;
            newNode.next = tmp;
        }
        size++;
    } 
    
相关问题