Improved on Singly Linked List Programs (#1558)
* Improved Singly Linked List Added String Representations of Nodes and Linked Lists Added support for indexing and changing of Node data using indices. * Added a few comments to Linked Lists * Reformatted to conform to PEP8 * Added from_sequence.py Convert a Python List to Linked List comprising of Nodes and return head. * Added print_reverse.py Recursive program to print the elements of a Linked List in reverse. * Change 'is not None' for more Pythonicness
This commit is contained in:
committed by
Christian Clauss
parent
db515e585e
commit
4c37eb7d07
45
data_structures/linked_list/from_sequence.py
Normal file
45
data_structures/linked_list/from_sequence.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# Recursive Prorgam to create a Linked List from a sequence and
|
||||
# print a string representation of it.
|
||||
|
||||
class Node:
|
||||
def __init__(self, data=None):
|
||||
self.data = data
|
||||
self.next = None
|
||||
|
||||
def __repr__(self):
|
||||
"""Returns a visual representation of the node and all its following nodes."""
|
||||
string_rep = ""
|
||||
temp = self
|
||||
while temp:
|
||||
string_rep += f"<{temp.data}> ---> "
|
||||
temp = temp.next
|
||||
string_rep += "<END>"
|
||||
return string_rep
|
||||
|
||||
|
||||
|
||||
def make_linked_list(elements_list):
|
||||
"""Creates a Linked List from the elements of the given sequence
|
||||
(list/tuple) and returns the head of the Linked List."""
|
||||
|
||||
# if elements_list is empty
|
||||
if not elements_list:
|
||||
raise Exception("The Elements List is empty")
|
||||
|
||||
# Set first element as Head
|
||||
head = Node(elements_list[0])
|
||||
current = head
|
||||
# Loop through elements from position 1
|
||||
for data in elements_list[1:]:
|
||||
current.next = Node(data)
|
||||
current = current.next
|
||||
return head
|
||||
|
||||
|
||||
|
||||
list_data = [1,3,5,32,44,12,43]
|
||||
print(f"List: {list_data}")
|
||||
print("Creating Linked List from List.")
|
||||
linked_list = make_linked_list(list_data)
|
||||
print("Linked List:")
|
||||
print(linked_list)
|
||||
Reference in New Issue
Block a user