Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: linked_list_manager.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script provides classes for implementing singly linked lists and doubly linked lists in Python.
- It includes functionalities to insert nodes at the front and end of the lists, remove nodes from the front and end,
- and print the contents of the lists.
- Requirements:
- - Python 3.x
- Functions:
- - ListNode.__init__(self, val):
- Initializes a node with a given value.
- - DoublyLinkedList.__init__(self):
- Initializes a doubly linked list.
- - DoublyLinkedList.insertFront(self, val):
- Inserts a node with the given value at the front of the doubly linked list.
- - DoublyLinkedList.insertEnd(self, val):
- Inserts a node with the given value at the end of the doubly linked list.
- - DoublyLinkedList.removeFront(self):
- Removes the node at the front of the doubly linked list.
- - DoublyLinkedList.removeEnd(self):
- Removes the node at the end of the doubly linked list.
- - DoublyLinkedList.print_list(self):
- Prints the contents of the doubly linked list in reverse order.
- - LinkedList.__init__(self):
- Initializes a singly linked list.
- - LinkedList.insertEnd(self, val):
- Inserts a node with the given value at the end of the singly linked list.
- - LinkedList.print_list(self):
- Prints the contents of the singly linked list.
- Example Output:
- Choose an option:
- 1: Linked List
- 2: Doubly-Linked List
- 0: Exit
- Enter your choice: 1
- Enter a string value to insert: Single
- Single ->
- Choose an option:
- 1: Linked List
- 2: Doubly-Linked List
- 0: Exit
- Enter your choice: 2
- Enter a string value to insert: Double
- Single -> Double ->
- Usage:
- - Instantiate LinkedList or DoublyLinkedList objects.
- - Use insertEnd() to insert nodes into the linked lists.
- - Use print_list() to print the contents of the linked lists.
- - Run the script to interactively choose options for adding elements to the lists or exiting the program.
- """
- class ListNode:
- """Represents a node in a linked list."""
- def __init__(self, val):
- """Initializes a node with a given value."""
- self.val = val
- self.next = None
- self.prev = None
- class DoublyLinkedList:
- """Represents a doubly linked list."""
- def __init__(self):
- """Initializes a doubly linked list."""
- self.head = ListNode(None)
- self.tail = ListNode(None)
- self.head.next = self.tail
- self.tail.prev = self.head
- def insertFront(self, val):
- """Inserts a node with the given value at the front of the doubly linked list."""
- new_node = ListNode(val)
- new_node.prev = self.head
- new_node.next = self.head.next
- self.head.next.prev = new_node
- self.head.next = new_node
- def insertEnd(self, val):
- """Inserts a node with the given value at the end of the doubly linked list."""
- new_node = ListNode(val)
- new_node.next = self.tail
- new_node.prev = self.tail.prev
- self.tail.prev.next = new_node
- self.tail.prev = new_node
- def removeFront(self):
- """Removes the node at the front of the doubly linked list."""
- first_node = self.head.next
- first_node.next.prev = self.head
- self.head.next = first_node.next
- def removeEnd(self):
- """Removes the node at the end of the doubly linked list."""
- last_node = self.tail.prev
- last_node.prev.next = self.tail
- self.tail.prev = last_node.prev
- def print_list(self):
- """Prints the contents of the doubly linked list in reverse order."""
- current_node = self.tail.prev
- while current_node != self.head:
- print(current_node.val, " -> ", end="")
- current_node = current_node.prev
- print()
- class LinkedList:
- """Represents a singly linked list."""
- def __init__(self):
- """Initializes a singly linked list."""
- self.head = None
- def insertEnd(self, val):
- """Inserts a node with the given value at the end of the singly linked list."""
- new_node = ListNode(val)
- if self.head is None:
- self.head = new_node
- else:
- current = self.head
- while current.next:
- current = current.next
- current.next = new_node
- def print_list(self):
- """Prints the contents of the singly linked list."""
- current = self.head
- while current:
- print(current.val, " -> ", end="")
- current = current.next
- print()
- def main():
- """Main function for interactive usage."""
- choice = None
- linked_list = LinkedList()
- doubly_linked_list = DoublyLinkedList()
- while choice != '0':
- print("Choose an option:\n")
- print("1: Linked List")
- print("2: Doubly-Linked List")
- print("0: Exit")
- choice = input("\nEnter your choice: ")
- if choice == '1':
- val = input("\nEnter a string value to insert: ")
- linked_list.insertEnd(val)
- doubly_linked_list.insertFront(val)
- linked_list.print_list()
- elif choice == '2':
- val = input("\nEnter a string value to insert: ")
- doubly_linked_list.insertFront(val)
- linked_list.insertEnd(val)
- doubly_linked_list.print_list()
- elif choice == '0':
- print("\nExiting Program... GoodBye!\n")
- else:
- print("\nInvalid choice! Please enter a valid option.\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement