我正在尝试创建一个程序,该程序使用双向链表来表示一副牌中的所有52张牌。我怎么做? 我应该创建类和方法,不能使用现有的java类。
这是我的双重链接列表
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 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 + " " );}
}
这是我的主要课程
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){
}
}
}
}
如何创建仅使用双重LinkedList而不是arraylist的牌组?
答案 0 :(得分: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
}
如果不清楚,请不要犹豫。
答案 1 :(得分:0)
很好地使用泛型。帮助自己保持一致:永远不要只使用Node
,始终Node<T>
。
您可能需要声明持有卡片套装和价值的卡片类。您也可以使用字符串作为卡片,如下所示:
for (String suit : suits) {
for (String value : cardValues) {
dll.insertFirst(value + " of " + suit);
}
}
让我知道它是否有帮助。