Data Structures Midterm CHAPTER 8

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

12. What C++ data type should be used for numbers that can never be negative?

Unsigned integer.

21. Write a new constant member function that returns a copy of the item at the rear of the queue. Use the LL version of the queue.

Body of your function should assert size() > 0 and then return with: return rear_ptr->data;

1. What is the meaning of FIFO?

First in First Out

26. Suppose you are using a LL to implement a deque. What kind of LL would be best?

For inserting at both ends, an ordinary LL is fine. But for removing at both ends, a doubly LL is needed.

17. Under what circumstances is a helper function useful?

Helper function is a private member function that is useful when a class requires an operation that should not be part of the public interface.

25. Suppose you are storing the items of a deque in an array. Why do you need to use a circular array rather than putting all the items at the front?

If all items were kept at the front of the array, then it would be time-consuming to add or remove at the front.

19. In a circular array implementation of a queue, why does the constructor set last to final index of the array.

If last is initialized to final index of array, then a new item is inserted correctly into empty Q. Specifically, the statement "last = next_index(last)" will place the first item at data[first].

20. A programmer who sees our array implementation of a queue shown in fig 8.10 on 418 gives us the suggestion: "Why not eliminate the count member variable, since the number of elements in the queue can be determined from the values of front and rear"? Is there a problem with this suggestion?

Main problem is that you cannot tell the diff between empty Q (which has front equal to next_index(rear) and a full queue (which also has front equal to next_index(rear))

23. For our implementation, the front_ptr must be NULL for an empty queue. Which member functions rely on this requirement?

Push member function requires that front_ptr is NULL for an empty Q. Otherwise the call to list_head_insert does not work. Destructor also requires a valid NULL pointer for empty Q (otherwise list_clear does not work).

16. When the car wash simulation finishes, there could still be some numbers in the queue. What do these numbers represent from the real world?

These are cars that arrived during the simulation, but are still waiting in line at end of simulation

8. How would you modify the palindromes program so that it indicates the first position in the input string that violates the palindrome property?

Use a 2nd Q called line. As input is being read, each character placed in line Q. During comparison phase, also keep track of how many characters correctly matched. If mismatch occurs, can print appropriate ammound of line queue as part of error message.

13. Describe at least one assumption we made about the real-world car wash in order to make the simulation more manageable?

We assumed that no more than 2 customer arrives during any particular second.

10. Can a single program use both a stack and a queue?

Yes

27. Why does the test for Case 2 of Fig 8.13 use *last_bp rather than last_bp?

last_bp would be 3rd pointer on left side of Fig 8.13

18. Write a new constant member function that returns a copy of the item at the rear of the queue. Use the array version of the queue.

Body of function should assert that size() > 0 and then return data[rear]

4. Suppose a program uses a queue of characters to read in a word and then echo it to the screen. Now suppose input word is LINE. Trace the algorithm, giving all calls to operations push, front, and pop.

push(L) push(I) push(N) push(E) front() pop() front() pop() front() pop() front() pop()

15. What is RAND_MAX? Where is it defined?

Constant defined in cstdlib Specifies largest return value of rand

22. For our implementation, what is the value of the rear_ptr in an empty queue.

Could be any value. Our implementation does not care.

9. How would you modify the palindromes program so that upper- and lowercase versions of letters are considered different?

Do no apply toupper function.

6. Write pseudocode for an algorithm that reads an even number of characters. The algorithm then prints the 1st character, 3rd character, 5th character, and so on. On a second output line, the algorithm prints the second character, fourth character, sixth character, and so on. Use 2 queues to store the characters.

Read characters two at a time. Each pair of characters has first character placed in Q1, second placed in Q2. Print all characters from Q1 on one line Print all characters from Q2 on next

5. Name some common situations where a PC's OS uses some kind of a queue

Reading input from a keyboard and sending output to a printer

11. How are time stamps used in simulation?

Record arrival and waiting times for items in simulation. Time stamp is number of simulated seconds that have passed since start of simulation. Waiting time of an item is the total number of seconds simulated so far minus time stamp.

24. What goes wrong if we try to put the front of the queue at the tail of the LL?

Removals will be hard to implement.

7. What C++ function deterrmines if a character is alphabetic? What header provides this function?

The cctype lib facility provides the isalpha function. Returns true if its single argument is one of alphabetic characters.

14. Use short sentences to describe the 3 main actions that occur during each second of simulated time in the car wash simulation.

a. Sometimes add a new customer to arrivals Q b. Sometimes start a new car through the washer c. Tell washer that another second has passed

2. What are the traditional names for the queue operations that add an item and remove an item from a queue?

enqueue de-queue

3. What queue functions are used to avoid queue overflow and underflow?

size function tests for queue capacity empty function tests for empty queue


Kaugnay na mga set ng pag-aaral

C243 Ch. 8 Intercompany Indebtedness

View Set

Impact of European Colonialism in the Americas

View Set

4A: Dónde vs. Adónde Additional Practice

View Set

Chapter 7: Assisting with the Nursing Process

View Set

II Lecture Chapter 20 Multiple Choice and True False Quiz

View Set