|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +## |
| 4 | +# Singly Linked List |
| 5 | +## |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +class Node: |
| 10 | + """ |
| 11 | + A class representing a node in a linked list. |
| 12 | +
|
| 13 | + Attributes: |
| 14 | + value (int): The value stored in the node. |
| 15 | + next (Node): A reference to the next node in the list. |
| 16 | + """ |
| 17 | + def __init__(self, value): |
| 18 | + """ |
| 19 | + Initializes the node with a given value and sets the next reference to None. |
| 20 | +
|
| 21 | + Args: |
| 22 | + value (int): The value to be stored in the node. |
| 23 | + """ |
| 24 | + self.value = value |
| 25 | + self.next = None |
| 26 | + |
| 27 | + def show_node(self): |
| 28 | + """ |
| 29 | + Prints the value of the node. |
| 30 | + """ |
| 31 | + print(self.value) |
| 32 | + |
| 33 | + |
| 34 | +class LinkedList: |
| 35 | + """ |
| 36 | + A class representing a singly linked list. |
| 37 | +
|
| 38 | + Attributes: |
| 39 | + first (Node): The first node in the linked list. |
| 40 | + """ |
| 41 | + def __init__(self): |
| 42 | + """ |
| 43 | + Initializes an empty linked list with no nodes. |
| 44 | + """ |
| 45 | + self.first = None |
| 46 | + |
| 47 | + def insert_start(self, value): |
| 48 | + """ |
| 49 | + Inserts a new node with the given value at the start of the list. |
| 50 | +
|
| 51 | + Args: |
| 52 | + value (int): The value to be inserted at the start of the list. |
| 53 | + """ |
| 54 | + new_node = Node(value) |
| 55 | + new_node.next = self.first |
| 56 | + self.first = new_node |
| 57 | + |
| 58 | + def display(self): |
| 59 | + """ |
| 60 | + Displays the values of all nodes in the list. |
| 61 | + If the list is empty, it prints a message indicating so. |
| 62 | + """ |
| 63 | + if self.first is None: |
| 64 | + print('The list is empty') |
| 65 | + return None |
| 66 | + |
| 67 | + current = self.first |
| 68 | + while current is not None: |
| 69 | + current.show_node() |
| 70 | + current = current.next |
| 71 | + |
| 72 | + def search(self, value): |
| 73 | + """ |
| 74 | + Searches for a node with the given value in the list. |
| 75 | +
|
| 76 | + Args: |
| 77 | + value (int): The value to search for. |
| 78 | +
|
| 79 | + Returns: |
| 80 | + Node: The node containing the value, or None if not found. |
| 81 | + """ |
| 82 | + if self.first is None: |
| 83 | + print('The list is empty') |
| 84 | + return None |
| 85 | + |
| 86 | + current = self.first |
| 87 | + while current.value != value: |
| 88 | + if current.next is None: |
| 89 | + return None |
| 90 | + else: |
| 91 | + current = current.next |
| 92 | + return current |
| 93 | + |
| 94 | + def delete_start(self): |
| 95 | + """ |
| 96 | + Deletes the first node from the list. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Node: The node that was deleted, or None if the list is empty. |
| 100 | + """ |
| 101 | + if self.first is None: |
| 102 | + print('The list is empty') |
| 103 | + return None |
| 104 | + |
| 105 | + temp = self.first |
| 106 | + self.first = self.first.next |
| 107 | + return temp |
| 108 | + |
| 109 | + def delete_position(self, value): |
| 110 | + """ |
| 111 | + Deletes a node with the given value from the list. |
| 112 | +
|
| 113 | + Args: |
| 114 | + value (int): The value of the node to delete. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + Node: The node that was deleted, or None if not found. |
| 118 | + """ |
| 119 | + if self.first is None: |
| 120 | + print('The list is empty') |
| 121 | + return None |
| 122 | + |
| 123 | + current = self.first |
| 124 | + previous = self.first |
| 125 | + while current.value != value: |
| 126 | + if current.next is None: |
| 127 | + return None |
| 128 | + else: |
| 129 | + previous = current |
| 130 | + current = current.next |
| 131 | + |
| 132 | + if current == self.first: |
| 133 | + self.first = self.first.next |
| 134 | + else: |
| 135 | + previous.next = current.next |
| 136 | + |
| 137 | + return current |
| 138 | + |
| 139 | + |
| 140 | +# Tests |
| 141 | +linked_list = LinkedList() |
| 142 | +linked_list.insert_start(1) |
| 143 | +linked_list.insert_start(2) |
| 144 | +linked_list.insert_start(3) |
| 145 | +linked_list.insert_start(4) |
| 146 | +linked_list.insert_start(5) |
| 147 | + |
| 148 | +linked_list.display() |
| 149 | + |
| 150 | +print(20 * '-') |
| 151 | + |
| 152 | +search_result = linked_list.search(3) |
| 153 | + |
| 154 | +linked_list.delete_start() |
| 155 | +linked_list.delete_position(4) |
| 156 | +linked_list.delete_position(2) |
| 157 | + |
| 158 | +linked_list.display() |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
0 commit comments