12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 4.6 Exercises 149(a) The data field is eight bytes, a pointer is four bytes, <strong>and</strong> the array holdstwenty elements.(b) The data field is two bytes, a pointer is four bytes, <strong>and</strong> the array holdsthirty elements.(c) The data field is one byte, a pointer is four bytes, <strong>and</strong> the array holdsthirty elements.(d) The data field is 32 bytes, a pointer is four bytes, <strong>and</strong> the array holdsforty elements.4.12 Determine the size of an int variable, a double variable, <strong>and</strong> a pointer onyour computer.(a) Calculate the break-even point, as a function of n, beyond which thearray-based list is more space efficient than the linked list for listswhose elements are of type int.(b) Calculate the break-even point, as a function of n, beyond which thearray-based list is more space efficient than the linked list for listswhose elements are of type double.4.13 Modify the code of Figure 4.18 <strong>to</strong> implement two stacks sharing the samearray, as shown in Figure 4.20.4.14 Modify the array-based queue definition of Figure 4.25 <strong>to</strong> use a separateBoolean member <strong>to</strong> keep track of whether the queue is empty, rather thanrequire that one array position remain empty.4.15 A palindrome is a string that reads the same forwards as backwards. Usingonly a fixed number of stacks <strong>and</strong> queues, the stack <strong>and</strong> queue ADT functions,<strong>and</strong> a fixed number of int <strong>and</strong> char variables, write an algorithm <strong>to</strong>determine if a string is a palindrome. Assume that the string is read fromst<strong>and</strong>ard input one character at a time. The algorithm should output true orfalse as appropriate.4.16 Re-implement function fibr from Exercise 2.11, using a stack <strong>to</strong> replacethe recursive call as described in Section 4.2.4.4.17 Write a recursive algorithm <strong>to</strong> compute the value of the recurrence relationT(n) = T(⌈n/2⌉) + T(⌊n/2⌋) + n; T(1) = 1.Then, rewrite your algorithm <strong>to</strong> simulate the recursive calls with a stack.4.18 Let Q be a non-empty queue, <strong>and</strong> let S be an empty stack. Using only thestack <strong>and</strong> queue ADT functions <strong>and</strong> a single element variable X, write analgorithm <strong>to</strong> reverse the order of the elements in Q.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!