Minchae Minchae - 4 months ago 72
Java Question

Create one doubly linked list to represent all the 52 cards in a deck

I am trying to create a program that uses a doubly linked list to represent all the 52 cards in a deck. How do I do that?
I am supposed to create the classes and methods and can not use the existing java classes.

Here is my Doubly Linked List

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("");
}

}


Here is my Node

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 + " " );}

}


Here is my main class

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){

}
}

}

}


How do I create the deck of cards making use of only doubly LinkedList and not arraylist?

Answer

You may change the constructor of DoublyLinkedList to something like this:

/**
 * 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
}

If it is not clear, don't hesitate to ask.