CSC 1052 Homework 4: Queue Concepts and Implementations - Prof. Catherine Helwig, Assignments of Data Structures and Algorithms

Multiple-choice questions related to the concepts and implementations of queues, including circular arrays, linked lists, and priority queues. Topics covered include the differences between queues and stacks, the order of removal, insertion in circular arrays, and the use of queues to simulate people waiting in a line.

Typology: Assignments

Pre 2010

Uploaded on 02/25/2010

koofers-user-urk
koofers-user-urk 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSC 1052 Homework 4 - Queue
Multiple Choice
1. One difference between a queue and a stack is:
oA. Queues require linked lists, but stacks do not.
oB. Stacks require linked lists, but queues do not.
oC. Queues use two ends of the structure; stacks use only one.
oD. Stacks use two ends of the structure, queues use only one.
o
2. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then
removed one at a time, in what order will they be removed?
oA. ABCD
oB. ABDC
oC. DCAB
oD. DCBA
3. Suppose we have a circular array implementation of the queue class, with ten items
in the queue stored at data[2] through data[11]. The current capacity is 12. Where
does the insert method place the new entry in the array?
oA. data[1]
oB. data[0]
oC. data[11]
oD. data[12]
o
4. Consider the implementation of the Queue using a circular array. What goes wrong
if we try to keep all the items at the front of a partially-filled array (so that data[0] is
always the front).
oA. The constructor would require linear time.
oB. The remove method would require linear time.
oC. The insert method would require linear time.
oD. The isEmpty method would require linear time.
5. In the circular array version of the Queue class, which operations require linear
time for their worst-case behavior?
oA. remove
oB. insert when the capacity has not yet been reached
oC. isEmpty
oD. None of these operations require linear time.
6. If data is a circular array of CAPACITY elements, and rear is an index into that
array, what is the formula for the index after rear?
oA. (rear % 1) + CAPACITY
oB. rear % (1 + CAPACITY)
oC. (rear + 1) % CAPACITY
oD. rear + (1 % CAPACITY)
1
pf3
pf4

Partial preview of the text

Download CSC 1052 Homework 4: Queue Concepts and Implementations - Prof. Catherine Helwig and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CSC 1052 Homework 4 - Queue Multiple Choice

  1. One difference between a queue and a stack is: o A. Queues require linked lists, but stacks do not. o B. Stacks require linked lists, but queues do not. o C. Queues use two ends of the structure; stacks use only one. o D. Stacks use two ends of the structure, queues use only one. o
  2. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? o A. ABCD o B. ABDC o C. DCAB o D. DCBA
  3. Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The current capacity is 12. Where does the insert method place the new entry in the array? o A. data[1] o B. data[0] o C. data[11] o D. data[12] o
  4. Consider the implementation of the Queue using a circular array. What goes wrong if we try to keep all the items at the front of a partially-filled array (so that data[0] is always the front). o A. The constructor would require linear time. o B. The remove method would require linear time. o C. The insert method would require linear time. o D. The isEmpty method would require linear time.
  5. In the circular array version of the Queue class, which operations require linear time for their worst-case behavior? o A. remove o B. insert when the capacity has not yet been reached o C. isEmpty o D. None of these operations require linear time.
  6. If data is a circular array of CAPACITY elements, and rear is an index into that array, what is the formula for the index after rear? o A. (rear % 1) + CAPACITY o B. rear % (1 + CAPACITY) o C. (rear + 1) % CAPACITY o D. rear + (1 % CAPACITY)
  1. I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into a NONEMPTY queue? o A. Neither changes o B. Only front changes. o C. Only rear changes. o D. An exception is caused
  2. Suppose remove is called on a priority queue that has exactly two entries with equal priority. How is the return value of remove selected? o A. One is chosen at random. o B. The one which was inserted first. o C. The one which was inserted most recently. o D. This can never happen (violates the precondition)
  3. An array of queues can be used to implement a priority queue, with each possible priority corresponding to its own element in the array. When is this implementation not feasible? o A. When the number of possible priorities is huge. o B. When the number of possible priorities is small. o C. When the queues are implemented using a linked list. o D. When the queues are implemented with circular arrays.
  4. To simulate people waiting in a line, which data structure would you use? a) Vector b) Queue c) Stack d) Set e) List For questions 11- 13, consider the following operations on a Queue data structure that stores int values. Queue q = new Queue( ); q.enqueue(3); q.enqueue(5); q.enqueue(9); System.out.println(q.dequeue( )); // d q.enqueue(2); q.enqueue(4); System.out.println(q.dequeue( )); // d System.out.println(q.dequeue( )); // d q.enqueue(1); q.enqueue(8);
  5. After the code above executes, how many elements would remain in q? a) 0 b) 4 c) 5 d) 6 e) 7

q.enqueue(1); q.dequeue( ); q.dequeue( ); q.enqueue(6); q.enqueue(5); q.dequeue( ); // at this point, there are only 2 item2 in the queue q.enqueue(7); // this enqueue can not occur, why??

  1. A queue class is given as follows, public class Queue{ Node front, rear; //front points to the beginning of the queue //rear points to the end of the queue class Node //inner class Node is the element of the queue { int value; Node next; private Node(int v) { value = v; next = null; } } public boolean isEmpty(); //check whether the queue is empty public Node peek(); //return a node from the front public Node dequeue(); //remove a node from the front public void enqueue(Node item); //add a new node at the end public int size(); //return the number of nodes in the queue } Implement Enqueue method
  2. Observe this code: IntQueue q = new IntQueue( ); q.insert(1); q.insert(2); q.insert(3); System.out.println(q.remove()); Suppose that q is represented by a linked list. Draw the state of the private instance variables of q after the above code: _______ front| | |_______|

rear | | |_______|

  1. .Describe why it is a bad idea to implement a linked list version of a queue that uses the head of the list as the rear of the queue and no tail pointer.