Problem 4¶
Implement a linked list which stores integers.
Provide the following operations:
add_first
,remove_first
,remove_last
,clear
,is_empty
,get
(at index),set
(at index), anditerate
(over all the elements of the list).
(We substantially shortened the text of this problem to only give the crux of the problem and leave out the parts with user input/output. Please refer to the original exercise in German for exact details.)
Classes:
|
Represent a node of a linked list containing integers. |
|
Provide a cursor to iterate manually over a linked list. |
|
Provide a linked list. |
- class Node(value: int, next_node: Optional[Node])[source]¶
Represent a node of a linked list containing integers.
Methods:
__init__
(value, next_node)
- class Cursor(linked_list: LinkedList)[source]¶
Provide a cursor to iterate manually over a linked list.
Methods:
__init__
(linked_list)Initialize the cursor to point to the beginning of the
linked_list
.value
()Retrieve the value point to by the cursor.
set_value
(value)Set the value in the linked list at the cursor.
move
()Move the cursor to the next element.
done
()Return
True
if the cursor is past the end of linked list.is_last
()Return
True
if the cursor points to the last element of the list.remove
()Remove the node (and the value, respectively) at the cursor.
- __init__(linked_list: LinkedList) None [source]¶
Initialize the cursor to point to the beginning of the
linked_list
.
- class LinkedList(values: Optional[Iterable[int]] = None)[source]¶
Provide a linked list.
- Establishes
len(list(self.values())) == self.count()
self._last is not None
⇒self._last.next_node is None
len(list(self.values())) != 0 ^ self.is_empty()
self.is_empty() ^ (self._first is not None and self._last is not None)
Methods:
__init__
([values])Initialize the list by populating it with the given
values
.add_first
(value)Prepend the
value
to the list.add_last
(value)Append the
value
to the list.count
()Remove first element of the list.
Remove the last element of the list.
clear
()Remove all elements in the list.
is_empty
()get
(index)Retrieve the
index
-th element of the list.set
(index, value)Set the
index
-th element tovalue
.values
()Iterate over all the values in the list.
cursor
()Get a cursor pointing to the beginning of the list.
- __init__(values: Optional[Iterable[int]] = None) None [source]¶
Initialize the list by populating it with the given
values
.
- add_first(value: int) None [source]¶
Prepend the
value
to the list.- OLD
.values
=list(self.values())
.count
=self.count()
- Ensures
self.count() == OLD.count + 1
not self.is_empty()
[value] + OLD.values == list(self.values())
- add_last(value: int) None [source]¶
Append the
value
to the list.- OLD
.values
=list(self.values())
.count
=self.count()
- Ensures
self.count() == OLD.count + 1
not self.is_empty()
OLD.values + [value] == list(self.values())
- remove_first() int [source]¶
Remove first element of the list.
- Requires
not self.is_empty()
- OLD
.count
=self.count()
.values
=list(self.values())
- Ensures
OLD.values[0] == result
self.count() == OLD.count - 1
OLD.values[1:] == list(self.values())
- remove_last() int [source]¶
Remove the last element of the list.
- Requires
not self.is_empty()
- OLD
.values
=list(self.values())
.count
=self.count()
- Ensures
OLD.values[-1] == result
self.count() == OLD.count - 1
OLD.values[:-1] == list(self.values())
- clear() None [source]¶
Remove all elements in the list.
- Requires
not self.is_empty()
- Ensures
self.count() == 0
self.is_empty()
- get(index: int) int [source]¶
Retrieve the
index
-th element of the list.- Requires
0 <= index < self.count()
- Ensures
list(self.values())[index] == result