0

结点类别我的代码有什么问题? (链表的Java /洗牌)

public class NodeDouble{ 
    private int card; 
    NodeDouble next; 
    NodeDouble prev; 

    public NodeDouble(int card){ 
     this.card = card; 
    } 

    public int getCard(){ 
     return this.card; 
    } 

    public NodeDouble getNext(){ 
     return this.next; 
    } 

    public NodeDouble getPrev(){ 
     return this.prev; 
    } 

    public void displayNode(){ 
     System.out.println("Card: "+card); 
    } 
} 

LinkedList类

import java.util.Random; 

public class CardListDouble{ 

private NodeDouble head; 
private NodeDouble tail; 
private int size; 

//constructors 
public CardListDouble(){ 
    head = null; 
    tail = null; 
    size = 0; 
} 
public CardListDouble(int numberOfCards){ 
    for(int i = numberOfCards; i>=1; i--){ 
     this.insertFirst(i); 
    } 
    size = numberOfCards; 
} 
//methods 
public void insert(int card, int index){ 
    if(index < 1 || index > size+1){throw new IllegalArgumentException("index is smaller than 1!, or larger than size!");} 
    else if(index == 1){//add at beginning 
     insertFirst(card); 
    } 
    else if(index == size+1){//add to the end 
     NodeDouble temp = new NodeDouble(card); 

     tail.next = temp; 
     temp.prev = tail; 
     tail = temp; 
    }else{//add to the 'middle' somewhere 
     NodeDouble temp = new NodeDouble(card); 
     NodeDouble indexmin1 = getNode(index-1); 
     NodeDouble indexcurrent = getNode(index); 

     indexmin1.next = temp; 
     temp.next = indexcurrent; 
     indexcurrent.prev = temp; 
     temp.prev = indexmin1; 
    }size++; 
} 

public void insertFirst(int card){ 
    NodeDouble temp = new NodeDouble(card); 

    if(isEmpty()){ 
     head = temp; 
     tail = temp; 
    }else{ 
     temp.next = head; 
     head.prev = temp; 
     head = temp; 
    }size++; 
} 

public int removeCard(int card){ 
    if(card > size){throw new IllegalArgumentException("card not here!");} 
    if(head == null){System.out.println("cannot delete from empty list");} 
    else{ 
     NodeDouble current = head; 
     NodeDouble oneBehind = null; 
     boolean found = false; 
     int fua = 0; 
     //traverse list to find which node/card to delete 
     while(found == false && fua < size){ 
      fua++; 
      if(current.getCard() == card){ 
       found = true; 
       break; 
      }else{ 
       current = current.next; 
      } 

     } 
     //did not find node 
     if(current == null){System.out.println("Card not in list!");} 
     //did find node 
     else{ 
      int tempCard = current.getCard(); 
      if(head == current){//case1; card is at head 
        head = head.next; 
        head.prev = null; 
       } 
       else if(current == tail){//case2; card is tail 
        tail = tail.prev; 
        tail.next = null; 
       } 
       else{//card is in the 'middle' somewhere 
       current.prev.next = current.next; 
       current.next.prev = current.prev; 
       current.next = null; 
       current.prev = null; 
       }size--;return tempCard; 
      } 
     }return 0;//did not find card so return 0; 
    } 




public NodeDouble getNode(int index){ 
    if(index <=0){throw new IllegalArgumentException("index < 1 yo! (getnodemethod)");} 
    else if(index > size){throw new IllegalArgumentException("index is bigger than size!");} 
    else if(index < (size/2)){//traverse from right 
     NodeDouble current = head; 
     for (int i = 1; i < index; i++) { 
      current = current.next; 
     }return current; 
    }else if(index >= (size/2)){//traverse from left 
     NodeDouble current = tail; 
     for (int i = size; i > index; i--) { 
      current = current.prev; 
     }return current; 
    }else{ 
     System.out.println("list is empty or index is smaller than 0!"); 
     return null; 
    } 
} 

public void displayList(){ 
    if(isEmpty()){ 
     System.out.println("Empty List!"); 
    }else{ 
     NodeDouble current = head; 

     while(current != null){ 
      current.displayNode(); 
      current = current.next; 
     } 
    }System.out.println("size of deck:" + size); 
} 
public boolean isEmpty(){ 
    return size == 0; 
} 

public void shuffle(){ 
    Random rng = new Random(); 
    for(int i = 1; i<size; i++){ 
     int temp = rng.nextInt(size) + 1; 
     System.out.println("i:" + i + " temp: " + temp); 
     removeCard(i); 
     insert(i,temp); 
    } 
    System.out.println("shuffling done!"); 
} 
} 

测试/ main方法

public class Test{ 

    public static void main(String[] args){ 
     CardListDouble list = new CardListDouble(52); 

     long startTime = System.currentTimeMillis(); 
     list.shuffle(); 
     list.displayList(); 
     long estimatedTime = System.currentTimeMillis() - startTime; 
     System.out.println("Time taken: " + estimatedTime); 
    } 

大家嗨。我正在制作一个双链表来包含一些卡片。 一切似乎工作正常(当我测试每个方法本身似乎工作)。 但是当我运行shuffle方法时,偶尔我会得到一个NullPointerException,我似乎无法找出问题所在。

在shuffle方法中,我只有一个for循环,它可以删除for循环所在的卡。然后它将它插入列表中的随机位置。

我是初学者,所以我希望你能理解我的代码。我添加了一些评论。

回答

0

在您的insert方法中,您的案例之一调用insertFirst并继续。发生这种情况时,insertFirst递增列表大小,然后insert再次递增列表大小。现在列表的大小太大,当它结束时,你会在remove中得到NullPointerException

解决此特定问题的一种方法是在insert致电insertFirst(card)后添加return

+0

谢谢老兄,解决了这个问题。你是我的英雄:D。我还可以问你用什么方法来发现问题吗? –

+0

我在循环中运行你的代码来复制问题。 shuffle中的代码会打印多少次它会循环超过大小,并且它已经超过了53次,所以很明显,大小增加了太多次。我研究了它可能改变的地方,并试图找到一条可能改变多次的路径。 –