【问题标题】:Queue reverse implementation - code not working队列反向实现 - 代码不起作用
【发布时间】:2018-08-10 15:23:43
【问题描述】:

我是编程新手,我尝试实现一种算法,该算法使用链表和堆栈反转队列中的 K 个元素,但由于某种原因,我无法执行该算法,非常感谢任何帮助。

我发现队列是空的,尽管我向它添加了值并且我在控制台中看到了添加的值,但是当我检查 queue.isEmpty() 时,它是真实的。

这是我的代码

public class QueueReverseK {

    public void reverse(QueueADT queue, int k) {

        if (queue.isEmpty()) {
            System.out.println("in if empty");
            return;

        }

        if (k > queue.size()) {
            k = queue.size;
        } else if (k < 0) {
            k = 0;
        }
        StackADT tempStack = new StackADT(k);
        QueueADT newQueue = new QueueADT();
        for (int i = 0; i < k; i++) {
            tempStack.push(queue.deQueue().getData());
        }
        while (!tempStack.isEmpty()) {
            newQueue.enQueue(tempStack.pop());
        }
        while (!queue.isEmpty()) {
            newQueue.enQueue(queue.deQueue().getData());
        }
        queue = newQueue;

    }

队列类

   public class Queue {    
        LinkedList items;
        int size;
        Node head;
        Node tail;
        LinkedListADT list = new LinkedListADT();
        public Queue() {
            items = new LinkedList();
            size = 0;
            head = null;
            tail = null;
        }

        public int size() {
            return size;
        }
        public boolean isEmpty() {
            if (head == null) return true;
            else return false;

        }
        public void enQueue(int i) {
            items.addHead(i);

        }

        public Node deQueue() {
            return items.deleteHead();
        }


        public void printQueue() {
            items.printList();
        }



    }

链表类

        public class LinkedList {

        Node head;
        Node tail;

        LinkedList() {
            head = null;
            tail = null;
        }

        public void addHead(int val) {
            Node n = new Node(val);
            if (head == null) {
                head = n;
                tail = n;
            } else {
                Node tempNode = head;
                while (tempNode.next != null) {
                    tempNode = tempNode.next;
                }
                tempNode.next = n;

            }
        }

        public void addTail(int val) {
            Node n = new Node(val);
            if (head == null) {
                head = n;
                tail = n;
            } else {
                tail.next = n;
                tail = n;
            }
        }

        public int deleteTail() {
            Node n = tail;
            if (head == null) {
                return -1;
            } else if (head == tail) {
                head = null;
                tail = null;
            } else {
                Node cur = head;
                while (cur.getNext() != tail)
                    cur = cur.next;
                tail = cur;
                tail.next = null;
            }
            return n.getData();
        }

        public Node deleteHead() {
            Node n = head;

            head = head.next;

            return n;
        }

        public int count() {
            int size = 0;
            Node n = head;
            while (n != null) {
                n = n.getNext();
                size++;
            }
            return size;
        }

        public Node getHead() {
            return head;
        }

        public void setHead(Node head) {
            this.head = head;
        }

        public Node getTail() {
            return tail;
        }

        public void setTail(Node tail) {
            this.tail = tail;
        }

        public void printList() {
            if (this.head == null) {
                return;
            }
            // print all nodes
            Node tempNode = this.head;
            while (tempNode != null) {
                System.out.print(tempNode.data + "->");
                tempNode = tempNode.next;
            }
        }

        void printMiddle(int n) {
            Node slow_ptr = head;
            Node fast_ptr = head;
            if (head != null) {
                while (fast_ptr != null && fast_ptr.next != null) {
                    fast_ptr = fast_ptr.next.next;
                    slow_ptr = slow_ptr.next;
                }
                System.out.print(slow_ptr.data + " ");
                for (int i = 1; i <= n && slow_ptr != null; i++) {
                    slow_ptr = slow_ptr.next;
                    System.out.print(slow_ptr.data + " ");
                }
            }
        }
    }

主类

      public static void main(String[] args) {
        Queue Q = new Queue();

        Q.enQueue(1);
        Q.enQueue(2);
        Q.enQueue(3);
        Q.enQueue(4);
        QueueReverseK k = new QueueReverseK();
        k.reverse(Q, 2)
        Q.printQueue();

    }

堆栈类

    public class Stack {

        private int top;
        private int items[];
        private int max;

        public StackADT(int n) {
            this.max = n;
            top = 0;
            items = new int[n];
        }

        public boolean isEmpty() {
            return top == 0;
        }

        public boolean isFull() {
            return top == max;
        }

        public void push(int item) {
            if (isFull()) throw new IllegalArgumentException();
            else items[top++] = item;
        }

        public int pop() {
            if (isEmpty()) throw new IllegalArgumentException();
            else return items[--top];
        }

        public int size() {
            return top;
        }
    }
}

示例

输入队列:12 34 65 76 23 12 36 90 输出队列:12 34 65 76 90 36 12 23

【问题讨论】:

标签: java data-structures linked-list stack queue


【解决方案1】:

我并不完全肯定,但看起来您的队列就像一个堆栈。你的 enQueue 是推到队列的头部,而不是队列的尾部。

【讨论】:

  • 我在调试后发现,由于某种原因,head 每次都返回 null,这就是我猜的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多