二重リンクリストを使用してデッキ内の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だけの?
なぜこのようにしますか?privateノード nodes [] =新しいノード[cardValues.length * suits.length]; '? –
Minchae
すべてのノード(すべてのカード)を保持する配列を作成します。アレイのサイズは52 - 4スーツ* 13枚です。 (プライベートノード nodes [] = new Node [52];に置き換えることができます) –
c0der
明確にするコメントと「次の」ノードへのリンクがありません。 – c0der