class Node:
def __init__(self, name, phone, address, nexxt):
self._name = name
self._phone = phone
self._address = address
self._next = nexxt
def __str__(self):
return "("+self._name+","+self._phone+","+self._address+")"
n1 = Node("Anna","111-2222","123 Main Street",None)
n2 = Node("Thomas","111-2223","124 Main Street",None)
print(n1)
print(n2)
#from Node import *
class ContactsLL(object):
# singly linked list of contacts with header node arranged in sorted order of _name
def __init__(self):
self._head = Node("","","",None)
self._size = 0
# find searches the LL for name and returns pointer to previous Node if name is found
# else returns pointer to previous Node after which name should appear in the sorted list
def find(self,name):
p = self._head
found = False
stop = False
while p._next != None and not found and not stop:
if p._next._name == name:
found = True
elif p._next._name < name:
p = p._next
else:
stop = True
return (found,p)
# insert takes a tuple, contact = ("Raj","111-1111","123 main st"), as a parameter, and inserts it
# into the sorted linked list in the proper position; finally return True/False
def insert(self,contact):
(found,p) = self.find(contact[0])
if not found:
temp = Node(contact[0],contact[1],contact[2],p._next)
p._next = temp
self._size = self._size + 1
return True
else:
return False
# This method takes name as parameter and deletes entry for name from the LL;
# returns True/False
def delete(self,name):
## Homework
???
self._size = self._size - 1
# This method takes contact as parameter and updated the phone and address for
# the entry corresponding to contact[0]
# returns True/False
def update(self,contact):
## Homework
def size(self):
return self._size
def __str__(self):
p = self._head
result = "\n"
while p._next != None:
result = result + str(p._next)+"\n"
p = p._next
return result+"\n"