from node import *

class LinkedList(object):
    '''A linked list implementation'''
    
    def __init__(self):
        """A new empty List."""
        self.head = None
    
    def is_empty(self):
        '''Return True iff this List is empty.'''
        return self.head == None
    
    def add(self, d):
        '''Add d to this list.'''
        temp = Node(d)
        temp.next = self.head
        self.head = temp
    
    def length(self):
        '''Return the number of items in this List.'''
        curr = self.head
        count = 0
        while curr != None:
            count += 1
            curr = curr.next
        
        return count

    def contains(self, item):
        '''Return whether item is in this List.'''
        curr = self.head
        while curr != None and curr.data != item:
            curr = curr.next
        
        # If we found it, curr is not None.
        return curr != None

    def remove(self, item):
        '''Remove item from me.  Requires: item is in this List.'''
        curr = self.head
        prev = None
        while curr.data != item:
            prev = curr
            curr = curr.next
    
        if self.head == curr:
            # item is at the front
            self.head = curr.next
        else:
            prev.next = curr.next


