Ejemplo n.º 1
0
    def remove_index(self, index):
        """
        Delete single node at index
        Returns deleted value
        """
        if self.empty_list():
            raise ListEmpty("List is empty")

        current = self.head
        ret = None

        previous = None
        counter = 0
        while current is not None:
            ret = current.data
            if counter == index:
                if previous is None:
                    self.head = current.next
                else:
                    previous.next = current.next
                return ret

            previous = current
            current = current.next
            counter += 1

        raise IndexError("Index out of bounds, too high")
Ejemplo n.º 2
0
 def erase(self):
     """ Erase entire list """
     # What if someone tries to erase an empty list?
     if self.empty_list():
         raise ListEmpty("List is empty")
     current = self.head
     while current.next is not None:
         nxt = current.next
         self.remove_index(0) # Always delete first node
         current = nxt
     self.remove_index(0)
     return True
Ejemplo n.º 3
0
    def print_list(self):
        """ Shows list, so long as it it relatively simple """
        l_str = ""
        if not self.empty_list():
            current = self.head
            while current.next is not None:
                l_str += str(current.data) + " "
                current = current.next
            l_str += str(current.data) # Very easy to forget last value
            return l_str

        raise ListEmpty("List is empty")
Ejemplo n.º 4
0
    def get(self, index):
        """ Get value by index """
        if not self.empty_list():
            current_node = self.head
            counter = 0
            while current_node.next is not None and counter < index:
                current_node = current_node.next
                counter += 1
            if counter == index:
                return current_node.data

            raise KeyErr("Index not in list")

        raise ListEmpty("List is empty")
Ejemplo n.º 5
0
    def remove_value(self, value):
        """ Remove all instances of value """
        # Get index(es)
        counter = 0
        while True:
            try:
                index = self.search(value)
            except ListEmpty:
                raise ListEmpty("Can't search empty list")
            except ValErr:
                # Value not in list. We could be done,
                # or nothing was done.
                return counter

            # So now we know the index is there.
            self.remove_index(index)
            counter += 1
Ejemplo n.º 6
0
    def search(self, value):
        """ Traverse list, search for value. Return integer for index"""
        if not self.empty_list():
            current = self.head
            counter = 0
            while current.next is not None:
                if current.data == value:
                    # print("Value: ", value)
                    return counter
                counter += 1
                current = current.next

            if current.data == value: # Allways the last node on its own.
                return counter

            raise ValErr("Search value not in list")

        raise ListEmpty("List is empty")