|
| 1 | +import os |
| 2 | +from typing import Sized |
| 3 | + |
| 4 | +class _Node: |
| 5 | + __slots__ = '_element', '_link' |
| 6 | + |
| 7 | + def __init__(self, element, link): |
| 8 | + self._element = element |
| 9 | + self._link = link |
| 10 | + |
| 11 | +class LinkedList: |
| 12 | + def __init__(self): |
| 13 | + self._head = None |
| 14 | + self._tail = None |
| 15 | + self._size = 0 |
| 16 | + |
| 17 | + def __len__(self): |
| 18 | + return self._size |
| 19 | + |
| 20 | + def isempty(self): |
| 21 | + return self._size == 0 |
| 22 | + |
| 23 | + def addLast(self, e): |
| 24 | + newest = _Node(e,None) |
| 25 | + |
| 26 | + if self.isempty(): |
| 27 | + self._head = newest |
| 28 | + else: |
| 29 | + self._tail._link = newest |
| 30 | + |
| 31 | + self._tail = newest |
| 32 | + self._size += 1 |
| 33 | + |
| 34 | + def addFirst(self, e): |
| 35 | + |
| 36 | + newest = _Node(e, None) |
| 37 | + |
| 38 | + if self.isempty(): |
| 39 | + self._head = newest |
| 40 | + self._tail = newest |
| 41 | + else: |
| 42 | + newest._link = self._head |
| 43 | + self._head = newest |
| 44 | + self._size += 1 |
| 45 | + |
| 46 | + def addAnywhere(self, e, index): |
| 47 | + |
| 48 | + newest = _Node(e, None) |
| 49 | + |
| 50 | + i = index - 1 |
| 51 | + p = self._head |
| 52 | + |
| 53 | + if self.isempty(): |
| 54 | + self.addFirst(e) |
| 55 | + else: |
| 56 | + for i in range(i): |
| 57 | + p = p._link |
| 58 | + newest._link = p._link |
| 59 | + p._link = newest |
| 60 | + self._size += 1 |
| 61 | + |
| 62 | + def display(self): |
| 63 | + if self.isempty() == 0: |
| 64 | + p = self._head |
| 65 | + while p: |
| 66 | + print(p._element, end='-->') |
| 67 | + p = p._link |
| 68 | + print("NULL") |
| 69 | + else: |
| 70 | + print("List is Empty") |
| 71 | + |
| 72 | + def search(self, key): |
| 73 | + p = self._head |
| 74 | + index = 0 |
| 75 | + while p: |
| 76 | + if p._element == key: |
| 77 | + return index |
| 78 | + p = p._link |
| 79 | + index += 1 |
| 80 | + return -1 |
| 81 | + |
| 82 | +############################################################################### |
| 83 | + |
| 84 | +def options(): |
| 85 | + options_list = ['Add item', 'Add First', 'Add Anywhere', 'Display List', 'Print Size', 'Search', 'Exit'] |
| 86 | + |
| 87 | + print("MENU") |
| 88 | + for i, option in enumerate(options_list): |
| 89 | + print(f'{i + 1}. {option}') |
| 90 | + |
| 91 | + choice = int(input("Enter choice: ")) |
| 92 | + return choice |
| 93 | + |
| 94 | +def switch_case(choice): |
| 95 | + |
| 96 | + os.system('cls') |
| 97 | + if choice == 1: |
| 98 | + elem = int(input("Enter Item: ")) |
| 99 | + L.addLast(elem) |
| 100 | + print("Added Item at Last!\n\n") |
| 101 | + elif choice == 2: |
| 102 | + elem = int(input("Enter Item: ")) |
| 103 | + L.addFirst(elem) |
| 104 | + print("Added Item at First!\n\n") |
| 105 | + elif choice == 3: |
| 106 | + elem = int(input("Enter Item: ")) |
| 107 | + index = int(input("Enter Index: ")) |
| 108 | + L.addAnywhere(elem, index) |
| 109 | + print(f"Added Item at index {index}!\n\n") |
| 110 | + elif choice == 4: |
| 111 | + print("List: ", end='') |
| 112 | + L.display() |
| 113 | + print("\n") |
| 114 | + elif choice == 5: |
| 115 | + print("Size:", len(L)) |
| 116 | + print("\n") |
| 117 | + elif choice == 6: |
| 118 | + key = int(input("Enter item to search: ")) |
| 119 | + if L.search(key) >= 0: |
| 120 | + print(f"Item {key} found at index position {L.search(key)}\n\n") |
| 121 | + else: |
| 122 | + print("Item not in the list\n\n") |
| 123 | + |
| 124 | + elif choice == 7: |
| 125 | + exit |
| 126 | + |
| 127 | + |
| 128 | +L = LinkedList() |
| 129 | +while True: |
| 130 | + choice = options() |
| 131 | + switch_case(choice) |
0 commit comments