This project serves as an exploration of the sllist topic and is not intended for global use.
The package I have developed is specifically designed for handling integer data. It does not support other data types, and therefore, no tests have been conducted using non-numeric data. If you encounter errors while using non-numeric values, please note that such behavior is expected.
The package offers a variety of functions that may cater to your needs, along with corresponding tests to help identify any errors. If you intend to modify any of the functions, it is advisable to test their functionality to ensure their effectiveness.
Detailed documentation for each function can be found in the index.py file. This documentation provides a clear understanding of each function's purpose, parameter types, input requirements, return values, and data types of the output. Additionally, the code includes brief comments alongside each function, describing its functionality and usage.
pip install SimpleSinglyLinkedList
Windows
py -m pip install --upgrade pip
Linux/MAC OS
python3 -m pip install --upgrade pip
pip install pytest
Adds the item to the end of the list
l = LinkedList() # Creating a LinkedList
l.append(1) -> [1]
l.append(2) -> [1, 2]
Adds the item after the value
l = LinkedList()
l.append(2) -> [2]
l.append_after(2,3) -> [2, 3]
# "2" is value after which we add item "3"
# if the value was not found, raises ValuerError
Adds the item before the value
l = LinkedList()
l.append(2) -> [2]
l.append_before(2,1) -> [1, 2]
# "2" is value before which we add item "1"
# if the value was not found, raises ValuerError
Adds item at the beginning of the list
l = LinkedList()
l.append(2) -> [2]
l.prepend(1) -> [1, 2]
Inserts the item at index
l = LinkedList()
l.append(1) -> [1]
l.append(3) -> [1, 3]
l.insert(1,2) -> [1, 2, 3]
# "2" is index into which item will be inserted
# if index is bigger then the length of the list, raises IndexError
Removes item from the list
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.remove(2) -> [1]
# "2" is item that removes from the list
# if the "item" was not found, raises ValueError
Removes all items from the list
l.append(1) -> [1]
l.append(1) -> [1, 1]
l.append(1) -> [1, 1, 1]
l.append(2) -> [1, 1, 1, 2]
l.remove_all(1) -> [2]
# "1" is item that removes completely
# If list is empty, raises IndexError
Removes item at a specific index
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.delete(1) -> [1, 3]
# "1" is index of the element to be deleted
# if index is out of the range, raises IndexError
Deletes value by index and return data of this value
l = LinkedList()
l.append(3) -> [3]
l.append(4) -> [3, 4]
l.append(2) -> [3, 4, 2]
l.pop(1) -> 4 , [3, 2]
# "1" is the index of the element to be deleted
# returns deleted data
# if index is out of the range, raises IndexError
Looks for the smallest number in the list
l = LinkedList()
l.append(1) -> [1]
l.append(7) -> [1, 7]
l.append(3) -> [1, 7, 3]
l.minimal() -> 1
Searches for the highest number in the list
l = LinkedList()
l.append(1) -> [1]
l.append(7) -> [1, 7]
l.append(3) -> [1, 7, 3]
l.maximal() -> 7
Returns the index of the specified number
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.append(7) -> [1, 2, 3, 7]
l.index_of(3) -> 2
# If item was not found, raises ValueError
Displays LinkedList as an array in console
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
print(l)
# outputs [1, 2, 3]
Returns all list items as if we were using a print
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
str(l) -> [1, 2, 3]
# They are the same:
print(str(l))
print(l)
Counts how many "items" are on the list
l = LinkedList()
l.append(1) -> [1]
l.append(1) -> [1, 1]
l.append(2) -> [1, 1, 2]
l.count(1) -> 2
Counts the length of the list
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.length() -> 3
Counts the length of the list but uses dunder method len
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
len(l) -> 3
Counts the length of the list but uses dunder method repr and returns it as a string
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
repr(l) -> "3"
Indicates whether the list is empty or not
l = LinkedList()
l.is_empty() -> True
l.append(1) -> [1]
l.is_empty() -> False
Indicates whether something is on the list
l = LinkedList()
bool(l) -> False
l.append(1) -> [1]
bool(l) -> True
Refers to an exact copy of the LinkedList
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
list_2 = LinkedList.copy(l)
# list_2 -> [1, 2]
Clears the list completely
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.clear() -> []
Makes a list of unique LinkedList items
l = LinkedList()
l.append(1) -> [1]
l.append(1) -> [1, 1]
l.append(1) -> [1, 1, 1]
l.append(2) -> [1, 1, 1, 2]
l.append(7) -> [1, 1, 1, 2, 7]
l.to_set() -> [1, 2, 7]
# It returns an array and not changing LinkedList
Sorts the list
l = LinkedList()
l.append(8) -> [8]
l.append(6) -> [8, 6]
l.append(5) -> [8, 6, 5]
l.append(11)-> [8, 6, 5, 11]
l.append(1) -> [8, 6, 5, 11, 1]
l.sort() -> [1, 5, 6, 8, 11]
Sorts the list by lambda condition
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.append(4) -> [1, 2, 3, 4]
l.append(5) -> [1, 2, 3, 4, 5]
condition = lambda x: x > 3
l.filter_list(condition) -> [1, 2, 3]
Moves the list to the right or left "times" times
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.shift("right", 1) -> [3, 1, 2]
# if the direction is not written correctly, raises TypeError
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.append(4) -> [1, 2, 3, 4]
l.shift("left", 2) -> [3, 4, 1, 2]
# if the direction is not written correctly, raises TypeError
Reverses the list
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.append(4) -> [1, 2, 3, 4]
l.reverse() -> [4, 3, 2, 1]
Returns the sum of all the elements
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
l.append(4) -> [1, 2, 3, 4]
l.reduce_list() -> 10
Lengths of lists are compared
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_1.append(3) -> [1, 2, 3]
list_2 = LinkedList.copy(list_1) -> [1, 2, 3]
list_1 < list_2 # False, 3 < 3
list_1 <= list_2 # True, 3 <= 3
list_2.append(4) -> [1, 2, 3, 4]
list_1 < list_2 # True, 3 < 4
Lengths of lists are compared
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_1.append(3) -> [1, 2, 3]
list_2 = LinkedList.copy(list_1) -> [1, 2, 3]
list1 > list_2 # False, 3 > 3
list1 >= list_2 # True 3 >= 3
list_1.append(4) -> [1, 2, 3, 4]
list_1 > list_2 # True, 4 > 3
"==" compares each item in the list and if the lists are perfectly equal returns True
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_2 = LinkedList.copy(list_1) -> [1, 2]
list_1 == list_2(2) # True, 1 == 1, 2 == 2
# =========================================
list_1 = LinkedList()
list_1.append(1)
list_1.append(2)
list_2 = LinkedList()
list_2.append(2)
list_2.append(3)
list_1 == list_2 # False, 1 == 2, 2 == 3
"!=" compares each item in the list and if the lists are perfectly equal returns False
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_2 = LinkedList.copy(list_1) -> [1, 2]
list_1 != list_2(2) # False, 1 != 1, 2 != 2
# =========================================
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_2 = LinkedList()
list_2.append(2) -> [2]
list_2.append(3) -> [2, 3]
list_1 != list_2 # True, 1 != 2, 2 != 3
Concatenate the LinkedList with another LinkedList
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_2 = LinkedList.copy(list_1) -> [1, 2]
list_1 += list_2
list_1 -> [1, 2, 1, 2]
# if list_2 is not LinkedList, raises TypeError
list_1 = LinkedList()
list_1.append(1) -> [1]
list_1.append(2) -> [1, 2]
list_2 = LinkedList.copy(list_1) -> [1, 2]
list_3 = list_1 + list_2
list_3 -> [1, 2, 1, 2]
# if list_2 is not LinkedList, raises TypeError
Repeats the LinkedList n number of times
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l = l * 3
l -> [1, 2, 1, 2, 1, 2]
# if 3 not int TypeError will be raised
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l[1] -> 2
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l[1] = 3
l -> [1, 3]
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
del l[1]
l -> [1]
Goes through each element using the yield
l = LinkedList()
l.append(1) -> [1]
l.append(2) -> [1, 2]
l.append(3) -> [1, 2, 3]
x = sum(node.data for node in l.__iter__()) -> 6