l
\ไLc               @   s:   d  d l  m  Z  Gd   d e  Z Gd   d e  Z d S(   i    (   u   ListNodec             B   s   |  Ee  Z d d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d d  Z d	   Z d
   Z d   Z d S(   c             C   s   | d k r d |  _ nU t | d d  |  _ |  j } x3 | d d  D]! } t | d  | _ | j } qH Wt |  |  _ d S(   uD   create an LList
        post: creates a list containing items in seqi    i   N(    (   u   Noneu   headu   ListNodeu   linku   lenu   size(   u   selfu   sequ   lastu   item(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __init__   s    	c             C   s   |  j  S(   u)   post: returns number of items in the list(   u   size(   u   self(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __len__   s    c             C   sR   d | k o |  j  k  n s% t  |  j } x t |  D] } | j } q; W| S(   u๐   private method that returns node that is at location position
        in the list (0 is first item, size-1 is last item)
        pre: 0 <= position < self.size
        post: returns the ListNode at the specified position in the
        listi    (   u   sizeu   AssertionErroru   headu   rangeu   link(   u   selfu   positionu   nodeu   i(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   _find&   s
    %	c             C   sY   t  |  } |  j d k	 r= |  j |  j d  } | | _ n	 | |  _ |  j d 7_ d S(   uS   appends x onto end of the list
        post: x is appended onto the end of the listi   N(   u   ListNodeu   headu   Noneu   _findu   sizeu   link(   u   selfu   xu   newNodeu   node(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   append8   s    	c             C   s   |  j  |  } | j S(   u   return data item at location position
        pre: 0 <= position < size
        post: returns data item at the specified position(   u   _findu   item(   u   selfu   positionu   node(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __getitem__M   s    c             C   s   |  j  |  } | | _ d S(   u   set data item at location position to value
        pre: 0 <= position < self.size
        post: sets the data item at the specified position to valueN(   u   _findu   item(   u   selfu   positionu   valueu   node(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __setitem__X   s    c             C   s6   d | k o |  j  k  n s% t  |  j |  d S(   uจ   delete item at location position from the list
        pre: 0 <= position < self.size
        post: the item at the specified position is removed from 
        the listi    N(   u   sizeu   AssertionErroru   _delete(   u   selfu   position(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __delitem__c   s    %c             C   sk   | d k r* |  j  j } |  j  j |  _  n. |  j | d  } | j j } | j j | _ |  j d 8_ | S(   Ni    i   (   u   headu   itemu   linku   _findu   size(   u   selfu   positionu   itemu	   prev_node(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   _deletep   s    c             C   si   |  j  d k r: | d k s@ d | k o5 |  j  k  n s@ t  | d k r\ |  j  d } n  |  j |  S(   uE  returns and removes at position i from list; the default is to
        return and remove the last item

        pre: self.size > 0 and ((i is None or (0 <= i < self.size))

        post: if i is None, the last item in the list is removed
        and returned; otherwise the item at position i is removed 
        and returnedi    i   N(   u   sizeu   Noneu   AssertionErroru   _delete(   u   selfu   i(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   pop   s    @c             C   s   d | k o |  j  k n s% t  | d k rI t | |  j  |  _ n( |  j | d  } t | | j  | _ |  j  d 7_  d S(   uๆ   inserts x at position i in the list
        pre: 0 <= i <= self.size
        post: x is inserted into the list at position i and 
              old elements from position i..oldsize-1 are at positions 
              i+1..newsize-1i    i   N(   u   sizeu   AssertionErroru   ListNodeu   headu   _findu   link(   u   selfu   iu   xu   node(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   insertฃ   s    %c             C   sB   t    } |  j } x) | d k	 r= | j | j  | j } q W| S(   u?   post: returns a new LList object that is a shallow copy of selfN(   u   LListu   headu   Noneu   appendu   itemu   link(   u   selfu   au   node(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __copy__ธ   s    		c             C   s   t  |  j  S(   N(   u   LListIteratoru   head(   u   self(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __iter__ะ   s    N(    (   u   __name__u
   __module__u   __init__u   __len__u   _findu   appendu   __getitem__u   __setitem__u   __delitem__u   _deleteu   Noneu   popu   insertu   __copy__u   __iter__(   u
   __locals__(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   LList   s   
									u   LListc             B   s)   |  Ee  Z d    Z d   Z d   Z d S(   c             C   s   | |  _  d  S(   N(   u   currnode(   u   selfu   head(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __init__ฺ   s    c             C   s;   |  j  d  k r t  n |  j  j } |  j  j |  _  | Sd  S(   N(   u   currnodeu   Noneu   StopIterationu   itemu   link(   u   selfu   item(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   next฿   s
    	c             C   s;   |  j  d  k r t  n |  j  j } |  j  j |  _  | Sd  S(   N(   u   currnodeu   Noneu   StopIterationu   itemu   link(   u   selfu   item(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   __next__้   s
    	N(   u   __name__u
   __module__u   __init__u   nextu   __next__(   u
   __locals__(    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   LListIteratorึ   s   
		
u   LListIteratorN(   u   ListNodeu   objectu   LListu   LListIterator(    (    (    uC   C:\Users\Terry\Desktop\Reed\CodeByChapter\Python3\Chapter4\LList.pyu   <module>   s   า