def print_with_iterator(l): """ Print elements of a list using an iterator. :param l: The list to be printed :type l: dict """ it = list.get_listiterator(l) while (list.hasNext(it)): print(list.next(it), end=' ') print()
def print_with_iterator_reverse_bis(l): """ Print elements of a list using an iterator in reverse order. :param l: The list to be printed :type l: dict """ it = list.get_listiterator(l, r=True) while (list.hasPrevious(it)): print(list.previous(it), end=' ') print()
def print_with_iterator(l): """ Print elements of a list using an iterator. :param l: The list to be printed :type l: dict """ l_iterator = list.get_listiterator(l) while list.hasNext(l_iterator): print(list.next(l_iterator)) return None
def print_with_iterator_reverse_bis(l): """ Print elements of a list using an iterator in reverse order. :param l: The list to be printed :type l: dict """ l_iterator = list.get_listiterator(l, from_the_end=True) while list.hasPrevious(l_iterator): print(list.previous(l_iterator)) return None
def print_with_iterator_reverse(l): """ Print elements of a list using an iterator in reverse order. :param l: The list to be printed :type l: dict """ it = list.get_listiterator(l, from_the_end=True) while list.hasPrevious(it): print(list.previous(it)['value'], end=" ") print()
def ordering_insert(l, v): """ Add *v* to list *l* such that *l* is kept ordered. :param l: An ordered list. :type l: dict :param v: The value to be inserted. :type v: same as elements of *l* """ it = list.get_listiterator(l) while list.hasNext(it) and it['successor']['value'] < v: list.next(it) list.add(it, v)
def get(l, i): """ Get the i-th element of *l*. Raise Exception if *i* is not valid. :param l: An ordered list. :type l: dict :return: the i-th element :rtype: Type of the elements of the list Throws NoSuchElementException if *i* is out of bounds. """ it = list.get_listiterator(l) for j in range(i): list.next(it) return list.next(it)
def ordering_insert(l, v): """ Add *v* to list *l* such that *l* is kept ordered. :param l: An ordered list. :type l: dict :param v: The value to be inserted. :type v: same as elements of *l* """ it = list.get_listiterator(l) ajouter = False while (list.hasNext(it) and not ajouter): if v < list.next(it): list.previous(it) list.add(it, v) ajouter = True if not ajouter: list.add(it, v)
def ordering_insert(l, v): """ Add *v* to list *l* such that *l* is kept ordered. :param l: An ordered list. :type l: dict :param v: The value to be inserted. :type v: same as elements of *l* """ if list.is_empty(l): cons(l, v) Found = True else: Found = False it = list.get_listiterator(l) while list.hasNext(it) and not Found: if v <= list.next(it): list.previous(it) list.add(it, v) Found = True if not Found: list.add(it, v) return None
if __name__ == "__main__": l = list.empty_list() for i in reversed(range(1, 5)): list.cons(l, i) list.print_list(l) # test 0 : impression renversee list.print_list(l, reverse=True) # test 1 : impression avec iterateurs print_with_iterator(l) print_with_iterator_reverse(l) # test 2 : insertion avant le 3eme element it = list.get_listiterator(l) print(list.next(it)) print(list.next(it)) list.add(it, 23) assert (list.previous(it)["value"] == 23) print_with_iterator(l) print_with_iterator_reverse(l) # test 3 : insertion apres le dernier element it = list.get_listiterator(l) while (list.hasNext(it)): list.next(it) list.add(it, 45) assert (list.previous(it)['value'] == 45) print_with_iterator(l) print_with_iterator_reverse(l)