定义双链表,实现扑克三带二发牌排序

2019-12-18  本文已影响0人  vpractical

[TOC]

结果展示

方块1; 黑桃1; 梅花3; 红桃4; 梅花4; 红桃5; 方块5; 梅花6; 方块7; 红桃8; 黑桃9; 梅花9; 红桃11; 梅花12; 红桃12; 梅花13; 红桃13;

梅花1; 红桃1; 梅花2; 黑桃4; 方块4; 黑桃5; 梅花5; 方块6; 红桃6; 梅花7; 黑桃8; 方块9; 红桃10; 方块10; 黑桃11; 梅花11; 方块11;

方块2; 红桃2; 黑桃2; 方块3; 黑桃3; 黑桃6; 黑桃7; 红桃7; 方块8; 梅花8; 红桃9; 黑桃10; 梅花10; 方块12; 黑桃12; 方块13; 黑桃13; 

双链表实现

理解

1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
3.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。
这一点要看实际情况的。若只对单条数据插入或删除,ArrayList的速度反而优于LinkedList.但若是批量随机的插入删除数据,LinkedList的速度大大优于ArrayList.因为ArrayList每插入一条数据,要移动插入点及之后的所有数据。

实现

    private static class Node<E> {
        E it;
        Node<E> prev;
        Node<E> next;

        public Node(Node<E> prev, E it, Node<E> next) {
            this.it = it;
            this.prev = prev;
            this.next = next;
        }
    }
    private int size;
    private Node<E> first;
    private Node<E> last;
    public void add(E it) {
        Node<E> node = new Node<>(last, it, null);
        if (last != null) {
            last.next = node;
        }
        last = node;
        if (first == null) {
            first = node;
        }
        size++;
    }
    
    public void add(int index,E it){
        //找到第index位置的节点,修改前驱后继引用
    }
    

扑克发牌实现

/**
 * 三带二服务器
 */
public class Server {
    //所有的牌
    private static List<Poker> list = new ArrayList<>();
    //3个牌手的牌
    private static LinkedList<LinkedList<Poker>> roles = new LinkedList<>();

    static {
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 13; j++) {
                list.add(new Poker(i,j));
            }
        }

        for (int i = 0; i < 54; i++) {
            if(list.get(i).num == 3){
                list.remove(i);
                break;
            }
        }

        for (int i = 0; i < 4; i++) {
            roles.add(new LinkedList<Poker>());
        }
    }


    /**
     * 洗牌分牌
     */
    public static void init(){

        //随机取出发牌
        int index = 0;
        while (list.size() > 0){
            int random = (int) (Math.random() * list.size());
            Poker poker = list.remove(random);
            roles.get(index).add(poker);
            index++;
            if(index >= 3){
                index = 0;
            }
        }
    }

    /**
     * 牌手拿牌 :1  2  3
     * @param index
     */
    public static LinkedList<Poker> handOut(int index){
        return roles.get(index - 1);
    }
}

public class Poker {

    /**
     * type = 红桃,方块,黑桃,梅花 = 1.2.3.4
     * num = 1-13
     */
    public int type,num;

    public Poker(int type, int num){
        this.type = type;
        this.num = num;
    }

    @Override
    public String toString() {
        return name() + num + "; ";
    }

    public String name(){
        switch (type){
            case 1:
                return "红桃";
            case 2:
                return "方块";
            case 3:
                return "黑桃";
            case 4:
                return "梅花";
        }
        return "";
    }
}
    Server.init();
    
    LinkedList<Poker> list =  Server.handOut(1);
    sort(list);
    System.out.println(list.toString());
    public static void sort(LinkedList<Poker> list){
        LinkedList<Poker>[] linkArr = new LinkedList[13];
        for (int i = 0; i < 13; i++) {
            linkArr[i] = new LinkedList<>();
        }

        for (int i = 0; i < list.size(); i++) {
            Poker poker = list.get(i);
            linkArr[poker.num - 1].add(poker);
        }

        list.clear();
        for (LinkedList l:linkArr) {
            list.addAll(l);
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读