2015-03-02 46 views
0

我需要爲我的程序添加一個索引和一個計數函數,但是我對如何解決這個問題感到困惑,因爲我的老師非常寬泛。任何想法,我可以做一個索引和計數函數添加到我的鏈接列表? 這裏是我的代碼:在我的鏈表中添加一個計數和一個索引

from ListNode import ListNode 

    class LinkedList(object): 

    #-------------------------------------------------------------- 

    def __init__(self, seq=()): 

     """ Pre: Creates a Linked List 
     Post: Creates a list containing the items in the seq=()""" 

     if seq ==(): 

      # If there is no items to be put into the list, then it creates an empty one. 
      self.head = None 

     else: 

      # Creates a node for the first item. 
      self.head = ListNode(seq[0], None) 

      # If there are remaining items, then they're added while keeping track of the last node. 
      last = self.head 
      for item in seq[1:]: 
       last.link = ListNode(item, None) 
       last = last.link 

     self.size = len(seq) 

    #------------------------------------------------------------- 

    def __len__(self): 

     '''Pre: Nothing. 
      Post: Returns the number of items in the list.''' 

     return self.size 

    #------------------------------------------------------------- 

    def _find(self, position): 

     '''This is a private method, which means it only works in this class. 
     This returns the last node used. (0 is the first item, size-1 is the last item) 
     Pre: 0 (less than or equal to) position (less than) self.size 
     Post: Returns the ListNode at the specified position in the list.''' 

     assert 0 <= position < self.size 

     node = self.head 
     # move forward until we reach the specified node 
     for i in range(position): 
      node = node.link 
      return node 

    #------------------------------------------------------------- 

    def append(self,x): 

     '''This adds (Appends) 'x' onto the end of the list 
     Post: X is appended to the end of the list.''' 

     # First create a new node containing x 
     newNode = ListNode(x) 

     # This will link it onto the end of the list. 

     if self.head is not None: 
      # Not an empty list 
      node = self._find(self.size - 1) 
      node.link = newNode 

     else: 
      # If it is an empty list. 
      # You will set self.head to the new node 
      self.head = newNode 
     self.size += 1 

    #------------------------------------------------------------- 

    def __getitem__(self, position): 

     ''' returns the data item at the location position 
     Pre: 0 <= position < size 
     Post: Returns data item at the specified position.''' 

     node = self._find(position) 
     return node.item 

    #------------------------------------------------------------- 

    def __setitem__(self, position, value): 

     ''' Sets the data item at the location position to the value. 
     Pre: 0 <= position < self.size 
     Post: Sets the data item at the specified position to value.''' 

     node = self._find(position) 
     node.item = value 

    #-------------------------------------------------------------- 

    def __delitem__(self, position): 

     ''' Deletes the item at the location position from the list. 
     Pre: 0 <= position < self.size 
     Post: The item at the specified position is removed from the list.''' 

     assert 0 <= position < self.size 

     self._delete(position) 

    #-------------------------------------------------------------- 

     def __max__(self): 

      ''' Goes through each node and compares what the max is for the linked list. 
      Post: Finds the max of the linked list and returns that value.''' 

      max_value = self.head.item 
      node = self.head.next 
      while node is not None: 
       if node.item > max_value: 
        max_value = node.item 
       node = node.link 
      return max_value 

     #-------------------------------------------------------------- 

     def __min__(self): 

      ''' Goes through each node and compares what the min is for the linked list. 
      Post: Finds the min of the linked list and returns that value.''' 

      min_value = self.head.item 
      node = self.head.next 
      while node is not None: 
       if node.item < min_value: 
        min_value = node.item 
       node = node.link 
      return min_value 

    #-------------------------------------------------------------- 

    def _delete(self, position): 

     ''' This is a private function where it deletes an item at the location 
     position from the list. 
     Pre: 0 <= position < self.size 
     Post: The item at the specified location is removed from the list. 
     The item is then returned (To be used with pop.)''' 

     if position == 0: 
      # Save the item from the initial node 
      item = self.head.item 

      # Change the self.head to point "over" the deleted node. 
      self.head = self.head.link 

     else: 
      # Find the node before the one that you are deleting. 
      prev_node = self._find(position - 1) 

      # Save the item from the node you are deleting. 
      item = prev_node.link.item 

      # Change the previous node to go over the deleted node. (Example 2 goes to 4) 
      prev_node.link = prev_node.link.link 

     self.size -= 1 
     return item 

    #-------------------------------------------------------------- 

    def pop(self, i = None): 

     ''' This returns and removes whatever is at position 'i' from the list. It is 
     defaulted 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, then the last item in the list is removed and then returned. 
     Otherwise, the item at position 'i' is removed and returned.''' 

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

     # The default is to delete the last item in the list. 
     # 'i' could be zero so we need to compare to None. 

     if i is None: 
      i = self.size - 1 

     return self._delete(i) 

    #----------------------------------------------------------------- 

    def insert(self, i, x): 

     ''' Inserts 'x' at the position 'i' is at in the list. 
     Pre: 0 <= i <= self.size 
     Post: x is inserted into the list at position 'i', and the old segment 
       from position 'i' (oldsize-1) are now at position 'i'+1 (newsize - 1).''' 

     assert 0 <= i <= self.size 

     if i == 0: 
      # Inserting before) requires changing/updating self.head. 
      self.head = ListNode(x, self.head) 

     else: 
      # Find the item that this node will be inserted after. 
      node = self._find(x, self.head) 
      node.link = ListNode(x, node.link) 

     self.size += 1 

    #----------------------------------------------------------------- 

    def __copy__(self): 

     ''' Post: Returns a new Linked List object that is a shallow copy of self.''' 

     a = LinkedList() 
     node = self.head 
     while node is not None: 
      a.append(node.item) 
      node = node.link 
     return a 

    #----------------------------------------------------------------- 

    def __iter__(self): 

     return LinkedListIterator(self.head) 

#--------------------------------------------------------------------- 

class LinkedListIterator(object): 

    #----------------------------------------------------------------- 

    def __init__(self, head): 
     self.currnode = head 

    #----------------------------------------------------------------- 

    def next(self): 
     if self.currnode is None: 
      raise StopIteration 
     else: 
      item = self.currnode.item 
      self.currnode = self.currnode.link 
      return item 

這似乎基本爲你們,但我在努力搞清楚什麼我的老師希望我的索引或該計劃數。

+0

你不應該在這裏發表您的實驗室。請問你的助教,講師還是你的朋友? – taesu 2015-03-02 17:34:03

回答

0

最好是看看這些方法的Python的文檔:

[].index.__doc__ 
[].count.__doc__ 
相關問題