Last active
August 30, 2020 14:07
-
-
Save pvn/62b5fd36131d80f14423dae769574f59 to your computer and use it in GitHub Desktop.
Linked list implementation with add, search, delete, output operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, data): | |
self.data = data | |
self.next = None | |
return | |
def has_value(self, value): | |
if self.data == value: | |
return True | |
else: | |
return False | |
class SingleLinkedList: | |
def __init__(self): | |
self.head = None | |
self.tail = None | |
return | |
def add_list_item(self, item): | |
if not isinstance(item, Node): | |
item = Node(item) | |
if self.head is None: | |
self.head = item | |
else: | |
self.tail.next = item | |
self.tail = item | |
return | |
def show_linked_list(self): | |
nodes = [] | |
current_node = self.head | |
while current_node is not None: | |
nodes.append(current_node.data) | |
current_node = current_node.next | |
print('[' + ', '.join(nodes) + ']') | |
def unordered_search(self, value): | |
node_index = 1 | |
results = [] | |
current_node = self.head | |
while current_node is not None: | |
if current_node.has_value(value): | |
results.append(node_index) | |
current_node = current_node.next | |
node_index = node_index + 1 | |
return results | |
def remove_list_item_by_id(self, item_id): | |
""" | |
Removing the item with position index from the list | |
STEP1: First set the current_id as 1, as traversing from first index | |
STEP2: Set the current node as head, as traversing from first node as well | |
STEP3: Set the previous node as None, to fill the bridge after removing the node | |
STEP4: Traverse till last node and compare position index with current_id | |
Objective: | |
First check position index with current index, if not then keep traversing by incrementing current index | |
and set current node to previous node and current node to next to current node | |
If target index is first one then your next current node would become head node, | |
so (self.head = current_node.next) & no need to look further | |
Else set previous node next pointer to current node next pointer | |
""" | |
current_id = 1 | |
current_node = self.head | |
previous_node = None | |
while current_node is not None: | |
if current_id == item_id: | |
if previous_node is not None: | |
previous_node.next = current_node.next | |
else: | |
#if this is the first node | |
self.head = current_node.next | |
#no need to look further | |
return | |
previous_node = current_node | |
current_node = current_node.next | |
current_id = current_id + 1 | |
return | |
def remove_list_item_by_value(self, value): | |
print("Deleted value is " + value) | |
results = self.unordered_search(value) | |
for node_index in results: | |
self.remove_list_item_by_id(node_index) | |
node1 = Node("15") | |
node2 = Node("16") | |
node3 = Node("Poland") | |
node4 = Node("Norway") | |
node5 = Node("Eng") | |
node6 = Node("India") | |
track = SingleLinkedList() | |
for current_item in [node1, node2, node3, node4, node5, node6]: | |
track.add_list_item(current_item) | |
track.show_linked_list() | |
searched_value = "8.2" | |
results = track.unordered_search("8.2") | |
print('Searched value ' + searched_value +' is found at ' + str(results) + ' position') | |
# track.remove_list_item_by_id(4) | |
track.remove_list_item_by_value("15") | |
track.show_linked_list() | |
track.remove_list_item_by_value("Poland") | |
track.show_linked_list() | |
track.remove_list_item_by_value("16") | |
track.show_linked_list() | |
track.remove_list_item_by_value("Norway") | |
track.show_linked_list() | |
track.remove_list_item_by_value("Eng") | |
track.show_linked_list() | |
track.remove_list_item_by_value("India") | |
track.show_linked_list() | |
''' | |
A linked list is a sequential list of node that hold data which point to other nodes also containing data. | |
''' | |
''' | |
Where are the linked lists used? | |
1]: Used in many list, queue & stack implementations. | |
2]: Great for creating circular linked lists. | |
3]: Used in separate chaining, which is present certain Hashtable implementations to deal with hashing collisions. | |
4]: Often used in the implementation of adjacent lists for graphs. | |
''' | |
''' | |
Disadvantages: | |
Memory Usage: They use more memory than arrays because of the storage used by their pointers. | |
There is no random access. Linked lists only allow sequential access. If you want to access 80th element, then you have | |
to traverse till 79th element | |
In single link list every node contains a single pointer to represent next node So we can move only forwardly. | |
Backward movement is not possible without recursion. | |
''' | |
'''Excersise to implement | |
InsertAtEnd | |
InsertAtHead | |
isEmpty | |
Reverse a linked list | |
Return Nth node from the end in a linked list | |
Remove duplicates from a linked list | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment