如何使用remove方法删除链接列表中的第一个节点

时间:2014-04-01 02:30:44

标签: java nodes singly-linked-list

我想创建一个链表然后用1-100填充它。之后,我打印出所有偶数,而没有从列表中删除奇数,然后再次打印出偶数,但加倍。在这些之后,我从链表中删除奇数并打印出列表。我提到的最后一步是我被挂断了。其他一切正常,只是我的删除方法删除除了1之外的所有奇数。在我的main方法中,我使用if语句,说明如果节点%2中包含的数字等于零,则删除节点。它适用于除第一个节点之外的每个节点。谢谢你们给我任何帮助。我的代码如下。

import java.util.*;

/*
 * My ListNode class
 */
class ListNode<Integer> {
private Integer item;
private ListNode<Integer> next;

public ListNode(Integer item) {
    this.item = item;
    next = null;
}

public ListNode(Integer item, ListNode<Integer> next) {
    this.item = item;
    this.next = next;
}

public Integer getItem() {
    return item;
}

public ListNode<Integer> getNext() {
    return next;
}

public void setItem(Integer item) {
    this.item = item;
}

public void setNext(ListNode<Integer> next) {
    this.next = next;

}   
}

/*
 * My LinkedList class
 */
class LinkedList<Integer> {
public ListNode<Integer> front;

public LinkedList() {
    front = null;
}

public boolean isEmpty() {
    return front == null;
}

 public boolean contains(int target) {
     for (ListNode<Integer> node = front;
          node != null;
          node = node.getNext()) {
         if (node.getItem().equals(target)) {
             return true;
         }
     }
     return false;
 }

 public int size() {
     int count = 0;
     for (ListNode<Integer> node = front;
          node != null;
          node = node.getNext()) {
         count++;
     }
     return count;
 }

 public String toString() {
     String result = "( ";
     for (ListNode<Integer> node = front;
          node != null;
          node = node.getNext()) {
         result += node.getItem() + " ";
     }
     return result + ")";
 }

 public Integer get(int index) {
     ListNode<Integer> node = front;
     for (int i = 0; i < index; i++) {
         node = node.getNext();
     }
     return node.getItem();
 }

 public void set(int index, Integer target) {
     ListNode<Integer> node = front;
     for (int i = 0; i < index; i++) {
         node = node.getNext();
     }
     node.setItem(target);
 }

 public void add(int index, int target) {
     if (isEmpty()) {
         front = new ListNode(target);
     } else {
         ListNode last = front;
         while (last.getNext() != null) {
             last = last.getNext();
         }
         last.setNext(new ListNode(target));
     }
 }

 public Integer remove(int index) {
     ListNode<Integer> node = front;
     ListNode<Integer> prev = front;
     for (int i = 0; i < index; i++) {
         prev = node;
         node = node.getNext();
 }
     prev.setNext(node.getNext());
     return node.getItem();
}
 }
public class LinkedListTest {
//interface Removal {
    //Integer remove (Integer item);
//}
public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();

    System.out.println(list);

    System.out.println("The list size is " + list.size());      
    System.out.println();

    /*
     * This adds the numbers 1 through 100 to a LinkedList
     */
    for (int i = 1; i <= 100; i++)
        list.add(0, i);

    System.out.println(list);


    System.out.println("The list size is " + list.size());
    System.out.println();

    /*
     * This prints out only even numbers by excluding indexes that are even,
     * because all the even numbers are held in the odd numbered indexes, thus 
     * index 0 is 1 but index 1 is 2, index 3 is 4
     */
    for (int i = 0; i < list.size(); i++) 
        if (i % 2 == 1) {
            System.out.print(list.get(i) + " ");
        }

    System.out.println();

    System.out.println("The list size is " + list.size());
    System.out.println();

    /*
     * This doubles even numbers                    
     */
    for (int i = 0; i < list.size(); i++) 
        if (i % 2 == 1) {
            int result = list.get(i) * 2;
        System.out.print(result + " ");
        }
    System.out.println();
    System.out.println("The list size is " + list.size());
    System.out.println();

    for (int i = 0; i < list.size(); i++) 
            if (list.get(i) % 2 == 1) {
                list.remove(i);
            }

    System.out.print(list);
    System.out.println();
    System.out.println("The list size is " + list.size());
    System.out.println();
    /*
     * These contain methods only work for the first list created
     */
    System.out.println("Does the list contain 32? " + list.contains(32));

    System.out.println("Does the list contain 33? " + list.contains(33));

}   
}

1 个答案:

答案 0 :(得分:1)

remove的值为index时,0方法无法正常工作。因为nodeprev都已初始化为front并且循环未执行,因为i<index条件为false。因此,您必须为index=0的情况添加另一个条件 在remove方法的开头添加这些行可以解决问题..

if(index==0){
    ListNode<integer>temp=front;
    front=front.getNext();
    return temp.getItem();
}
希望它有所帮助...