0

I'm new to programming and trying to implement an algorithm which reverse K elements in a queue using linkedlist and stack but for some reason, I'm not able to execute the algorithm, any help is greatly appreciated.

I found queue is coming as empty eventhough I add values to it & I see added values in console but when I checked queue.isEmpty() is coming as true.

Here is my code

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;

    }

Queue class

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



    }

LinkedList Class

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

Main Class

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

    }

Stack Class

    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;
        }
    }
}

Example

input Queue: 12 34 65 76 23 12 36 90 output Queue : 12 34 65 76 90 36 12 23

  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Andy Turner Aug 10 '18 at 15:24
  • I recommend editing your question to provide more information - such as specifying where your work starts to fail - and also to fix the formatting (because its a bit broken). – ImmortaleVBR Aug 10 '18 at 16:22

1 Answers1

0

I'm not entirely positive, but it looks like your queue is acting like a stack. You're enQueue is pushing to the head, not the tail of the queue.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • what I found after debugging is head is returning null every time for some reason and it is creating the issue I guess – Nani Reddy Aug 10 '18 at 16:22