Upload 4.py
Browse files
4.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class DoublyLinkedBase:
|
| 2 |
+
class Node:
|
| 3 |
+
__slots__ = '_element', '_prev', '_next'
|
| 4 |
+
|
| 5 |
+
def __init__(self, element, prev, next):
|
| 6 |
+
self._element = element
|
| 7 |
+
self._prev = prev
|
| 8 |
+
self._next = next
|
| 9 |
+
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self._header = self.Node(None, None, None)
|
| 12 |
+
self._trailer = self.Node(None, None, None)
|
| 13 |
+
self._header._next = self._trailer
|
| 14 |
+
self._trailer._prev = self._header
|
| 15 |
+
self._size = 0
|
| 16 |
+
|
| 17 |
+
def __len__(self):
|
| 18 |
+
return self._size
|
| 19 |
+
|
| 20 |
+
def is_empty(self):
|
| 21 |
+
return self._size == 0
|
| 22 |
+
|
| 23 |
+
def _insert_between(self, e, predecessor, successor):
|
| 24 |
+
newest = self.Node(e, predecessor, successor)
|
| 25 |
+
predecessor._next = newest
|
| 26 |
+
successor._prev = newest
|
| 27 |
+
self._size += 1
|
| 28 |
+
|
| 29 |
+
def insert_at_position(self, pos, e):
|
| 30 |
+
if pos < 1 or pos > self._size + 1:
|
| 31 |
+
print("Invalid position")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
current = self._header
|
| 35 |
+
for _ in range(1, pos):
|
| 36 |
+
current = current._next
|
| 37 |
+
|
| 38 |
+
self._insert_between(e, current, current._next)
|
| 39 |
+
print("The element", e, "is successfully inserted at position", pos)
|
| 40 |
+
|
| 41 |
+
def _delete_node(self, node):
|
| 42 |
+
predecessor = node._prev
|
| 43 |
+
successor = node._next
|
| 44 |
+
predecessor._next = successor
|
| 45 |
+
successor._prev = predecessor
|
| 46 |
+
self._size -= 1
|
| 47 |
+
element = node._element
|
| 48 |
+
node._prev = node._next = node._element = None
|
| 49 |
+
return element
|
| 50 |
+
|
| 51 |
+
def delete_by_position(self, pos):
|
| 52 |
+
if pos < 1 or pos > self._size:
|
| 53 |
+
print("Invalid position")
|
| 54 |
+
return
|
| 55 |
+
|
| 56 |
+
current = self._header._next
|
| 57 |
+
for _ in range(1, pos):
|
| 58 |
+
current = current._next
|
| 59 |
+
|
| 60 |
+
removed = self._delete_node(current)
|
| 61 |
+
print("The element", removed, "is successfully removed")
|
| 62 |
+
|
| 63 |
+
def display(self):
|
| 64 |
+
if self.is_empty():
|
| 65 |
+
print("Doubly linked list is Empty!")
|
| 66 |
+
else:
|
| 67 |
+
current = self._header._next
|
| 68 |
+
print("Elements in Doubly linked list:")
|
| 69 |
+
while current != self._trailer:
|
| 70 |
+
print(current._element, "<->", end=" ")
|
| 71 |
+
current = current._next
|
| 72 |
+
print("NULL")
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
print("\t\tImplementation of Doubly Linked List ADT")
|
| 76 |
+
print("\t\t---------------------------------------")
|
| 77 |
+
|
| 78 |
+
dll = DoublyLinkedBase()
|
| 79 |
+
|
| 80 |
+
while True:
|
| 81 |
+
print("\n\tOPERATIONS")
|
| 82 |
+
print("1-Insertion at position")
|
| 83 |
+
print("2-Delete at position")
|
| 84 |
+
print("3-Length")
|
| 85 |
+
print("4-Is Empty")
|
| 86 |
+
print("5-Display")
|
| 87 |
+
print("6-Exit")
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
ch = int(input("Enter your choice: "))
|
| 91 |
+
|
| 92 |
+
if ch == 1:
|
| 93 |
+
value = int(input("Enter an element: "))
|
| 94 |
+
pos = int(input("Enter position: "))
|
| 95 |
+
dll.insert_at_position(pos, value)
|
| 96 |
+
|
| 97 |
+
elif ch == 2:
|
| 98 |
+
if dll.is_empty():
|
| 99 |
+
print("Doubly linked list is empty!")
|
| 100 |
+
else:
|
| 101 |
+
pos = int(input("Enter position to delete: "))
|
| 102 |
+
dll.delete_by_position(pos)
|
| 103 |
+
|
| 104 |
+
elif ch == 3:
|
| 105 |
+
print("Number of elements:", len(dll))
|
| 106 |
+
|
| 107 |
+
elif ch == 4:
|
| 108 |
+
print("Is list empty?", dll.is_empty())
|
| 109 |
+
|
| 110 |
+
elif ch == 5:
|
| 111 |
+
dll.display()
|
| 112 |
+
|
| 113 |
+
elif ch == 6:
|
| 114 |
+
print("Program Exited")
|
| 115 |
+
break
|
| 116 |
+
|
| 117 |
+
else:
|
| 118 |
+
print("Invalid choice")
|
| 119 |
+
|
| 120 |
+
except ValueError:
|
| 121 |
+
print("Please enter a valid number")
|