Example #1
0
 def reverse(self, phoneb):
     '''(PhoneBook,PhoneBook) -> PhoneBook
     Reverse the contacts from the phonebook that are present in the
     alphabetical order
     REQ: List must be given'''
     # create empty sll to return the reversed link
     rev = SingleLinkedList()
     # if the reversed sll is empty
     if (rev.get_head() is None):
         # create a node and set the element from given phonebook
         new = Node(phoneb.get_head().get_element())
         # set the node head
         rev.set_head(new)
     # Traverse through the given phonebook and continue traversing
     # through the list
     curr = phoneb._head.get_next()
     while (curr is not None):
         new = Node(curr.get_element())
         rev.add_first(new)
         curr = curr.get_next()
     # return the reversed phonebook
     return rev
Example #2
0
 def reversed_phonebook(self):
     '''(PhoneBook) -> PhoneBook
     Returns the phonebook of the contacts in the reverse order from the
     phonebook of the  alphabetical order
     REQ: phonebook must not be empty'''
     # create the empty sll for reverse contacts
     rev = SingleLinkedList()
     # if the reverse phonebook is empty
     if (rev.get_head() is None):
         # create a new node and add that element in reverse phonebook
         new = Node(self.get_head().get_element())
         # set that node as a head of the reverse phonebook
         rev.set_head(new)
     # traverse through the list & keep adding the contact in rev phonebook
     curr = self._head.get_next()
     while (curr is not None):
         # create empty node and add the element in it
         new = Node(curr.get_element())
         # keep adding notes in the front in rev phonebook
         rev.add_first(new)
         # continue traversing through the list
         curr = curr.get_next()
     # return the reversed phonebook
     return rev