1

I want to reverse the order of my output using stack and queues in Java.

Here is my code:

import java.util.*;

public class Books {
    public static void main(String[] args) {
        Queue<String> book = new LinkedList<String>();
        Stack<String> Title = new Stack<>();
        Scanner user = new Scanner(System.in);

        System.out.println("Enter four book titles.");
        int b = 4;
        for (int i = 1; i <= b; i++) {
            System.out.print("Book " + i + ": ");
            String Enter = user.nextLine();
            book.offer(Enter);
        }

        System.out.println("New order of books:");
        System.out.println(book);

    }
}

Here is the output of this code.

Enter four book titles.
Book 1: wew1
Book 2: wew2
Book 3: wew3
Book 4: wew4
New order of books:
[wew1, wew2, wew3, wew4]

What I was trying to is to make it in reverse order. But I don't know what to do.

0xh3xa
  • 4,801
  • 2
  • 14
  • 28
  • this question isn't related to stacks and queues I think. You can use simple arraylist for that – Tugay Nov 10 '20 at 14:53

2 Answers2

1

You can try to add the book in the Queue and add them to Stack then printing using pop() operation.

It's recommended to use ArrayDeque implementation when you want to use a Stack

    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        Scanner user = new Scanner(System.in);

        System.out.println("Enter four book titles.");
        int b = 4;
        for (int i = 1; i <= b; i++) {
            System.out.print("Book " + i + ": ");
            String Enter = user.nextLine();
            queue.add(Enter);
        }

        ArrayDeque<String> stack = new ArrayDeque<>();
        while (!queue.isEmpty()) {
            stack.push(queue.poll());
        }

        System.out.println("New order of books:");
        while (!stack.isEmpty()) {
            System.out.print(stack.pop()+" ");
        }
        user.close();
    }

, output

Enter four book titles.
Book 1: wew1
Book 2: wew2
Book 3: wew3
Book 4: wew4
New order of books:
wew4 wew3 wew2 wew1
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
1

Use the advantage that LinkedList implements Deque (double-ended queue). Note that Deque also implements Queue (single-ended queue).

You can use the method Deque#offerFirst(E e) to insert the book at the beginning of the queue. There is no need of reversing such queue afterward. Minimal example:

Deque<String> queue = new LinkedList<>();
        
for (int i = 1; i <= 4; i++){
    queue.offerFirst("book" + i);
}

System.out.println("Reversed order of books:");
System.out.println(queue);

Output:

Reversed order of books:
[book4, book3, book2, book1]

Note 1: Consider using ArrayDeque instad of LinkedList as long as you don't need to access by an index.

Note 2: Don't use Stack as long as it is considered obsolete and deprecated.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • The task that was given to use says "create a java stack consisting of (4) book titles. Pop all the elements one by one, each popped will be added to a queue". Is that even possible? – Jiggy Palconit Nov 10 '20 at 15:50
  • 2
    What you say right now is very different from what you describe in the question. – Nikolas Charalambidis Nov 10 '20 at 15:59
  • What do you mean? Its what it says in the instructions, we followed what was the instructions in the task but ended up a different expected output. The expected output was something reversed since the stack is getting pop one by one. – Jiggy Palconit Nov 10 '20 at 16:03
  • *" it says in the instructions"* - what instructions? There are no further instructios in the question. All the question states is *"I want to reverse the order of my output using stack and queues in java"*. There is no point in doint it in such way you describe unless it would be a school task as an exercise. In that case, you HAVE to provide such information or else don't expect such answer to appear. – Nikolas Charalambidis Nov 10 '20 at 16:05