23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Suppose an <strong>in</strong>itially-empty queue Q has performed a total of 32 enqueue<br />

operations, 10 front operations, <strong>and</strong> 15 dequeue operations, 5 of which<br />

generated QueueEmptyExceptions, which were caught <strong>and</strong> ignored. What is the<br />

current size of Q?<br />

R-5.8<br />

If the queue of the previous problem was implemented with an array of capacity<br />

N = 30, as described <strong>in</strong> the chapter, <strong>and</strong> it never generated a<br />

FullQueueException, what would be the current values of f <strong>and</strong> r?<br />

R-5.9<br />

Describe the output for the follow<strong>in</strong>g sequence of deque ADT operations:<br />

addFirst(3), addLast(8), addLast(9), addFirst(5),<br />

removeFirst(), remove-Last(), first(), addLast(7),<br />

removeFirst(), last(), removeLast().<br />

R-5.10<br />

Suppose you have a deque D conta<strong>in</strong><strong>in</strong>g the numbers (1,2,3,4,5,6,7,8), <strong>in</strong> this<br />

order. Suppose further that you have an <strong>in</strong>itially empty queue Q. Give a pseudocode<br />

description of a method that uses only D <strong>and</strong> Q (<strong>and</strong> no other variables or<br />

objects) <strong>and</strong> results <strong>in</strong> D stor<strong>in</strong>g the elements (1,2,3,5,4,6,7,8), <strong>in</strong> this order.<br />

R-5.11<br />

Repeat the previous problem us<strong>in</strong>g the deque D <strong>and</strong> an <strong>in</strong>itially empty stack S.<br />

Creativity<br />

C-5.1<br />

Suppose you have a stack S conta<strong>in</strong><strong>in</strong>g n elements <strong>and</strong> a queue Q that is <strong>in</strong>itially<br />

empty. Describe how you can use Q to scan S to see if it conta<strong>in</strong>s a certa<strong>in</strong><br />

element x, with the additional constra<strong>in</strong>t that your algorithm must return the<br />

elements back to S <strong>in</strong> their orig<strong>in</strong>al order. You may not use an array or l<strong>in</strong>ked<br />

list—only S <strong>and</strong> Q <strong>and</strong> a constant number of reference variables.<br />

C-5.2<br />

Give a pseudo-code description for an array-based implementation of the<br />

double-ended queue ADT. What is the runn<strong>in</strong>g time for each operation?<br />

C-5.3<br />

306

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

Saved successfully!

Ooh no, something went wrong!