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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

150 Chap. 4 Lists, Stacks, <strong>and</strong> Queues4.19 A common problem for compilers <strong>and</strong> text edi<strong>to</strong>rs is <strong>to</strong> determine if theparentheses (or other brackets) in a string are balanced <strong>and</strong> properly nested.For example, the string “((())())()” contains properly nested pairs of parentheses,but the string “)()(” does not, <strong>and</strong> the string “())” does not containproperly matching parentheses.(a) Give an algorithm that returns true if a string contains properly nested<strong>and</strong> balanced parentheses, <strong>and</strong> false otherwise. Use a stack <strong>to</strong> keeptrack of the number of left parentheses seen so far. Hint: At no timewhile scanning a legal string from left <strong>to</strong> right will you have encounteredmore right parentheses than left parentheses.(b) Give an algorithm that returns the position in the string of the first offendingparenthesis if the string is not properly nested <strong>and</strong> balanced.That is, if an excess right parenthesis is found, return its position; ifthere are <strong>to</strong>o many left parentheses, return the position of the first excessleft parenthesis. Return −1 if the string is properly balanced <strong>and</strong>nested. Use a stack <strong>to</strong> keep track of the number <strong>and</strong> positions of leftparentheses seen so far.4.20 Imagine that you are designing an application where you need <strong>to</strong> performthe operations Insert, Delete Maximum, <strong>and</strong> Delete Minimum. Forthis application, the cost of inserting is not important, because it can be doneoff-line prior <strong>to</strong> startup of the time-critical section, but the performance ofthe two deletion operations are critical. Repeated deletions of either kindmust work as fast as possible. Suggest a data structure that can support thisapplication, <strong>and</strong> justify your suggestion. What is the time complexity foreach of the three key operations?4.21 Write a function that reverses the order of an array of n items.4.7 Projects4.1 A deque (pronounced “deck”) is like a queue, except that items may be added<strong>and</strong> removed from both the front <strong>and</strong> the rear. Write either an array-based orlinked implementation for the deque.4.2 One solution <strong>to</strong> the problem of running out of space for an array-based listimplementation is <strong>to</strong> replace the array with a larger array whenever the originalarray overflows. A good rule that leads <strong>to</strong> an implementation that is bothspace <strong>and</strong> time efficient is <strong>to</strong> double the current size of the array when thereis an overflow. Re-implement the array-based List class of Figure 4.2 <strong>to</strong>support this array-doubling rule.

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

Saved successfully!

Ooh no, something went wrong!