Linear Data Structures
These structures store data in a sequential manner.
Singly Linked List
Bases: Generic[T]
A strictly typed, memory-optimized Singly Linked List.
Source code in pure_python_ds/linear/singly_linked_list.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
__iter__()
Generator-based traversal.
Allows users to run: for val in linked_list: with O(1) memory overhead.
Source code in pure_python_ds/linear/singly_linked_list.py
55 56 57 58 59 60 61 62 63 | |
__len__()
Allows the user to call len(linked_list) in O(1) time.
Source code in pure_python_ds/linear/singly_linked_list.py
29 30 31 | |
__str__()
Provides a visual representation of the Linked List. Example: Future -> Systems -> Architect -> None
Source code in pure_python_ds/linear/singly_linked_list.py
19 20 21 22 23 24 25 26 27 | |
append(value)
Adds a new node to the end of the list in O(1) time.
Source code in pure_python_ds/linear/singly_linked_list.py
33 34 35 36 37 38 39 40 41 42 | |
prepend(value)
Adds a new node to the beginning of the list in O(1) time.
Source code in pure_python_ds/linear/singly_linked_list.py
44 45 46 47 48 49 50 51 52 53 | |
remove(value)
Removes the first occurrence of value from the list.
Source code in pure_python_ds/linear/singly_linked_list.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
remove_head()
Removes and returns the first node's value in O(1) time.
Source code in pure_python_ds/linear/singly_linked_list.py
65 66 67 68 69 70 71 72 73 74 75 76 77 | |
reverse()
Reverses the list in O(n) time and O(1) space.
Source code in pure_python_ds/linear/singly_linked_list.py
79 80 81 82 83 84 85 86 87 88 | |
search(value)
Returns True if value exists in the list, else False.
Source code in pure_python_ds/linear/singly_linked_list.py
90 91 92 93 94 95 96 97 | |
Doubly Linked List
Bases: Generic[T]
A strictly typed, memory-optimized Doubly Linked List.
Source code in pure_python_ds/linear/doubly_linked_list.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
__iter__()
Forward generator traversal.
Source code in pure_python_ds/linear/doubly_linked_list.py
80 81 82 83 84 85 | |
__str__()
Visual representation showing bidirectional pointers.
Source code in pure_python_ds/linear/doubly_linked_list.py
87 88 89 90 91 92 | |
append(value)
Adds a node to the end in O(1) time.
Source code in pure_python_ds/linear/doubly_linked_list.py
22 23 24 25 26 27 28 29 30 31 32 | |
prepend(value)
Adds a node to the beginning in O(1) time.
Source code in pure_python_ds/linear/doubly_linked_list.py
34 35 36 37 38 39 40 41 42 43 44 | |
remove(value)
Removes the first occurrence of value from the list.
Source code in pure_python_ds/linear/doubly_linked_list.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
remove_head()
Removes the first node in O(1) time.
Source code in pure_python_ds/linear/doubly_linked_list.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
remove_tail()
Removes the last node in O(1) time (Impossible in Singly Linked Lists!).
Source code in pure_python_ds/linear/doubly_linked_list.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
Stack
Bases: Generic[T]
A strictly typed, memory-optimized Stack (LIFO).
Source code in pure_python_ds/linear/stack.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
pop()
Removes and returns the top item in O(1) time.
Source code in pure_python_ds/linear/stack.py
21 22 23 | |
push(value)
Pushes an item onto the top of the stack in O(1) time.
Source code in pure_python_ds/linear/stack.py
17 18 19 | |
Queue
Bases: Generic[T]
A strictly typed, memory-optimized Queue (FIFO).
Source code in pure_python_ds/linear/queue.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
dequeue()
Removes and returns the front item in O(1) time.
Source code in pure_python_ds/linear/queue.py
21 22 23 | |
enqueue(value)
Adds an item to the back of the queue in O(1) time.
Source code in pure_python_ds/linear/queue.py
17 18 19 | |
Min / Max Heap
Bases: Generic[T]
Source code in pure_python_ds/linear/heap.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
heapify(data)
classmethod
Transforms an unsorted list into a MinHeap in O(n) time.
Source code in pure_python_ds/linear/heap.py
55 56 57 58 59 60 61 62 | |