Skip to main content
Rollback to Revision 2
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

Edit (Added suggested methods):

def __len__(self):
    """
    Returns the current size of the list. O(n), linear time
    """ 
    current = self.head
    count = 0
    while current:
        count += 1
        current = current.next
    return count
    
def __contains__(self,value):
    """
    Returns True or False depending on whether an item with
    node.value = value is in the list 
    """
    current = self.head
    found = False
    while current and not found:
        if current.value == value:
            found = True
            return True
        else:
            current = current.next
    if not current:
        return False 
    
def __bool__(self):
    """
    Implements boolean check of the class
    """
    if self.__len__() == 0:
        return False
    else:
        return True
            
def __iter__(self):
    """
    Creates an iterator. Returns itself.
    """
    return self
        
def __next__(self):
    """
    Provides the next entry to the iterator
    """
    if not self.__current:
        self.__current=self.head
        raise StopIteration
    else:
        current = self.__current
        self.__current=self.__current.next
        return current

Edit (Added suggested methods):

def __len__(self):
    """
    Returns the current size of the list. O(n), linear time
    """ 
    current = self.head
    count = 0
    while current:
        count += 1
        current = current.next
    return count
    
def __contains__(self,value):
    """
    Returns True or False depending on whether an item with
    node.value = value is in the list 
    """
    current = self.head
    found = False
    while current and not found:
        if current.value == value:
            found = True
            return True
        else:
            current = current.next
    if not current:
        return False 
    
def __bool__(self):
    """
    Implements boolean check of the class
    """
    if self.__len__() == 0:
        return False
    else:
        return True
            
def __iter__(self):
    """
    Creates an iterator. Returns itself.
    """
    return self
        
def __next__(self):
    """
    Provides the next entry to the iterator
    """
    if not self.__current:
        self.__current=self.head
        raise StopIteration
    else:
        current = self.__current
        self.__current=self.__current.next
        return current
added 1484 characters in body
Source Link

Edit (Added suggested methods):

def __len__(self):
    """
    Returns the current size of the list. O(n), linear time
    """ 
    current = self.head
    count = 0
    while current:
        count += 1
        current = current.next
    return count
    
def __contains__(self,value):
    """
    Returns True or False depending on whether an item with
    node.value = value is in the list 
    """
    current = self.head
    found = False
    while current and not found:
        if current.value == value:
            found = True
            return True
        else:
            current = current.next
    if not current:
        return False 
    
def __bool__(self):
    """
    Implements boolean check of the class
    """
    if self.__len__() == 0:
        return False
    else:
        return True
            
def __iter__(self):
    """
    Creates an iterator. Returns itself.
    """
    return self
        
def __next__(self):
    """
    Provides the next entry to the iterator
    """
    if not self.__current:
        self.__current=self.head
        raise StopIteration
    else:
        current = self.__current
        self.__current=self.__current.next
        return current

Edit (Added suggested methods):

def __len__(self):
    """
    Returns the current size of the list. O(n), linear time
    """ 
    current = self.head
    count = 0
    while current:
        count += 1
        current = current.next
    return count
    
def __contains__(self,value):
    """
    Returns True or False depending on whether an item with
    node.value = value is in the list 
    """
    current = self.head
    found = False
    while current and not found:
        if current.value == value:
            found = True
            return True
        else:
            current = current.next
    if not current:
        return False 
    
def __bool__(self):
    """
    Implements boolean check of the class
    """
    if self.__len__() == 0:
        return False
    else:
        return True
            
def __iter__(self):
    """
    Creates an iterator. Returns itself.
    """
    return self
        
def __next__(self):
    """
    Provides the next entry to the iterator
    """
    if not self.__current:
        self.__current=self.head
        raise StopIteration
    else:
        current = self.__current
        self.__current=self.__current.next
        return current
deleted 145 characters in body; edited title
Source Link
jonrsharpe
  • 14.1k
  • 2
  • 37
  • 62

Python Linked List Implementation Unit Testingimplementation

Also, how could I unit test this implementation? That is, what particular tests should I implement to cover the use cases of the linked list?

Python Linked List Implementation Unit Testing

Also, how could I unit test this implementation? That is, what particular tests should I implement to cover the use cases of the linked list?

Python Linked List implementation

Source Link
Loading