Question

Computer Science-Data Structures and Algorithms

What would be the contents of queue Q1 and Q2 after the following code is executed and the following data are entered?

1 Q1 = createQueue
2 Q2 = createQueue
3 loop (not end of file)
1 read number
2 enqueue (Q1,number)
3 enqueue (Q2,number)
4 loop (not empty Q1)
1 dequeue (Q1,x)
2 enqueue (Q2,x)
5 end loop
4 end loop

The data are 5, 7, 12, 4, 0, 4, 6
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Let's think about the algorithm.

We start with two empty queues, Q1 and Q2.

For each number n in the file

add n to Q1

add n to Q2

remove everything from Q1 and add it to Q2 (Q1 will only contain n)

Q1 is empty at the end of each iteration. 2 copies of n were added to Q2.

So Q2 contains 2 copies of every number in the file, in the order, they are read: 5, 5, 7, 7, 12, 12, 4, 4, 0, 0, 4, 4, 6, 6


answered by: gra
Add a comment
Know the answer?
Add Answer to:
Computer Science-Data Structures and Algorithms
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
  • Imagine that the contents of queue Q1 and queue Q2 are as shown. What would be...

    Imagine that the contents of queue Q1 and queue Q2 are as shown. What would be the contents of Q3 after the following code is executed? The queue contents are shown front (left) to rear (right). Q1: 42 30 41 31 19 20 25 14 10 11 12 15 Q2: 4 5 4 10 13 1 Q3 = createQueue 2 count = 0 3 loop (not empty Q1 and not empty Q2)      1 count = count + 1     ...

  • The variable queue below is a queue that holds integers. The queue is empty before the...

    The variable queue below is a queue that holds integers. The queue is empty before the code is executed. What is printed? queue. enqueue (1); queue. enqueue (2); queue. enqueue(3); System.out.println(queue. dequeue ()); N 1 none of these is correct The variable queue below is a queue that holds integers. The queue is empty before the code is executed. What is the contents of the queue after the code executes? queue. enqueue (1); queue. enqueue (2); queue. dequeue(); queue. enqueue...

  • Data Structures and Algorithm (Stacks and Queues) Imagine you have a Queue of integers, Q1 and...

    Data Structures and Algorithm (Stacks and Queues) Imagine you have a Queue of integers, Q1 and Q2. Draw a picture of Q1 and Q2 after the following operations: **Note: make sure that ALL variables are simulated.** Data are: 5,7,12,4,0,4,6 (these are the numbers) Algorithm: 1 loop (not end of the file) 1read number 2enqueue(Q1,number) 3enqueue(Q2,number) 4loop(not empty Q1) 1dequeue(Q1,x) 2enqueue(Q2,x) Send loop 2end loop

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...

    Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front = 0, back = 0; void enqueue(int x) { data[back] = x; back = (back + 1) % 6; } void dequeue() { front = (front + 1) % 6; } and we perform the following series of queue operations: enqueue(1); dequeue(); enqueue(2); dequeue(); enqueue(7); enqueue(3); enqueue(5); dequeue(); dequeue(); enqueue(4); enqueue(6); Write the state of the queue array after each operation, and at the end,...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a...

    (Data Strcture) Tool(s)/Software Java programming language with NetBeans IDE. Description Implementing a Linear Queue using a Singly Linked-List and Implementing a Priority Queue Tasks/Assignments(s) ■ Write your own program to implement priority queue using the Linked-List and implement the Enqueue, Dequeue and Display methods. implement the Merge methods in your main program that receive Q1 and Q2 and merge the two queues into one queue. Public static PriorityQueue MergeQueue(PriorityQueue Q1,PriorityQueue Q2) Write main program to test priority queue class and...

  • Using the below files, Write a program that reads a document containing endnotes indicated in this...

    Using the below files, Write a program that reads a document containing endnotes indicated in this manner, collects them in a queue, and prints them on the screen. For this lab, you will create a text file called sample.txt and put the following paragraph in it. This part is the beginning. {This part is the footnote.} This part is the end. /* Queue.h contains the declaration of class Queue. Basic operations: Constructor: Constructs an empty queue empty: Checks if a...

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • A. Starting with an initially empty stack, after 6 push operations, 3 pop operations, and 2...

    A. Starting with an initially empty stack, after 6 push operations, 3 pop operations, and 2 push operations, the number of elements in the stack would be: B. Starting with an initially empty queue, after 5 enqueue operations, 4 dequeue operations, and 6 enqueue operations, the number of elements in the queue would be:

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
Active Questions
ADVERTISEMENT