正在加载图片...
6.001, Spring Semester, 2005--Quiz II Part 1:(25 points) We are going to explore a new kind of data structure, called a cycle. You can think of this as a kind of circular list: indeed we are going to represent a cy cle as a loop of cons cells, where the car of each cell point s to a value, and the cdr of each cell points to the next element in the circular sequence. To create a cycle from a list, we can use (define (list->cycle lst) et-cdr! (last lst) lst) (define (last lst) (if (null? lst) (error "not long enough") (if (null? (cdr lst)) (last (cdr lst))))) (define test-cycle (list->cycle (a b c g))) To see what this struct ure looks like, you might find it convenient to draw a box-and-pointer Associated wit h a cycle, we have several operat ions head will ret urn the value stored at the cell in the cy cle pointed to by its argument, e. g (head test-cycle)will return the value a rotate-left willrot ate t he cycle one element to the left, e.g,(head (rotate-left test-cycle)) will return the value b; (you can think of a cycle as a circular list in which the arranged in clo ckwise sequence, the head is at the top, and"left means rot at ing the sequence counter-clockwise) rotate-right will rot ate the cycle one element to the right, in analogy to rot ate-left, e. g (head (rot ate-right test-cy cle)) will ret urn t he value￾       '     :    <  , /   %&    !   )/  )% %  /   %2  ,    %  . %    ￾ %& ,) ) ￾  )  %   -&  ) ￾  )  %  ) <   )  %*!    .   %& ,  %2 ￾ ￾   ￾  ￾    ￾ ￾  ￾ ￾  ￾    ￾ ￾ ￾    ￾ ￾    <2 ￾   ￾  ￾    % ,) )% % /% /& . ) =  -  ,  0<77   ) % 0,! %% ,)  .& , )- %- %2 ￾  ,  ) - %  )   ) .   0. % & !!   ￾ ￾  ,  ) - > ￾     ,  ) .    ) & !!&       ￾ ￾  ,  ) - > ?.  )/   . %   %  ,)) ) %    /,% %*& ) ) %  ) &  @A %  ) %* 7/,%B> ￾     ,  ) .    ) )&  .      & !!&       ￾ ￾  ,  ) - !
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有