2016-07-10 8 views
0

二重リンクリストを使用してデッキ内の52枚のカードをすべて表すプログラムを作成しようとしています。それ、どうやったら出来るの? クラスとメソッドを作成するはずですが、既存のJavaクラスは使用できません。ここでデッキ内の52枚のカードすべてを表す1つの二重リンクリストを作成します。

はここ

public class DoublyLinkedList<T> { 
    private Node<T> head; 
    private Node<T> rear; 

    public DoublyLinkedList(){ 
     head = null; 
     rear = null; 
    } 

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

    public void insertFirst (T dd){ 
     Node<T> newNode = new Node<T>(dd); 
     if (isEmpty()) 
      rear = newNode; 
     else 
      head.setPrevious(newNode); 

      newNode.setNext(head); 
      head = newNode; 
    } 

    //0. Only can use this to insert AFTER first node 
    public boolean insertAfter(int key, T dd){ 
     Node<T> current = head; 
     //1. traverse and check if key exisst and reference it using a node 
     while ((Integer) current.getData()!=key){ 
      current = current.getNext(); 
      if(current == null) 
       return false; //cannot find it 
     } 

     Node<T> newNode = new Node<T>(dd); //make new link 

     if (current == rear) //2. if key is the rear node, 
     { 
      newNode.setNext(null); 
      rear = newNode;  //3. Set reat to point to the new node 
     } 
     else //4. if key is not the rear link, 
     { 
      //5. handle the 2 link changes that is right hand side of the new node 
      newNode.setNext(current.getNext()); 
      current.getNext().setPrevious(newNode); 
     } 
     //6. handle the 2 link changes that is left hand sid of the node 
     newNode.setPrevious(current); 
     current.setNext(newNode); 
     return true; //found it, insert 
    } 

    public Node deleteFirst(){ 
     Node<T> temp = head; 
     if (head.getNext() == null) 
      rear = null; //to indicate empty DLL 
     else 
      head.getNext().setPrevious(null); 
     head = head.getNext(); 
     return temp; 
    } 

    public Node deleteLast(){ 
     Node<T> temp = rear; 
     if (head.getNext() == null) 
      head = null; //to indicate empty DLL 
     else 
      rear.getPrevious().setNext(null); 
     rear = rear.getPrevious(); 
     return temp; 
    } 

    public Node deleteKey(int key){ 
     Node<T> current = head; 
     while ((Integer) current.getData() != key){ 
      current = current.getNext(); 
      if (current == null) 
       return null; //cannot find it 
     } 
     if (current == head) //found it; head item? 
      head = current.getNext(); 
     else 
      current.getPrevious().setNext(current.getNext()); 

     if (current == rear) //rear item? 
      rear = current.getPrevious(); 
     else 
      //not rear 
      current.getNext().setPrevious(current.getPrevious()); 
     return current; //return value 
    } 

    public void displayForward(){ 
     System.out.print("List (head to rear): "); 
     Node<T> current = head; //start at beginning 
     while (current != null) //until end of list, 
     { 
      current.displayLink(); //display data 
      current = current.getNext(); //move to next link 
     } 
     System.out.println(""); 
    } 

    public void displayBackward(){ 
     System.out.print("List (Backwards) : "); 
     Node<T> current = rear; 
     while (current != null){ 
      current.displayLink(); 
      current = current.getPrevious(); 
     } 
     System.out.println(""); 
    } 

} 

は、私が利用したカードのデッキを作成するにはどうすれば私のメインクラス

public class Question { 

    public static void main(String[] args) { 
     Scanner sc = new Scanner(System.in); 
     int userChoice = 0; 
     DoublyLinkedList dll = new DoublyLinkedList(); 
     String[] suits = {"C", "D", "H", "S"}; //arraylist? not supposed to use that... 
     String[] cardValues = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; 

     int cardsInDeck = 52; 

     for (int i = 0; i < 10; i++){ 
      System.out.print("Enter a number. \nIf you want to pick a card from " 
        + "FRONT, enter 1. \n" 
        + "If you want to pick a card from BACK, enter 2.\n" 
        + "If you want to SWAP 4 PAIRS OF RANDOM CARD, enter 3.\n" 
        + "If you want to SPLIT THE DECK, enter 4.\n" 
        + "Number: "); 
      userChoice = sc.nextInt(); 

      if (userChoice == 1){ 

      } 
     } 

    } 

} 

ここに私のノード

public class Node <T> { 
private T data; 
    private Node next; 
    private Node previous;    // previous link in list 

    public T getData() { 
     return data; 
    } 

    public Node getNext() { 
     return next; 
    } 

    public Node getPrevious() { 
     return previous; 
    } 

    public void setData(T data) { 
     this.data = data; 
    } 
    public void setNext(Node next) { 
     this.next = next; 
    }  
    public void setPrevious(Node previous) { 
     this.previous = previous; 
    } 
    public Node(T data) { 
     this.data = data; 
     this.next = null; 
     this.previous = null; 
    } 
    public void displayLink(){ System.out.print(data + " ");} 

} 

である私の二重にリンクされたリストがされますarraylistではなく、二重のLinkedListだけの?

答えて

1

あなたはこのような何かにDoublyLinkedListのコンストラクタを変更することがあります。

/** 
* The class represents a full deck of cards. 
* Each card is represented by a Node. 
* It is initialized so each Node (card), accept the first, 
* is linked to its previous card, and each node but last, 
* is linked to its next card in the deck. 
*/ 
public class DoublyLinkedList<T> { 

    //13 cards per suit 
    String[] cardValues = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; 
    //4 suits 
    String[] suits = {"C", "D", "H", "S"}; 

    //an array to hold (references to) 4*13 Nodes. Each node represents a card 
    private Node<T> nodes[] = new Node[cardValues.length * suits.length]; 

    public DoublyLinkedList(){ 

     int cardCounter = 0; 

     //Iterate over all suits 
     for(String suit : suits) { 

      //Iterate over all cards 
      for(String card : cardValues) { 

       //create a Node representing a card 
       Node<T> node = new Node(suit+card); // substitute suit+card for "real" data 

       //define previous node to every node but first 
       if (cardCounter > 0) { 
        //link this node to previous one 
        node.setPrevious(nodes[cardCounter-1]); 

        //link previous node to this one 
        nodes[cardCounter-1].setNext(node); 
       } 
       //add node to array 
       nodes[cardCounter] = node; 

       cardCounter++; 
      } 
     } 
    } 

    public boolean isEmpty(){ 
     return nodes.length ==0; //no mode in array 
    } 

    //add more methods/functions as needed 
} 

それが明確でない場合は、お気軽にお問い合わせください。

+0

なぜこのようにしますか?privateノード nodes [] =新しいノード[cardValues.length * suits.length]; '? – Minchae

+0

すべてのノード(すべてのカード)を保持する配列を作成します。アレイのサイズは52 - 4スーツ* 13枚です。 (プライベートノード nodes [] = new Node [52];に置き換えることができます) – c0der

+0

明確にするコメントと「次の」ノードへのリンクがありません。 – c0der

0

ジェネリック薬をうまく使います。一貫性を保つことに賛成してください:Node、常にNode<T>を使用しないでください。

カードのスーツと値を保持するCardクラスを宣言することができます。また、単にこのように、カードの文字列を使用することがあります。

for (String suit : suits) { 
    for (String value : cardValues) { 
     dll.insertFirst(value + " of " + suit); 
    } 
} 

私はそれができますかどうかを知ってみましょう。

関連する問題