|
| 1 | +import os |
| 2 | + |
| 3 | + |
| 4 | +class _Node: |
| 5 | + ''' |
| 6 | + Creates a Node with two fields: |
| 7 | + 1. element (accesed using ._element) |
| 8 | + 2. link (accesed using ._link) |
| 9 | + ''' |
| 10 | + __slots__ = '_element', '_link' |
| 11 | + |
| 12 | + def __init__(self, element, link): |
| 13 | + ''' |
| 14 | + Initialses _element and _link with element and link respectively. |
| 15 | + ''' |
| 16 | + self._element = element |
| 17 | + self._link = link |
| 18 | + |
| 19 | + |
| 20 | +class QueueLL: |
| 21 | + ''' |
| 22 | + Consists of member funtions to perform different |
| 23 | + operations on the linked list. |
| 24 | + ''' |
| 25 | + |
| 26 | + def __init__(self): |
| 27 | + ''' |
| 28 | + Initialses head, tail and size with None, None and 0 respectively. |
| 29 | + ''' |
| 30 | + self._head = None |
| 31 | + self._tail = None |
| 32 | + self._size = 0 |
| 33 | + |
| 34 | + def __len__(self): |
| 35 | + ''' |
| 36 | + Returns length of Queue |
| 37 | + ''' |
| 38 | + return self._size |
| 39 | + |
| 40 | + def isempty(self): |
| 41 | + ''' |
| 42 | + Returns True if Queue is empty, otherwise False. |
| 43 | + ''' |
| 44 | + return self._size == 0 |
| 45 | + |
| 46 | + def enqueue(self, e): |
| 47 | + ''' |
| 48 | + Adds the passed element at the end of the linked list. |
| 49 | + That means, it enqueues element. |
| 50 | + ''' |
| 51 | + newest = _Node(e, None) |
| 52 | + |
| 53 | + if self.isempty(): |
| 54 | + self._head = newest |
| 55 | + else: |
| 56 | + self._tail._link = newest |
| 57 | + |
| 58 | + self._tail = newest |
| 59 | + self._size += 1 |
| 60 | + |
| 61 | + def dequeue(self): |
| 62 | + ''' |
| 63 | + Removes element from the beginning of the linked list and |
| 64 | + returns the removed element. |
| 65 | + That means, it dequeues. |
| 66 | + ''' |
| 67 | + if self.isempty(): |
| 68 | + print("Queue is Empty. Cannot perform dequeue operation.") |
| 69 | + return |
| 70 | + |
| 71 | + e = self._head._element |
| 72 | + self._head = self._head._link |
| 73 | + self._size = self._size - 1 |
| 74 | + |
| 75 | + if self.isempty(): |
| 76 | + self._tail = None |
| 77 | + return e |
| 78 | + |
| 79 | + def first(self): |
| 80 | + ''' |
| 81 | + Peeks and return the first element in the Queue. |
| 82 | + ''' |
| 83 | + if self.isempty(): |
| 84 | + print("Queue is Empty.") |
| 85 | + return |
| 86 | + e = self._head._element |
| 87 | + return e |
| 88 | + |
| 89 | + def display(self): |
| 90 | + ''' |
| 91 | + Utility function to display the Queue. |
| 92 | + ''' |
| 93 | + if self.isempty() == 0: |
| 94 | + p = self._head |
| 95 | + print("Front", end=' <--') |
| 96 | + while p: |
| 97 | + print(p._element, end='<--') |
| 98 | + p = p._link |
| 99 | + print(" Rear") |
| 100 | + else: |
| 101 | + print("Empty") |
| 102 | + |
| 103 | +############################################################################### |
| 104 | + |
| 105 | + |
| 106 | +def options(): |
| 107 | + ''' |
| 108 | + Prints Menu for operations |
| 109 | + ''' |
| 110 | + options_list = ['Enqueue', 'Dequeue', 'First', |
| 111 | + 'Display Queue', 'Exit'] |
| 112 | + |
| 113 | + print("MENU") |
| 114 | + for i, option in enumerate(options_list): |
| 115 | + print(f'{i + 1}. {option}') |
| 116 | + |
| 117 | + choice = int(input("Enter choice: ")) |
| 118 | + return choice |
| 119 | + |
| 120 | + |
| 121 | +def switch_case(choice): |
| 122 | + ''' |
| 123 | + Switch Case for operations |
| 124 | + ''' |
| 125 | + os.system('cls') |
| 126 | + if choice == 1: |
| 127 | + elem = int(input("Enter item to Enqueue: ")) |
| 128 | + Q.enqueue(elem) |
| 129 | + |
| 130 | + elif choice == 2: |
| 131 | + print('Dequeued item is: ', Q.dequeue()) |
| 132 | + |
| 133 | + elif choice == 3: |
| 134 | + print("First item is: ", Q.first()) |
| 135 | + |
| 136 | + elif choice == 4: |
| 137 | + print("Queue: ") |
| 138 | + Q.display() |
| 139 | + print("\n") |
| 140 | + |
| 141 | + elif choice == 5: |
| 142 | + import sys |
| 143 | + sys.exit() |
| 144 | + |
| 145 | +############################################################################### |
| 146 | + |
| 147 | + |
| 148 | +Q = QueueLL() |
| 149 | +while True: |
| 150 | + choice = options() |
| 151 | + switch_case(choice) |
0 commit comments