Answers for Chapter 8 Exercises - Data Structures | CSCI 270, Assignments of Data Structures and Algorithms

Material Type: Assignment; Professor: Huerter; Class: DATA STRUCTURES; Subject: Computer Science - CSCI; University: Texas A & M University-Commerce; Term: Spring 2006;

Typology: Assignments

Pre 2010

Uploaded on 08/17/2009

koofers-user-5kh
koofers-user-5kh ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 270 Answers for Chapter 8 Exercises
Section 8.2, p. 410
For #1-4, there is one queue picture for each add/remove operation, from left to
right.
1. | A | | | | | | A | B | | | | | A | B | C | | |
f b f b f b
ch = โ€˜Aโ€™
| A | B | C | | | | A | B | C | A | | queue contains B C A
f b f b
2. | X | | | | | | X | Y | | | | | X | Y | Z | | |
f b f b f b
ch = โ€˜Xโ€™ ch = โ€˜Yโ€™ ch = โ€˜Zโ€™
| X | Y | Z | | | | X | Y | Z | | | | X | Y | Z | | |
f b f b f b
queue is left empty
#3 and 4 are really awful -- I wouldn't ask you to do anything this confusing on the
exam!
i = 1
3. | q | | | | | | q | r | | | | | q | r | | | |
f b f b f b
i = 2
| q | r | q | | | | q | r | q | r | | | q | r | q | r | |
f b f b f b
i = 3
| q | r | q | r | r | | s | r | q | r | r | queue contains r r s
b f b f
i = 1
4. | A | | | | | | A | B | | | | | A | B | | | |
f b f b f b
i = 2
| A | B | A | | | | A | B | A | B | | | A | B | A | B | |
f b f b f b
i = 3
| A | B | A | B | B | | C | B | A | B | B | | C | B | A | B | B |
b f b f b f
i = 4
| C | A | A | B | B | error -- queue is full | C | A | A | B | B |
b f add operation ignored b f
pf3
pf4
pf5

Partial preview of the text

Download Answers for Chapter 8 Exercises - Data Structures | CSCI 270 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CSCI 270 Answers for Chapter 8 Exercises

Section 8.2, p. 410

For #1-4, there is one queue picture for each add/remove operation, from left to right.

  1. | A | | | | | | A | B | | | | | A | B | C | | | f b f b f b ch = โ€˜Aโ€™ | A | B | C | | | | A | B | C | A | | queue contains B C A f b f b
  2. | X | | | | | | X | Y | | | | | X | Y | Z | | | f b f b f b ch = โ€˜Xโ€™ ch = โ€˜Yโ€™ ch = โ€˜Zโ€™ | X | Y | Z | | | | X | Y | Z | | | | X | Y | Z | | | f b f b f b queue is left empty #3 and 4 are really awful -- I wouldn't ask you to do anything this confusing on the exam! i = 1
  3. | q | | | | | | q | r | | | | | q | r | | | | f b f b f b i = 2 | q | r | q | | | | q | r | q | r | | | q | r | q | r | | f b f b f b i = 3 | q | r | q | r | r | | s | r | q | r | r | queue contains r r s b f b f i = 1
  4. | A | | | | | | A | B | | | | | A | B | | | | f b f b f b i = 2 | A | B | A | | | | A | B | A | B | | | A | B | A | B | | f b f b f b i = 3 | A | B | A | B | B | | C | B | A | B | B | | C | B | A | B | B | b f b f b f i = 4 | C | A | A | B | B | error -- queue is full | C | A | A | B | B | b f add operation ignored b f
  1. a) prototype: bool full () const; // PRE: queue has been initialized // POST: returns true if no more items can be added to the queue, // or returns false otherwise bool Queue :: full () const { return myFront == (myBack + 1) % QUEUE_CAPACITY; }
  2. // prototype: int size () const; /================================================ Find number of elements in the queue. Precondition: none Postcondition: Number of queue elements is returned. ================================================/ // Definition: int Queue :: size () const { if (myFront == myBack) return 0; else if (myFront > myBack) return myBack โ€“ myFront + QUEUE_CAPACITY; else return myBack โ€“ myFront; }
  3. If itโ€™s not a member function and not a friend function, it must be a client function. /================================================ Find number of elements in a queue received as a value parameter. Precondition: none Postcondition: Number of queue elements is returned. ================================================/ int size (Queue q) { int count = 0; while (!q.empty()) { q.dequeue(); count++; } return count; }

last = q.front(); q.dequeue(); } return last; }

  1. // Prototype: QueueElement nthElement (int n); /================================================== ==== Retrieve the nth element of a queue. Precondition: 1 <= n <= number of queue elements (there are at least n elements) Postcondition: nth element of the queue is returned, unless queue has fewer than n Elements, in which case a QueueUnderflow exception is thrown (author prints an error message). The elements preceding the nth element are removed from the queue. =================================================== ==/ // Definition: QueueElement nthElement (int n) { QueueElement elem; while (n > 0 && !empty()) { elem = front(); dequeue(); n--; } if (n > 0) throw QueueUnderflow(); return elem; }
  2. // Prototype: QueueElement nthElement (int n) const; /================================================== ==== Retrieve the nth element of a queue. Precondition: 1 <= n <= number of queue elements (there are at least n elements) Postcondition: nth element of the queue is returned, unless queue has fewer than n Elements, in which case a QueueUnderflow exception is thrown (author prints an error message). The elements preceding the nth element remain unchanged. =================================================== ===/ // Definition: QueueElement Queue :: nthElement (int n) const
  1. The algorithm is as follows: Create a stack While the queue is not empty Remove an item from the queue Push the item onto the stack While the stack is not empty Remove an item from the stack Add this item to the queue