Получить изначальную последовательность чисел, зная результат работы алгоритма

Есть стопка карт с числами, которая раскладывается следующим образом: первая карта кладётся на стол, вторая под низ, третья на стол, четвёртая под низ и т.д. Зная результат этой раскладки, нужно определить начальную последовательность, используя очередь.

Пример: 3, 8, 23, 19, 2, 7, 17, 13, 5, 11

Начальная последовательность: 3, 7, 8, 11, 23, 17, 19, 5, 2, 13

Мой класс с очередью:

public class SimpleLinkedListQueue {

    private static class Node {
        private int value;
        private Node next;


        public Node(int value) {
            this(value, null);
        }

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    private Node head = null;  // first, top
    private Node tail = null;  // last
    private int count = 0;

    public void add(int value) {
        if (count == 0) {
            head = tail = new Node(value);
        } else {
            tail.next = new Node(value);
            tail = tail.next;
        }
        count++;
    }

    public int count() {
        return count;
    }

    public boolean empty() {
        return count() == 0;
    }

    public int element() throws Exception {
        if (count() == 0) {
            throw new Exception("Queue is empty");
        }
        return head.value;
    }

    public int remove() throws Exception {
        int result = element();
        head = head.next;
        if (count == 1) {
            tail = null;
        }
        count--;
        return result;
    }

    public String toString(){
        Node current = head;
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        while (current != null){
            if (current.next != null) {
                sb.append(current.getValue()).append("; ");
            } else {
                sb.append(current.getValue());
            }
            current = current.getNext();
        }
        sb.append("]");
        return sb.toString();
    }

Метод, который раскладывает стопку по правилам:

public SimpleLinkedListQueue shuffle(SimpleLinkedListQueue deck) throws Exception{
        Node current = deck.head;
        SimpleLinkedListQueue newDeck = new SimpleLinkedListQueue();

        while (deck.count() > 0 && current != null) {
            int topCard = current.getValue();
            newDeck.add(topCard);
            deck.remove();
            current = current.next;

            if (deck.count() != 0) {
                int secondCard = current.getValue();
                deck.remove();
                deck.add(secondCard);
                current = current.next;
            }
        }
        newDeck.add(deck.head.getValue());
        return newDeck;
    }

В подсказке сказано промоделировать алгоритм в обратном порядке, но у меня всё равно не получается получить начальную последовательность.

Какие у вас есть идеи?


Ответы (0 шт):