Вы находитесь на странице: 1из 4

# 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) 7. 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 o o

## B. Only front changes. C. Only rear changes. D. An exception is caused

8. 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) 9. 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. 10. 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( )); // d1 q.enqueue(2); q.enqueue(4); System.out.println(q.dequeue( )); // d2 System.out.println(q.dequeue( )); // d3 q.enqueue(1); q.enqueue(8); 11. After the code above executes, how many elements would remain in q? a) 0 b) 4 c) 5 d) 6 e) 7 12. What value is returned by the last dequeue operation (denoted above with a d3 in comments)? a) 3

b) 5 c) 9 d) 2 e) 4 13. If we replace the System.out.println statements (denoted in comments as d1, d2 and d3) with the statement q.enqueue(q.dequeue( )); q would contain which order of int values after all instructions have executed? a) 3, 5, 9, 2, 4, 1, 8 b) 3, 5, 9, 1, 8, 2, 4 c) 5, 9, 2, 4, 1, 8, 3 d) 3, 2, 4, 5, 9, 1, 8 e) 2, 4, 1, 8, 3, 5, 9 T/F 1) __The push and enqueue operations are essentially the same operations, push is used for Stacks and enqueue is used for Queues. 2) __Queues and Stacks can be implemented using either arrays or linked lists. 3) ___In order to input a list of values and output them in order, you could use a Queue. In order to input a list of values and output them in opposite order, you could use a Stack. Free Form 1. A Queue q stores int values. Show what q will look like after each of the following instructions is executed. q.enqueue(6); q.enqueue(12); q.enqueue(13); q.dequeue( ); q.dequeue( ); q.enqueue(19); q.enqueue(21); q.enqueue(22); q.dequeue( ); q.enqueue(20); 2. In implementing a Queue using an array, a problem might arise if the Queue is implemented in such a way that items in the Queue are inserted at the next available location and removed from the next leading position, but such that, once deleted, the emptied space is unused. The problem that arises is one where there is free space still in the array, but it is not usuable because it is not at the end. Demonstrate this problem with a Queue that is stored in an array of size 5 for the following instructions. Next, explain how you might resolve this problem. Queue q = new Queue(5); // assume the Queue constructor takes 5 as the size of the array q.enqueue(3); q.enqueue(4); q.enqueue(1); q.dequeue( ); q.dequeue( ); q.enqueue(6);

## q.enqueue(5); q.dequeue( ); q.enqueue(7);

// at this point, there are only 2 item2 in the queue // this enqueue can not occur, why??

3. 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 4. 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 | | |_______| 5. .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.