From 39a30cb258ea13b261cf9a7b3e187c15391a6bcf Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Tue, 1 Oct 2024 21:08:58 +0530 Subject: [PATCH 1/8] Linked Lists --- ...ven_Code_for_Circular_Doubly_LinkedList.py | 140 +++++++++++++++++ ...enu_Driven_Code_for_Circular_LinkedList.py | 109 ++++++++++++++ .../Menu_Driven_Code_for_Doubly_LinkedList.py | 127 ++++++++++++++++ ...r_Dynamic_Linear_Queue_using_LinkedList.py | 67 +++++++++ ...Code_for_Dynamic_Stack_using_LinkedList.py | 74 +++++++++ .../Menu_Driven_Code_for_Linear_LinkedList.py | 141 ++++++++++++++++++ .../Linked List/README.md | 65 ++++++++ 7 files changed, 723 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py create mode 100644 Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py create mode 100644 Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py create mode 100644 Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py create mode 100644 Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py create mode 100644 Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py create mode 100644 Algorithms_and_Data_Structures/Linked List/README.md diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py new file mode 100644 index 00000000..e11c0591 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py @@ -0,0 +1,140 @@ +""" +# Menu Driven Code for Circular Doubly Linked List +""" + + +class Node: + def __init__(self, data): + self.data = data + self.right = None + self.left = None + + +class LinkedList: + def __init__(self): + self.root = None + self.last = None + + def insertLeft(self, data): + n = Node(data) + if self.root == None: + self.root = n + self.last = n + self.last.right = self.root + self.root.left = self.last + else: + n.right = self.root + self.root.left = n + self.root = n + self.last.right = self.root + self.root.left = self.last + print('\nInserted Element: ', self.root.data) + self.printList() + + def deleteLeft(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + temp = self.root + if self.root == self.last: + self.root = None + else: + self.root = self.root.right + self.root.left = self.last + self.last.right = self.root + print('\nDeleted element: ', temp.data) + self.printList() + + def insertRight(self, data): + n = Node(data) + if self.root == None: + self.root = n + self.last = n + self.last.right = self.root + self.root.left = self.last + else: + self.last.right = n + n.left = self.last + self.last = n + self.last.right = self.root + self.root.left = self.last + print('\nInserted Element: ', n.data) + self.printList() + + def deleteRight(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + if self.root == self.last: + self.root = None + else: + print('Deleted Element: ', self.last.data) + self.last = self.last.left + self.last.right = self.root + self.root.left = self.last + self.printList() + + def printList(self): + if self.root == None: + print('\nLinked List is empty..!!') + return + temp = self.root + print('\nElements in Linked List are: ') + while True: + print('|', temp.data, '| <-> ', end='') + temp = temp.right + if temp == self.root: + break + print('Root') + print() + + def printReverseList(self): + if self.root == None: + print('\nLinked List is empty..!!') + return + temp = self.last + print('\nElements in Linked List are: ') + while True: + print('|', temp.data, '| <-> ', end='') + temp = temp.left + if temp == self.last: + break + print('Last') + print() + + +o = LinkedList() + +while True: + print('----------------------') + print('\n1. Insert from Left\n2. Insert from Right\n3. Delete from Left\n4. Delete from Right\n5. Print Linked List\n6. Print Reverse Linked List\n0. Exit') + print('----------------------') + + ch = int(input('\nEnter your choice: ')) + + if ch == 1: + data = int(input('\nEnter value to be inserted in left: ')) + o.insertLeft(data) + + elif ch == 2: + data = int(input('\nEnter value to be inserted in right: ')) + o.insertRight(data) + + elif ch == 3: + o.deleteLeft() + + elif ch == 4: + o.deleteRight() + + elif ch == 5: + o.printList() + + elif ch == 6: + o.printReverseList() + + elif ch == 0: + print('You are out of the program..!!') + break + + else: + print('\nWrong Input..\nEnter the correct choice..!!\n') diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py new file mode 100644 index 00000000..83e8c999 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py @@ -0,0 +1,109 @@ + + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.root = None + self.last = None + + def insertLeft(self, data): + n = Node(data) + if self.root == None: + self.root = n + self.last = n + self.last.next = self.root + else: + n.next = self.root + self.root = n + self.last.next = self.root + + def deleteLeft(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + temp = self.root + if self.root == self.last: + self.last = self.root = None + else: + self.root = self.root.next + self.last.next = self.root + print('\nDeleted element: ', temp.data) + + def insertRight(self, data): + n = Node(data) + if self.root == None: + self.root = n + self.last = n + self.last.next = self.root + else: + self.last.next = n + self.last = n + self.last.next = self.root + + def deleteRight(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + if self.root == self.last: + self.root = self.last = None + else: + temp = self.root + temp2 = self.root + while temp.next != self.root: + temp2 = temp + temp = temp.next + self.last = temp2 + temp2.next = self.root + print('\nDeleted element: ', temp.data) + + def printList(self): + if self.root == None: + print('\nLinked List is empty..!!') + temp = self.root + print('\nElements in Linked List are: ') + while True: + print('|', temp.data, '| -> ', end='') + temp = temp.next + if temp == self.root: + break + print('None') + print() + + +o = LinkedList() + +while True: + print('----------------------') + print('\n1. Insert from Left\n2. Insert from Right\n3. Delete from Left\n4. Delete from Right\n5. Print Linked List\n0. Exit') + print('----------------------') + + ch = int(input('\nEnter your choice: ')) + + if ch == 1: + data = int(input('\nEnter value to be inserted in left: ')) + o.insertLeft(data) + + elif ch == 2: + data = int(input('\nEnter value to be inserted in right: ')) + o.insertRight(data) + + elif ch == 3: + o.deleteLeft() + + elif ch == 4: + o.deleteRight() + + elif ch == 5: + o.printList() + + elif ch == 0: + print('You are out of the program..!!') + break + + else: + print('\nWrong Input..\nEnter the correct choice..!!\n') diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py new file mode 100644 index 00000000..5116ab95 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py @@ -0,0 +1,127 @@ + + +class Node: + def __init__(self, data): + self.data = data + self.right = None + self.left = None + + +class LinkedList: + def __init__(self): + self.root = None + self.last = None + + def insertLeft(self, data): + n = Node(data) + if self.root == None: + self.root = n + + else: + n.right = self.root + self.root.left = n + self.root = n + + def deleteLeft(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + temp = self.root + if self.root.right == self.root.left == None: + self.root = None + else: + self.root = self.root.right + print('Deleted: ', temp.data) + + def insertRight(self, data): + n = Node(data) + if self.root == None: + self.root = n + else: + temp = self.root + while temp.right != None: + temp = temp.right + temp.right = n + n.left = temp + n.right = None + print('Inserted Element: ', n.data) + + def deleteRight(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + if self.root.right == self.root.left: + self.root = None + else: + temp = self.root + while temp.right != None: + temp = temp.right + print('\nDeleted: ', temp.data) + temp = temp.left + temp.right = None + + def printList(self): + if self.root == None: + print('\nLinked List is empty..!!') + return + else: + temp = self.root + print('Elements of linked list are: ') + while temp != None: + print('|', temp.data, ' <-> ', end=" ") + temp = temp.right + print('None') + print('') + + def printListReverse(self): + if self.root == None: + print('\nLinked List is empty..!!') + return + else: + temp = self.root + print('Elements of linked list are: ') + while temp.right != None: + temp = temp.right + while temp != None: + print('|', temp.data, ' <-> ', end=" ") + temp = temp.left + + print('None') + print('') + + +o = LinkedList() + +while True: + print('----------------------') + print('\n1. Insert from Left\n2. Insert from Right\n3. Delete from Left\n4. Delete from Right\n5. Print Linked List\n6. Print Reverse Linked List\n0. Exit') + print('----------------------') + + ch = int(input('\nEnter your choice: ')) + + if ch == 1: + data = int(input('\nEnter value to be inserted in left: ')) + o.insertLeft(data) + + elif ch == 2: + data = int(input('\nEnter value to be inserted in right: ')) + o.insertRight(data) + + elif ch == 3: + o.deleteLeft() + + elif ch == 4: + o.deleteRight() + + elif ch == 5: + o.printList() + + elif ch == 6: + o.printListReverse() + + elif ch == 0: + print('You are out of the program..!!') + break + + else: + print('\nWrong Input..\nEnter the correct choice..!!\n') diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py new file mode 100644 index 00000000..a3619fa1 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py @@ -0,0 +1,67 @@ + + + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +class DynamicQueue: + def __init__(self): + self.front = None + self.rear = None + + def enqueue(self, data): + n = Node(data) + if self.front == None: + self.front = self.rear = n + else: + self.rear.next = n + self.rear = n + print('\nElement Enqueued in Queue: ', data) + + def dequeue(self): + if self.front == None: + print('\nQueue is empty..!!') + else: + temp = self.front + self.front = self.front.next + print('\nElement Dequeued from Queue: ', temp.data) + + def printQueue(self): + if self.front == None: + print('\nQueue is empty..!!') + else: + temp = self.front + while temp != None: + print(temp.data, ' --> ', end='') + temp = temp.next + print() + + +o = DynamicQueue() + +while True: + print('-----------') + print('\n1. Enqueue\n2. Dequeue\n3. Print\n0. Exit') + print('-----------') + + ch = int(input('\nEnter your choice: ')) + + if ch == 1: + data = int(input('\nEnter value to enqueue in Queue: ')) + o.enqueue(data) + + elif ch == 2: + o.dequeue() + + elif ch == 3: + o.printQueue() + + elif ch == 0: + print('You are out of the program..!!') + break + + else: + print('\nWrong Input..\nEnter the correct choice..!!\n') diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py new file mode 100644 index 00000000..a47251e8 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py @@ -0,0 +1,74 @@ + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +class DynamicStack: + def __init__(self): + self.tos = None + + def push(self, data): + n = Node(data) + if self.tos == None: + self.tos = n + else: + n.next = self.tos + self.tos = n + + def pop(self): + if self.tos == None: + print('\nStack is empty..!!') + else: + temp = self.tos + self.tos = self.tos.next + print('Popped Element from Stack: ', temp.data) + + def peek(self): + if self.tos == None: + print('\nStack is empty..!!') + else: + print('Peeked Element: ', self.tos.data) + + def printStack(self): + if self.tos == None: + print('\nStack is empty..!!') + else: + print('Stack Data') + temp = self.tos + while temp != None: + print(temp.data) + temp = temp.next + +# Main Code + + +o = DynamicStack() + +while True: + print('-----------') + print('\n1. Push\n2. Pop\n3. Peek\n4. Print\n0. Exit') + print('-----------') + + ch = int(input('\nEnter your choice: ')) + + if ch == 1: + data = int(input('\nEnter value to push in stack: ')) + o.push(data) + + elif ch == 2: + o.pop() + + elif ch == 3: + o.peek() + + elif ch == 4: + o.printStack() + + elif ch == 0: + print('You are out of the program..!!') + break + + else: + print('\nWrong Input..\nEnter the correct choice..!!\n') diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py new file mode 100644 index 00000000..7c945544 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py @@ -0,0 +1,141 @@ + + +class Node: + def __init__(self, data): + self.data = data + self.next = None + + +class LinkedList: + def __init__(self): + self.root = None + + def insertLeft(self, data): + n = Node(data) + if self.root == None: + self.root = n + else: + n.next = self.root + self.root = n + + def deleteLeft(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + temp = self.root + self.root = self.root.next + print('\nDeleted element: ', temp.data) + + def insertRight(self, data): + n = Node(data) + if self.root == None: + self.root = n + else: + temp = self.root + while temp.next != None: + temp = temp.next + temp.next = n + + def deleteRight(self): + if self.root == None: + print('\nLinked List is empty..!!') + else: + temp = self.root + temp2 = self.root + while temp.next != None: + temp2 = temp + temp = temp.next + temp2.next = None + if temp == self.root: + self.root = None + print('\nDeleted element: ', temp.data) + + def printList(self): + if self.root == None: + print('\nLinked List is empty..!!') + temp = self.root + print('\nElements in Linked List are: ') + while temp != None: + print('|', temp.data, '| -> ', end='') + temp = temp.next + print('None') + print() + + def searchList(self, data): + if self.root == None: + print('\nLinked List is empty..!!') + else: + count = 0 + temp = self.root + while temp != None: + if temp.data == data: + print('\nElement', data, 'found at node: ', count) + return + temp = temp.next + count += 1 + return ('\nElement not found') + + def deleteElement(self, data): + if self.root == None: + print('\nLinked List is empty..!!') + else: + count = 0 + temp = self.root + temp2 = self.root + while temp != None and temp.data != data: + temp2 = temp + temp = temp.next + count += 1 + if temp != None: + if temp == self.root: + self.root = self.root.next + elif temp.next == None: + temp2.next = None + else: + temp2.next = temp.next + print('\nDeleted Element:', temp.data, + 'from position: ', count) + else: + print(data, 'not found in Linked List') + + +o = LinkedList() + +while True: + print('----------------------') + print('\n1. Insert from Left\n2. Insert from Right\n3. Delete from Left\n4. Delete from Right\n5. Delete Element x\n6. Print Linked List\n7. Search Element x\n0. Exit') + print('----------------------') + + ch = int(input('\nEnter your choice: ')) + + if ch == 1: + data = int(input('\nEnter value to be inserted in left: ')) + o.insertLeft(data) + + elif ch == 2: + data = int(input('\nEnter value to be inserted in right: ')) + o.insertRight(data) + + elif ch == 3: + o.deleteLeft() + + elif ch == 4: + o.deleteRight() + + elif ch == 5: + x = int(input('\nEnter the value of Element x: ')) + o.deleteElement(x) + + elif ch == 6: + o.printList() + + elif ch == 7: + data = int(input('Enter the value of Element x: ')) + o.searchList(data) + + elif ch == 0: + print('You are out of the program..!!') + break + + else: + print('\nWrong Input..\nEnter the correct choice..!!\n') diff --git a/Algorithms_and_Data_Structures/Linked List/README.md b/Algorithms_and_Data_Structures/Linked List/README.md new file mode 100644 index 00000000..19a3c6ff --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/README.md @@ -0,0 +1,65 @@ +# **Linked List** +Linked lists are a fundamental data structure that allows efficient storage and manipulation of data + + +## **Explanation** + +1. ### **Linear Linked List** + A linear linked list consists of a sequence of nodes, where each node contains data and a pointer to the next node. The menu-driven code for a linear linked list typically includes options to perform operations such as inserting a node, deleting a node, searching for a specific value, displaying the list, and exiting the program. + + 🔗 View here: [Linear Linked List](./Menu_Driven_Code_for_Linear_LinkedList.py) + + +2. ### **Doubly Linked List** + A doubly linked list extends the concept of a linear linked list by including an additional pointer in each node, pointing to the previous node. The menu-driven code for a doubly linked list provides options for inserting, deleting, searching, displaying, and exiting, similar to a linear linked list. However, due to the presence of backward links, the code must handle operations involving both forward and backward traversal. + + 🔗 View here: [Doubly Linked List](./Menu_Driven_Code_for_Doubly_LinkedList.py) + + +3. ### **Circular Linked List** + In a circular linked list, the last node points back to the first node, creating a circular structure. The menu-driven code for a circular linked list offers operations such as insertion, deletion, searching, displaying, and exiting. Care must be taken to correctly handle the circular nature of the list when performing operations like traversal or deletion. + + 🔗 View here: [Circular Linked List](./Menu_Driven_Code_for_Circular_LinkedList.py) + + +4. ### **Circular Doubly Linked List** + A circular doubly linked list combines the properties of a circular linked list and a doubly linked list. Each node has a forward and backward pointer, and the last node connects back to the first node. The menu-driven code for a circular doubly linked list allows operations like insertion, deletion, searching, displaying, and exiting, taking into account both forward and backward traversal. + + 🔗 View here: [Circular Doubly Linked List](./Menu_Driven_Code_for_Circular_Doubly_LinkedList.py) + + +5. ### **Dynamic Linear Queue using Linked List** + A dynamic linear queue implemented using a linked list provides a queue data structure with dynamic memory allocation. The menu-driven code for this structure includes options for enqueueing (adding) elements, dequeuing (removing) elements, displaying the queue, checking if it is empty, checking if it is full, and exiting. + + 🔗 View here: [Dynamic Linear Queue using Linked List](./Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py) + + +6. ### **Dynamic Stack using Linked List** + A dynamic stack implemented using a linked list allows the creation of a stack with dynamic memory allocation. The menu-driven code for a dynamic stack provides options for pushing (adding) elements, popping (removing) elements, displaying the stack, checking if it is empty, checking if it is full, and exiting. + + 🔗 View here: [Dynamic Stack using Linked List](./Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py) + + +In each of these menu-driven linked list codes, you willbe presented with a menu of options to choose from, and the corresponding operations are performed based on their selections. This approach offers a convenient and user-friendly way to interact with linked lists and perform common operations without needing to write custom code for each functionality. + + +## **Setup Instructions** + +1. Install Python +2. Verify Python Installation + + ```bash + python --version + ``` + +3. Run the Python Script + ```bash + python filename.py + ``` + + Note: Replace `filename.py` with the name of the python file which is to be executed. + + +## **Author** + +- [Himanshu Agarwal](https://github.com/himanshu-03) \ No newline at end of file From db2f6e3ebe2e45d46209e6620c03d590c5bcbdaa Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Tue, 1 Oct 2024 21:09:45 +0530 Subject: [PATCH 2/8] Update README.md --- Algorithms_and_Data_Structures/Linked List/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/README.md b/Algorithms_and_Data_Structures/Linked List/README.md index 19a3c6ff..b417d5ab 100644 --- a/Algorithms_and_Data_Structures/Linked List/README.md +++ b/Algorithms_and_Data_Structures/Linked List/README.md @@ -59,7 +59,3 @@ In each of these menu-driven linked list codes, you willbe presented with a menu Note: Replace `filename.py` with the name of the python file which is to be executed. - -## **Author** - -- [Himanshu Agarwal](https://github.com/himanshu-03) \ No newline at end of file From d6c4f7007be37a4e3f0df913eee61270abb35e58 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Wed, 2 Oct 2024 01:06:06 +0530 Subject: [PATCH 3/8] Update Menu_Driven_Code_for_Circular_Doubly_LinkedList.py --- ...ven_Code_for_Circular_Doubly_LinkedList.py | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py index e11c0591..83e46f07 100644 --- a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py @@ -1,28 +1,48 @@ -""" -# Menu Driven Code for Circular Doubly Linked List -""" +class Node: + """ + Node class for circular doubly linked list. + Args: + data (int): The value to be stored in the node. + """ -class Node: def __init__(self, data): + """ + Initializes a new node with the given data and sets the left and right pointers to None. + """ self.data = data self.right = None self.left = None class LinkedList: + """ + Circular Doubly Linked List implementation with various operations. + """ + def __init__(self): + """ + Initializes an empty circular doubly linked list with root and last pointers set to None. + """ self.root = None self.last = None def insertLeft(self, data): + """ + Inserts a new node at the left (beginning) of the circular doubly linked list. + + Args: + data (int): The value to be inserted at the left. + """ n = Node(data) if self.root == None: + # If list is empty, initialize both root and last to the new node. self.root = n self.last = n self.last.right = self.root self.root.left = self.last else: + # Insert the new node to the left of root and update pointers. n.right = self.root self.root.left = n self.root = n @@ -32,13 +52,18 @@ def insertLeft(self, data): self.printList() def deleteLeft(self): + """ + Deletes a node from the left (beginning) of the circular doubly linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: temp = self.root if self.root == self.last: + # If there is only one node, set root and last to None. self.root = None else: + # Delete the root and update pointers. self.root = self.root.right self.root.left = self.last self.last.right = self.root @@ -46,13 +71,21 @@ def deleteLeft(self): self.printList() def insertRight(self, data): + """ + Inserts a new node at the right (end) of the circular doubly linked list. + + Args: + data (int): The value to be inserted at the right. + """ n = Node(data) if self.root == None: + # If list is empty, initialize both root and last to the new node. self.root = n self.last = n self.last.right = self.root self.root.left = self.last else: + # Insert the new node to the right of the last node and update pointers. self.last.right = n n.left = self.last self.last = n @@ -62,12 +95,17 @@ def insertRight(self, data): self.printList() def deleteRight(self): + """ + Deletes a node from the right (end) of the circular doubly linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: if self.root == self.last: + # If there is only one node, set root and last to None. self.root = None else: + # Delete the last node and update pointers. print('Deleted Element: ', self.last.data) self.last = self.last.left self.last.right = self.root @@ -75,6 +113,9 @@ def deleteRight(self): self.printList() def printList(self): + """ + Prints all elements in the circular doubly linked list in forward order. + """ if self.root == None: print('\nLinked List is empty..!!') return @@ -89,6 +130,9 @@ def printList(self): print() def printReverseList(self): + """ + Prints all elements in the circular doubly linked list in reverse order. + """ if self.root == None: print('\nLinked List is empty..!!') return @@ -103,6 +147,7 @@ def printReverseList(self): print() +# Main menu-driven code to interact with the linked list. o = LinkedList() while True: From e028a518b57e85c0f3bc2797d1550fd6bc88cec6 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Wed, 2 Oct 2024 01:07:38 +0530 Subject: [PATCH 4/8] Update Menu_Driven_Code_for_Circular_LinkedList.py --- ...enu_Driven_Code_for_Circular_LinkedList.py | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py index 83e8c999..d454b46b 100644 --- a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Circular_LinkedList.py @@ -1,57 +1,99 @@ - - class Node: + """ + Node class for Circular Linked List. + + Args: + data (int): The value to be stored in the node. + """ def __init__(self, data): + """ + Initializes a new node with the given data and sets the next pointer to None. + + Args: + data (int): The data to store in the node. + """ self.data = data self.next = None class LinkedList: + """ + Circular Linked List implementation with insertion, deletion, and print operations. + """ def __init__(self): + """ + Initializes an empty circular linked list with root and last pointers set to None. + """ self.root = None self.last = None def insertLeft(self, data): + """ + Inserts a new node at the left (beginning) of the circular linked list. + + Args: + data (int): The value to be inserted at the left. + """ n = Node(data) if self.root == None: + # If the list is empty, initialize both root and last to the new node. self.root = n self.last = n self.last.next = self.root else: + # Insert the new node at the beginning and update root and last.next. n.next = self.root self.root = n self.last.next = self.root def deleteLeft(self): + """ + Deletes a node from the left (beginning) of the circular linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: temp = self.root if self.root == self.last: + # If there is only one node, set root and last to None. self.last = self.root = None else: + # Update root to the next node and adjust the last.next pointer. self.root = self.root.next self.last.next = self.root print('\nDeleted element: ', temp.data) def insertRight(self, data): + """ + Inserts a new node at the right (end) of the circular linked list. + + Args: + data (int): The value to be inserted at the right. + """ n = Node(data) if self.root == None: + # If the list is empty, initialize both root and last to the new node. self.root = n self.last = n self.last.next = self.root else: + # Insert the new node at the end and update last and last.next. self.last.next = n self.last = n self.last.next = self.root def deleteRight(self): + """ + Deletes a node from the right (end) of the circular linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: if self.root == self.last: + # If there is only one node, set root and last to None. self.root = self.last = None else: + # Traverse to the second-last node and update last to the second-last node. temp = self.root temp2 = self.root while temp.next != self.root: @@ -62,8 +104,12 @@ def deleteRight(self): print('\nDeleted element: ', temp.data) def printList(self): + """ + Prints all elements in the circular linked list in forward order. + """ if self.root == None: print('\nLinked List is empty..!!') + return temp = self.root print('\nElements in Linked List are: ') while True: @@ -75,6 +121,7 @@ def printList(self): print() +# Main menu-driven code to interact with the linked list. o = LinkedList() while True: From a1ef6e26eddea848ed3305ce3121cc1d4dab4657 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Wed, 2 Oct 2024 01:08:59 +0530 Subject: [PATCH 5/8] Update Menu_Driven_Code_for_Doubly_LinkedList.py --- .../Menu_Driven_Code_for_Doubly_LinkedList.py | 61 +++++++++++++++++-- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py index 5116ab95..eefbe1da 100644 --- a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Doubly_LinkedList.py @@ -1,58 +1,99 @@ - - class Node: + """ + Node class for the Doubly Linked List. + + Args: + data (int): The value to be stored in the node. + """ def __init__(self, data): + """ + Initializes a new node with the given data and sets the right and left pointers to None. + + Args: + data (int): The data to store in the node. + """ self.data = data self.right = None self.left = None class LinkedList: + """ + Doubly Linked List implementation with insertion, deletion, and traversal operations. + """ def __init__(self): + """ + Initializes an empty doubly linked list with root and last pointers set to None. + """ self.root = None self.last = None def insertLeft(self, data): + """ + Inserts a new node at the left (beginning) of the doubly linked list. + + Args: + data (int): The value to be inserted at the left. + """ n = Node(data) if self.root == None: + # If the list is empty, initialize root to the new node. self.root = n - else: + # Insert the new node at the beginning and update root and the pointers. n.right = self.root self.root.left = n self.root = n def deleteLeft(self): + """ + Deletes a node from the left (beginning) of the doubly linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: temp = self.root if self.root.right == self.root.left == None: + # If only one node is present, set root to None. self.root = None else: + # Update root to the next node. self.root = self.root.right + self.root.left = None print('Deleted: ', temp.data) def insertRight(self, data): + """ + Inserts a new node at the right (end) of the doubly linked list. + + Args: + data (int): The value to be inserted at the right. + """ n = Node(data) if self.root == None: + # If the list is empty, initialize root to the new node. self.root = n else: + # Traverse to the end of the list and insert the new node. temp = self.root while temp.right != None: temp = temp.right temp.right = n n.left = temp - n.right = None print('Inserted Element: ', n.data) def deleteRight(self): + """ + Deletes a node from the right (end) of the doubly linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: if self.root.right == self.root.left: + # If only one node is present, set root to None. self.root = None else: + # Traverse to the last node and delete it. temp = self.root while temp.right != None: temp = temp.right @@ -61,6 +102,9 @@ def deleteRight(self): temp.right = None def printList(self): + """ + Prints all elements in the doubly linked list in forward order. + """ if self.root == None: print('\nLinked List is empty..!!') return @@ -74,22 +118,27 @@ def printList(self): print('') def printListReverse(self): + """ + Prints all elements in the doubly linked list in reverse order. + """ if self.root == None: print('\nLinked List is empty..!!') return else: temp = self.root - print('Elements of linked list are: ') + # Traverse to the last node while temp.right != None: temp = temp.right + # Traverse back to the first node and print elements + print('Elements of linked list are: ') while temp != None: print('|', temp.data, ' <-> ', end=" ") temp = temp.left - print('None') print('') +# Main menu-driven code to interact with the linked list. o = LinkedList() while True: From b207e543715135ce4bc359119fef52c99042ac46 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Wed, 2 Oct 2024 01:10:02 +0530 Subject: [PATCH 6/8] Update Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py --- ...r_Dynamic_Linear_Queue_using_LinkedList.py | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py index a3619fa1..e70e97e2 100644 --- a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Linear_Queue_using_LinkedList.py @@ -1,38 +1,74 @@ - - - class Node: + """ + Node class for the Dynamic Queue. + + Args: + data (int): The value to be stored in the node. + """ def __init__(self, data): + """ + Initializes a new node with the given data and sets the next pointer to None. + + Args: + data (int): The data to store in the node. + """ self.data = data self.next = None class DynamicQueue: + """ + Dynamic Queue implementation using linked list structure for efficient memory management. + """ def __init__(self): + """ + Initializes an empty queue with front and rear pointers set to None. + """ self.front = None self.rear = None def enqueue(self, data): + """ + Adds a new element to the rear of the queue. + + Args: + data (int): The value to be enqueued. + """ n = Node(data) if self.front == None: + # If queue is empty, both front and rear point to the new node. self.front = self.rear = n else: + # Add the new node at the rear and update the rear pointer. self.rear.next = n self.rear = n print('\nElement Enqueued in Queue: ', data) def dequeue(self): + """ + Removes an element from the front of the queue. + """ if self.front == None: + # If queue is empty, print a message. print('\nQueue is empty..!!') else: + # Remove the front element and move the front pointer to the next node. temp = self.front self.front = self.front.next print('\nElement Dequeued from Queue: ', temp.data) + # If the queue is now empty, reset the rear to None. + if self.front == None: + self.rear = None def printQueue(self): + """ + Prints all elements in the queue. + """ if self.front == None: + # If queue is empty, print a message. print('\nQueue is empty..!!') else: + # Traverse from front to rear and print each element. temp = self.front while temp != None: print(temp.data, ' --> ', end='') @@ -40,6 +76,7 @@ def printQueue(self): print() +# Main menu-driven code to interact with the dynamic queue. o = DynamicQueue() while True: @@ -50,18 +87,23 @@ def printQueue(self): ch = int(input('\nEnter your choice: ')) if ch == 1: + # Enqueue operation data = int(input('\nEnter value to enqueue in Queue: ')) o.enqueue(data) elif ch == 2: + # Dequeue operation o.dequeue() elif ch == 3: + # Print queue elements o.printQueue() elif ch == 0: + # Exit the program print('You are out of the program..!!') break else: + # Handle incorrect input print('\nWrong Input..\nEnter the correct choice..!!\n') From ead0bde4cc8d45555b9d8c5d45a2289a7f8fcb62 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Wed, 2 Oct 2024 01:11:03 +0530 Subject: [PATCH 7/8] Update Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py --- ...Code_for_Dynamic_Stack_using_LinkedList.py | 53 +++++++++++++++++-- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py index a47251e8..2293c482 100644 --- a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Dynamic_Stack_using_LinkedList.py @@ -1,49 +1,88 @@ - class Node: + """ + Node class to represent each element in the stack. + + Args: + data (int): The value to be stored in the node. + """ def __init__(self, data): + """ + Initializes a new node with the given data and sets the next pointer to None. + + Args: + data (int): The data to store in the node. + """ self.data = data self.next = None class DynamicStack: + """ + Dynamic Stack implementation using a linked list structure. + """ def __init__(self): + """ + Initializes an empty stack with tos (Top of Stack) set to None. + """ self.tos = None def push(self, data): + """ + Pushes a new element onto the stack. + + Args: + data (int): The value to be pushed onto the stack. + """ n = Node(data) if self.tos == None: + # If stack is empty, set tos to the new node. self.tos = n else: + # Otherwise, insert the new node on top and update tos. n.next = self.tos self.tos = n def pop(self): + """ + Removes the top element from the stack. + """ if self.tos == None: + # If stack is empty, print a message. print('\nStack is empty..!!') else: + # Remove the top element and update tos to the next element. temp = self.tos self.tos = self.tos.next print('Popped Element from Stack: ', temp.data) def peek(self): + """ + Returns the top element of the stack without removing it. + """ if self.tos == None: + # If stack is empty, print a message. print('\nStack is empty..!!') else: + # Display the top element. print('Peeked Element: ', self.tos.data) def printStack(self): + """ + Prints all elements in the stack. + """ if self.tos == None: + # If stack is empty, print a message. print('\nStack is empty..!!') else: - print('Stack Data') + # Traverse from top to bottom and print each element. + print('Stack Data:') temp = self.tos while temp != None: print(temp.data) temp = temp.next -# Main Code - +# Main code with menu-driven interaction o = DynamicStack() while True: @@ -54,21 +93,27 @@ def printStack(self): ch = int(input('\nEnter your choice: ')) if ch == 1: + # Push operation data = int(input('\nEnter value to push in stack: ')) o.push(data) elif ch == 2: + # Pop operation o.pop() elif ch == 3: + # Peek operation o.peek() elif ch == 4: + # Print all stack elements o.printStack() elif ch == 0: + # Exit the program print('You are out of the program..!!') break else: + # Handle incorrect input print('\nWrong Input..\nEnter the correct choice..!!\n') From 4f4dc7d822aba8ab71d8d0a125e240232a3513a7 Mon Sep 17 00:00:00 2001 From: Aviral Garg Date: Wed, 2 Oct 2024 01:12:39 +0530 Subject: [PATCH 8/8] Update Menu_Driven_Code_for_Linear_LinkedList.py --- .../Menu_Driven_Code_for_Linear_LinkedList.py | 87 ++++++++++++++++--- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py index 7c945544..47d1ab36 100644 --- a/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py +++ b/Algorithms_and_Data_Structures/Linked List/Menu_Driven_Code_for_Linear_LinkedList.py @@ -1,45 +1,82 @@ - - class Node: + """ + Node class to represent each element in the linked list. + + Args: + data (int): The value to be stored in the node. + """ def __init__(self, data): + """ + Initializes a new node with the given data and sets the next pointer to None. + """ self.data = data self.next = None class LinkedList: + """ + Linked list implementation with insert, delete, search, and traversal functions. + """ def __init__(self): + """ + Initializes an empty linked list with root set to None. + """ self.root = None def insertLeft(self, data): + """ + Inserts an element at the beginning of the linked list. + + Args: + data (int): The value to be inserted. + """ n = Node(data) if self.root == None: + # If the list is empty, make the new node the root. self.root = n else: + # Insert the new node at the beginning. n.next = self.root self.root = n def deleteLeft(self): + """ + Deletes the element from the beginning of the linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: + # Remove the first node and update the root to the next node. temp = self.root self.root = self.root.next print('\nDeleted element: ', temp.data) def insertRight(self, data): + """ + Inserts an element at the end of the linked list. + + Args: + data (int): The value to be inserted. + """ n = Node(data) if self.root == None: + # If the list is empty, make the new node the root. self.root = n else: + # Traverse to the end of the list and insert the new node. temp = self.root while temp.next != None: temp = temp.next temp.next = n def deleteRight(self): + """ + Deletes the element from the end of the linked list. + """ if self.root == None: print('\nLinked List is empty..!!') else: + # Traverse to the end and remove the last node. temp = self.root temp2 = self.root while temp.next != None: @@ -51,20 +88,30 @@ def deleteRight(self): print('\nDeleted element: ', temp.data) def printList(self): + """ + Prints all elements in the linked list. + """ if self.root == None: print('\nLinked List is empty..!!') - temp = self.root - print('\nElements in Linked List are: ') - while temp != None: - print('|', temp.data, '| -> ', end='') - temp = temp.next - print('None') - print() + else: + temp = self.root + print('\nElements in Linked List are: ') + while temp != None: + print('|', temp.data, '| -> ', end='') + temp = temp.next + print('None\n') def searchList(self, data): + """ + Searches for an element in the linked list. + + Args: + data (int): The value to search for. + """ if self.root == None: print('\nLinked List is empty..!!') else: + # Traverse through the list to find the element. count = 0 temp = self.root while temp != None: @@ -73,12 +120,19 @@ def searchList(self, data): return temp = temp.next count += 1 - return ('\nElement not found') + print('\nElement not found') def deleteElement(self, data): + """ + Deletes a specific element from the linked list. + + Args: + data (int): The value to be deleted. + """ if self.root == None: print('\nLinked List is empty..!!') else: + # Traverse to find the element and remove it from the list. count = 0 temp = self.root temp2 = self.root @@ -93,12 +147,12 @@ def deleteElement(self, data): temp2.next = None else: temp2.next = temp.next - print('\nDeleted Element:', temp.data, - 'from position: ', count) + print('\nDeleted Element:', temp.data, 'from position: ', count) else: print(data, 'not found in Linked List') +# Menu-driven interaction to perform linked list operations. o = LinkedList() while True: @@ -109,33 +163,42 @@ def deleteElement(self, data): ch = int(input('\nEnter your choice: ')) if ch == 1: + # Insert at the beginning of the list. data = int(input('\nEnter value to be inserted in left: ')) o.insertLeft(data) elif ch == 2: + # Insert at the end of the list. data = int(input('\nEnter value to be inserted in right: ')) o.insertRight(data) elif ch == 3: + # Delete from the beginning of the list. o.deleteLeft() elif ch == 4: + # Delete from the end of the list. o.deleteRight() elif ch == 5: + # Delete a specific element. x = int(input('\nEnter the value of Element x: ')) o.deleteElement(x) elif ch == 6: + # Print the entire list. o.printList() elif ch == 7: + # Search for a specific element. data = int(input('Enter the value of Element x: ')) o.searchList(data) elif ch == 0: + # Exit the program. print('You are out of the program..!!') break else: + # Handle incorrect input. print('\nWrong Input..\nEnter the correct choice..!!\n')