data_structures.linked_list.singly_linked_list¶
Classes¶
Create and initialize Node class instance. |
Functions¶
|
|
|
|
|
This section of the test used varying data types for input. |
Module Contents¶
- class data_structures.linked_list.singly_linked_list.LinkedList¶
- __getitem__(index: int) Any ¶
Indexing Support. Used to get a node at particular position >>> linked_list = LinkedList() >>> for i in range(0, 10): … linked_list.insert_nth(i, i) >>> all(str(linked_list[i]) == str(i) for i in range(0, 10)) True >>> linked_list[-10] Traceback (most recent call last):
…
ValueError: list index out of range. >>> linked_list[len(linked_list)] Traceback (most recent call last):
…
ValueError: list index out of range.
- __iter__() collections.abc.Iterator[Any] ¶
This function is intended for iterators to access and iterate through data inside linked list. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“tail”) >>> linked_list.insert_tail(“tail_1”) >>> linked_list.insert_tail(“tail_2”) >>> for node in linked_list: # __iter__ used here. … node ‘tail’ ‘tail_1’ ‘tail_2’
- __len__() int ¶
Return length of linked list i.e. number of nodes >>> linked_list = LinkedList() >>> len(linked_list) 0 >>> linked_list.insert_tail(“tail”) >>> len(linked_list) 1 >>> linked_list.insert_head(“head”) >>> len(linked_list) 2 >>> _ = linked_list.delete_tail() >>> len(linked_list) 1 >>> _ = linked_list.delete_head() >>> len(linked_list) 0
- __repr__() str ¶
String representation/visualization of a Linked Lists >>> linked_list = LinkedList() >>> linked_list.insert_tail(1) >>> linked_list.insert_tail(3) >>> linked_list.__repr__() ‘1 -> 3’ >>> repr(linked_list) ‘1 -> 3’ >>> str(linked_list) ‘1 -> 3’ >>> linked_list.insert_tail(5) >>> f”{linked_list}” ‘1 -> 3 -> 5’
- __setitem__(index: int, data: Any) None ¶
>>> linked_list = LinkedList() >>> for i in range(0, 10): ... linked_list.insert_nth(i, i) >>> linked_list[0] = 666 >>> linked_list[0] 666 >>> linked_list[5] = -666 >>> linked_list[5] -666 >>> linked_list[-10] = 666 Traceback (most recent call last): ... ValueError: list index out of range. >>> linked_list[len(linked_list)] = 666 Traceback (most recent call last): ... ValueError: list index out of range.
- delete_head() Any ¶
Delete the first node and return the node’s data. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“first”) >>> linked_list.insert_tail(“second”) >>> linked_list.insert_tail(“third”) >>> linked_list first -> second -> third >>> linked_list.delete_head() ‘first’ >>> linked_list second -> third >>> linked_list.delete_head() ‘second’ >>> linked_list third >>> linked_list.delete_head() ‘third’ >>> linked_list.delete_head() Traceback (most recent call last):
…
IndexError: List index out of range.
- delete_nth(index: int = 0) Any ¶
Delete node at given index and return the node’s data. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“first”) >>> linked_list.insert_tail(“second”) >>> linked_list.insert_tail(“third”) >>> linked_list first -> second -> third >>> linked_list.delete_nth(1) # delete middle ‘second’ >>> linked_list first -> third >>> linked_list.delete_nth(5) # this raises error Traceback (most recent call last):
…
IndexError: List index out of range. >>> linked_list.delete_nth(-1) # this also raises error Traceback (most recent call last):
…
IndexError: List index out of range.
- delete_tail() Any ¶
Delete the tail end node and return the node’s data. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“first”) >>> linked_list.insert_tail(“second”) >>> linked_list.insert_tail(“third”) >>> linked_list first -> second -> third >>> linked_list.delete_tail() ‘third’ >>> linked_list first -> second >>> linked_list.delete_tail() ‘second’ >>> linked_list first >>> linked_list.delete_tail() ‘first’ >>> linked_list.delete_tail() Traceback (most recent call last):
…
IndexError: List index out of range.
- insert_head(data: Any) None ¶
Insert data to the beginning of linked list. >>> linked_list = LinkedList() >>> linked_list.insert_head(“head”) >>> linked_list head >>> linked_list.insert_head(“head_2”) >>> linked_list head_2 -> head >>> linked_list.insert_head(“head_3”) >>> linked_list head_3 -> head_2 -> head
- insert_nth(index: int, data: Any) None ¶
Insert data at given index. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“first”) >>> linked_list.insert_tail(“second”) >>> linked_list.insert_tail(“third”) >>> linked_list first -> second -> third >>> linked_list.insert_nth(1, “fourth”) >>> linked_list first -> fourth -> second -> third >>> linked_list.insert_nth(3, “fifth”) >>> linked_list first -> fourth -> second -> fifth -> third
- insert_tail(data: Any) None ¶
Insert data to the end of linked list. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“tail”) >>> linked_list tail >>> linked_list.insert_tail(“tail_2”) >>> linked_list tail -> tail_2 >>> linked_list.insert_tail(“tail_3”) >>> linked_list tail -> tail_2 -> tail_3
- is_empty() bool ¶
Check if linked list is empty. >>> linked_list = LinkedList() >>> linked_list.is_empty() True >>> linked_list.insert_head(“first”) >>> linked_list.is_empty() False
- print_list() None ¶
This method prints every node data. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“first”) >>> linked_list.insert_tail(“second”) >>> linked_list.insert_tail(“third”) >>> linked_list first -> second -> third
- reverse() None ¶
This reverses the linked list order. >>> linked_list = LinkedList() >>> linked_list.insert_tail(“first”) >>> linked_list.insert_tail(“second”) >>> linked_list.insert_tail(“third”) >>> linked_list first -> second -> third >>> linked_list.reverse() >>> linked_list third -> second -> first
- head = None¶
- class data_structures.linked_list.singly_linked_list.Node¶
Create and initialize Node class instance. >>> Node(20) Node(20) >>> Node(“Hello, world!”) Node(Hello, world!) >>> Node(None) Node(None) >>> Node(True) Node(True)
- __repr__() str ¶
Get the string representation of this node. >>> Node(10).__repr__() ‘Node(10)’ >>> repr(Node(10)) ‘Node(10)’ >>> str(Node(10)) ‘Node(10)’ >>> Node(10) Node(10)
- data: Any¶
- data_structures.linked_list.singly_linked_list.main()¶
- data_structures.linked_list.singly_linked_list.test_singly_linked_list() None ¶
>>> test_singly_linked_list()
- data_structures.linked_list.singly_linked_list.test_singly_linked_list_2() None ¶
This section of the test used varying data types for input. >>> test_singly_linked_list_2()