Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gc
- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- self.prev = None
- class DoublyLinkedList:
- def __init__(self):
- self.head = None
- # function for inserting node at the front
- def insert_front(self, data):
- # allocate memory for newNode and assign data to newNode
- new_node = Node(data)
- # make newNode as a head
- new_node.next = self.head
- # assign null to prev
- # previous of head
- if self.head is not None:
- self.head.prev = new_node
- # head point to newNode
- self.head = new_node
- # function for inserting new node to a specific position
- def insert_after(self, prev_node, data):
- # check if previous node is null
- if prev_node is None:
- print("Previous node cannot be null!")
- return
- # allocate memory for newNode and assign data to newNode
- new_node = Node(data)
- # set next of newNode to next of prev node
- new_node.next = prev_node.next
- # set next of prev node to newNode
- prev_node.next = new_node
- # set prev of newNode to the previous node
- new_node.prev = prev_node
- # set prev of newNode's next to newNode
- if new_node.next:
- new_node.next.prev = new_node
- # function for inserting new node at the end of the list
- def insert_end(self, data):
- # allocate memory for newNode and assign data to newNode
- new_node = Node(data)
- # assign null to next of newNode (already done in constructor)
- # if the linked list is empty, make the newNode as head node
- if self.head is None:
- self.head = new_node
- return
- # store the head node temporarily (for later use)
- temp = self.head
- # if the linked list is not empty, traverse to the end of the linked list
- while temp.next:
- temp = temp.next
- # now, the last node of the linked list is temp
- # assign next of the last node (temp) to newNode
- temp.next = new_node
- # assign prev of newNode to temp
- new_node.prev = temp
- return
- # function for deleting node from the doubly linked list
- def delete_node(self, delete):
- # if head or del is null, deletion is not possible
- if self.head is None or delete is None:
- return
- # if del_node is the head node, point the head pointer to the next of del_node
- if self.head == delete:
- self.head = delete.next
- # if del_node is not at the last node, point the prev of node next to del_node to the previous of del_node
- if delete.next is not None:
- delete.next.prev = delete.prev
- # if del_node is not the first node, point the next of the previous node to the next node of del_node
- if delete.prev is not None:
- delete.prev.next = delete.next
- # free the memory of del_node
- gc.collect()
- # function for printing the doubly linked list
- def display_list(self, node):
- while node:
- print(node.data, end="->")
- last = node
- node = node.next
- # initialize an empty node
- doubly_linked_list = DoublyLinkedList()
- doubly_linked_list.insert_end("Glen")
- doubly_linked_list.insert_front("Ralph")
- doubly_linked_list.insert_front("Espe")
- doubly_linked_list.insert_end("Frieda")
- print("The node of the doubly linked list: ")
- doubly_linked_list.display_list(doubly_linked_list.head)
- print()
- # insert new node after head
- doubly_linked_list.insert_after(doubly_linked_list.head, "Angelo")
- # insert new node after the second node
- doubly_linked_list.insert_after(doubly_linked_list.head.next, "Nomer")
- print("\nThe node of the doubly linked list after insertion: ")
- doubly_linked_list.display_list(doubly_linked_list.head)
Add Comment
Please, Sign In to add comment