CS 140a Homework 2 Due: Monday, October 26
CS 140a Homework 2 Due: Monday, October 26
1. A self-adjusting list is like a regular list, except that all insertions are performed at the head of the list, and when an item is accessed through Search(), it is moved to the head of the list without changing the relative order of the other items. he Search() function returns either !"# or $A%S# &ased on whether the item was found. 'rite the (nsert() and Search() functions for a self-adjusting list implemented with an arra). 'hat are the time complexities of the functions* (+o not take into consideration the t)pe of item in the list, )our functions should work the same regardless of the t)pe.) void (nsert(itemt)pe item) if(current.si/e 0 max)%1current.si/e2 3 item4 current.si/e554 6 else error - list is full 6 int Search(itemt)pe ke)) for(int i374 i0current.si/e4 i55)if(%1i2 33 ke))!emove(ke))4 (nsert(ke))4 return !"#4 6 6 return $A%S#4 6 9. ,(1)
,(n)
88 remove will essentiall) shifts the items to right down 88 puts item &ack at the head of the list
Show how to implement a :ueue using two stacks. Anal)/e the running time of the :ueue operations. ;ou do not have to actuall) write the code, )ou ma) just descri&e the method (pictures are helpful), &ut &e sure )ou are clear. #n:ueue(item) <ush item onto Stack 1. +e:ueue() <op all items from Stack 1 while pushing onto Stack 9. =ow the top item in Stack 9 is the one to &e de:ueued (<op Stack 9). <op all remaining items from Stack 9 while pushing onto Stack 1. #n:ueue ,(1) +e:ueue ,(n)
>.
Show how to implement a stack using two :ueues. Anal)/e the running time of the :ueue operations. ;ou do not have to actuall) write the code, )ou ma) just descri&e the method (pictures are helpful), &ut &e sure )ou are clear. <ush(item) <op() #n:ueue item onto ?ueue 1. +e:ueue all items &ut one from ?ueue 1 while en:ueueing onto ?ueue 9. he last item in ?ueue 1 is what is popped (de:ueue ?ueue 1). =ow think of ?ueue 9 as ?ueue 1 and vice versa. <op ,(n)
<ush
,(1)
@.
$or each part, specif) what t)pe of data structure would &est &e used for the following situation (list, stack, or :ueue). a. &. c. d. e. Austomers at a grocer) store wait to pa) for their groceries. ?"#"# Bansel and Cretel leave their home and traverse a path dropping &readcrum&s at particular locations on the wa). he) then want to use their &readcrum&s to find their wa) home (using the reverse path). S AAD A &ank keeps track of their customer accounts. %(S A li&rar) stores the information a&out each &ook that the) own. %(S Students wait at the !egistrar to enroll in classes. ?"#"#
E.
'rite functions for an inefficient :ueue, where the items are stored in an arra) in such a wa) that the :ueueFs head is alwa)s at location 7 and the :ueueFs tail is alwa)s at location current_size_of queue -1. Specif) the time complexities of )our functions. void #n:ueue(itemt)pe item) if (tail G3 max) error - :ueue full else?1tail2 3 item4 tail554 6 6 itemt)pe +e:ueue() if (#mpt)()) error - :ueue empt) elseitemt)pe data 3 ?1head24 for(int i374 i0tail-14 i55) ?1i2 3 ?1i5124 tail--4 6 6 ,(1)
H.
Aonvert the following expressions to postfix notation. a. (a5&)I(c-dIe)8f a&5cdeI-If8 &. aI(&5c8dIe)-f a&cd8eI5IfShow the stack at each stage when evaluating the following postfix expression. K>9I-LH98->I5 K K > K > 9 K H > > L > L H > L H 9 > L > > E > E > > 1E 1L
J.