Advertisement
Python253

linked_list_manager

May 24th, 2024
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: linked_list_manager.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    This script provides classes for implementing singly linked lists and doubly linked lists in Python.
  10.    It includes functionalities to insert nodes at the front and end of the lists, remove nodes from the front and end,
  11.    and print the contents of the lists.
  12.  
  13. Requirements:
  14.    - Python 3.x
  15.  
  16. Functions:
  17.    - ListNode.__init__(self, val):
  18.        Initializes a node with a given value.
  19.    - DoublyLinkedList.__init__(self):
  20.        Initializes a doubly linked list.
  21.    - DoublyLinkedList.insertFront(self, val):
  22.        Inserts a node with the given value at the front of the doubly linked list.
  23.    - DoublyLinkedList.insertEnd(self, val):
  24.        Inserts a node with the given value at the end of the doubly linked list.
  25.    - DoublyLinkedList.removeFront(self):
  26.        Removes the node at the front of the doubly linked list.
  27.    - DoublyLinkedList.removeEnd(self):
  28.        Removes the node at the end of the doubly linked list.
  29.    - DoublyLinkedList.print_list(self):
  30.        Prints the contents of the doubly linked list in reverse order.
  31.    - LinkedList.__init__(self):
  32.        Initializes a singly linked list.
  33.    - LinkedList.insertEnd(self, val):
  34.        Inserts a node with the given value at the end of the singly linked list.
  35.    - LinkedList.print_list(self):
  36.        Prints the contents of the singly linked list.
  37.  
  38. Example Output:
  39.  
  40.    Choose an option:
  41.  
  42.    1: Linked List
  43.    2: Doubly-Linked List
  44.    0: Exit
  45.  
  46.    Enter your choice: 1
  47.  
  48.    Enter a string value to insert: Single
  49.    Single  ->
  50.    Choose an option:
  51.  
  52.    1: Linked List
  53.    2: Doubly-Linked List
  54.    0: Exit
  55.  
  56.    Enter your choice: 2
  57.  
  58.    Enter a string value to insert: Double
  59.    Single  -> Double  ->
  60.  
  61. Usage:
  62.    - Instantiate LinkedList or DoublyLinkedList objects.
  63.    - Use insertEnd() to insert nodes into the linked lists.
  64.    - Use print_list() to print the contents of the linked lists.
  65.    - Run the script to interactively choose options for adding elements to the lists or exiting the program.
  66. """
  67.  
  68. class ListNode:
  69.     """Represents a node in a linked list."""
  70.     def __init__(self, val):
  71.         """Initializes a node with a given value."""
  72.         self.val = val
  73.         self.next = None
  74.         self.prev = None
  75.  
  76. class DoublyLinkedList:
  77.     """Represents a doubly linked list."""
  78.     def __init__(self):
  79.         """Initializes a doubly linked list."""
  80.         self.head = ListNode(None)
  81.         self.tail = ListNode(None)
  82.         self.head.next = self.tail
  83.         self.tail.prev = self.head
  84.    
  85.     def insertFront(self, val):
  86.         """Inserts a node with the given value at the front of the doubly linked list."""
  87.         new_node = ListNode(val)
  88.         new_node.prev = self.head
  89.         new_node.next = self.head.next
  90.         self.head.next.prev = new_node
  91.         self.head.next = new_node
  92.  
  93.     def insertEnd(self, val):
  94.         """Inserts a node with the given value at the end of the doubly linked list."""
  95.         new_node = ListNode(val)
  96.         new_node.next = self.tail
  97.         new_node.prev = self.tail.prev
  98.         self.tail.prev.next = new_node
  99.         self.tail.prev = new_node
  100.  
  101.     def removeFront(self):
  102.         """Removes the node at the front of the doubly linked list."""
  103.         first_node = self.head.next
  104.         first_node.next.prev = self.head
  105.         self.head.next = first_node.next
  106.  
  107.     def removeEnd(self):
  108.         """Removes the node at the end of the doubly linked list."""
  109.         last_node = self.tail.prev
  110.         last_node.prev.next = self.tail
  111.         self.tail.prev = last_node.prev
  112.  
  113.     def print_list(self):
  114.         """Prints the contents of the doubly linked list in reverse order."""
  115.         current_node = self.tail.prev
  116.         while current_node != self.head:
  117.             print(current_node.val, " -> ", end="")
  118.             current_node = current_node.prev
  119.         print()
  120.  
  121. class LinkedList:
  122.     """Represents a singly linked list."""
  123.     def __init__(self):
  124.         """Initializes a singly linked list."""
  125.         self.head = None
  126.  
  127.     def insertEnd(self, val):
  128.         """Inserts a node with the given value at the end of the singly linked list."""
  129.         new_node = ListNode(val)
  130.         if self.head is None:
  131.             self.head = new_node
  132.         else:
  133.             current = self.head
  134.             while current.next:
  135.                 current = current.next
  136.             current.next = new_node
  137.  
  138.     def print_list(self):
  139.         """Prints the contents of the singly linked list."""
  140.         current = self.head
  141.         while current:
  142.             print(current.val, " -> ", end="")
  143.             current = current.next
  144.         print()
  145.  
  146. def main():
  147.     """Main function for interactive usage."""
  148.     choice = None
  149.     linked_list = LinkedList()
  150.     doubly_linked_list = DoublyLinkedList()
  151.     while choice != '0':
  152.         print("Choose an option:\n")
  153.         print("1: Linked List")
  154.         print("2: Doubly-Linked List")
  155.         print("0: Exit")
  156.         choice = input("\nEnter your choice: ")
  157.  
  158.         if choice == '1':
  159.             val = input("\nEnter a string value to insert: ")
  160.             linked_list.insertEnd(val)
  161.             doubly_linked_list.insertFront(val)
  162.             linked_list.print_list()
  163.         elif choice == '2':
  164.             val = input("\nEnter a string value to insert: ")
  165.             doubly_linked_list.insertFront(val)
  166.             linked_list.insertEnd(val)
  167.             doubly_linked_list.print_list()
  168.         elif choice == '0':
  169.             print("\nExiting Program...   GoodBye!\n")
  170.         else:
  171.             print("\nInvalid choice! Please enter a valid option.\n")
  172.  
  173. if __name__ == "__main__":
  174.     main()
  175.  
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement