Question


Using additional non-array variables, order all elements on a queue using also two additional queues (Give pseudo-code or syn
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Description of Program:

  • I made use of a Queue and then added values to the queue manually in the program.
  • Later on, I created a List and then sorted the list in ascending order using the Collections.sort() method
  • After which I just printed out the existing values in the List.

main.java:

import java.util.*;

class Main
{
public static void main(String[] args) {
Queue<Integer> q = new PriorityQueue<Integer>();
q.add(21);
q.add(0);
q.add(-11);
q.add(14);
List<Integer> l = new ArrayList<Integer>();
l.addAll(q);
Collections.sort(l);
Iterator<Integer> i = l.iterator();
System.out.print("Queue in order is: ");
while (i.hasNext()) {
System.out.print(i.next()+" ");
}
}
}

Output:

Queue in order is: -11 0 14 21 |

Add a comment
Know the answer?
Add Answer to:
Using additional non-array variables, order all elements on a queue using also two additional queues (Give...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Java] Efficiency Comparison of two Implemented Queues : Circular Array Queue and SSQueue and its enqueue...

    [Java] Efficiency Comparison of two Implemented Queues : Circular Array Queue and SSQueue and its enqueue and dequeue operations Need to write a program that compares the efficiency of the queue implementations. To do so, you need to find and compare the running times of 1 the following two scenarios for both queue implementations: 3.1 Scenario 1: Alternating Sequence of Enqueues and Dequeues For every n ∈ {20, 50, 100, 1000, 10000, 100000, 1000000}, do the following: 1. long startTime...

  • Queues This programming exercise introduces the Queue data structure. The students must create all the necessary...

    Queues This programming exercise introduces the Queue data structure. The students must create all the necessary methods for the queue and use the queue in a Java program. Step 1 - Create a new project in Netbeans Use the following naming convention: “Module4_Lastname_Assignment1”. Step 2 - Build a solution You have been asked to create a customer service program for a new phone store that will be opening soon. Since this company anticipates being the agent for a rising new...

  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • Please help with this visual basic code. If they are, indicate in which order the array elements will be processed. Give...

    Please help with this visual basic code. If they are, indicate in which order the array elements will be processed. Give reasons for the ones that are not correct. For j As Integer = 1 To intMAXCOL strBoard(j,i) = "*" For j As Integer = 1 To intMAXCOL strBoard(j,i) = "*"

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • Code a repetition control structure that: Processes an array of integers in index order to sum...

    Code a repetition control structure that: Processes an array of integers in index order to sum its elements. Only odd numbers should be included in the sum and processing should cease when either a negative integer or (be careful) the end of the array is encountered. The array can be any length but you do not have to deal with an empty array. Declare all required variables and give them initial values if required. Display the output using myWindow.writeOutLine(…). You...

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • C++ -- Event processing simulation using a transaction queue Hi! it is queue simulation please read...

    C++ -- Event processing simulation using a transaction queue Hi! it is queue simulation please read the instructions, write codes, and explain the code with comments. Thank you Transactions enter the system and are stored in a queue. Each transaction represents some work that needs to be accomplished. Servers exist which process transactions. Servers take transactions off the queue and process them. you’re building the simulation framework. The idea is that somebody would take your framework, and add the specifics...

  • Write a method to reverse the order of elements on stack S using two additional stacks....

    Write a method to reverse the order of elements on stack S using two additional stacks. When the method completes, stack S should have the items it originally had, but in reversed order.

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT