In [5]:
class Node:

    def __init__(self, name, phone, address, next):
        self._name = name
        self._phone = phone
        self._address = address
        self._next = next

    def __str__(self):
        return "("+self._name+","+self._phone+","+self._address+")"
In [6]:
n2 = Node("John","100-1234","123 Elm Street", None)
n1 = Node("Alice","111-1234","123 Main Street", n2)
In [9]:
class ContactsLL:
    
    def __init__(self):
        self._head = Node("","","",None)
        self._size = 0
    
    def find(self,name):
        # Return pointer to previous node if found
        # Return None if not found
        p = self._head
        found = False
        while p._next != None and not found:
            if p._next._name == name:
                found = True
            else:
                p = p._next
        if found:
            return p
        else:
            None

    def insert(self,contact):
        # insert contact as 1st item in linked list because we are not maintaining
        # sorted entries
        # first check if name is already in list; if yes the do not insert
        p = self.find(contact[0])
        if p == None:
            n = Node(contact[0],contact[1],contact[2],self._head._next)
            self._head._next = n
            self._size = self._size + 1
            return True
        else:
            return False
    
    def delete(self,name):
        p = self.find(name)
        if p == None:
            return False
        else:
            p._next = p._next._next
            self._size = self._size - 1
            return True
    
    def size(self):
        return self._size
    
    def update(self,contact):
        p = self.find(contact[0])
        if p == None:
            return False
        else:
            p._next._phone = contact[1]
            p._next._address = contact[2]
            return True
    
    def __str__(self):
        p = self._head
        result = "\n"
        while p._next != None:
            result = result + str(p._next)+"\n"
            p = p._next
        return result + "\n"
        
In [18]:
mylist = ContactsLL()
mylist.insert(("Alice","111-1111","123 Main Street"))
mylist.insert(("James","111-1112","123 Elm Street"))
mylist.insert(("John","111-1113","123 Oak Street"))
mylist.update(("Alice","999-9999","123 Oak Street"))
mylist.delete("James")
x = mylist.insert(("Alice","888-8888","123 Main Street"))
print(x)
x = mylist.delete("Raj")
print(x)
print(mylist)
False
False

(John,111-1113,123 Oak Street)
(Alice,999-9999,123 Oak Street)


In [ ]: