Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python code to perform circular linked list operations
- class Node:
- def __init__(self, data):
- self.data = data
- self.next = None
- class CircularLinkedList:
- def __init__(self):
- self.last = None
- def add_to_empty(self, data):
- if self.last is not None:
- return self.last
- # allocate memory to the new node and add data to the node
- new_node = Node(data)
- # assign last to new_node
- self.last = new_node
- # create link to itself
- self.last.next = self.last
- return self.last
- # add node to the front
- def add_front(self, data):
- # check if the list is empty
- if self.last is None:
- return self.add_to_empty(data)
- # allocate memory to the new node and add data to the node
- new_node = Node(data)
- # store the address of the current first node in the new_node
- new_node.next = self.last.next
- # make new_node as last
- self.last.next = new_node
- return self.last
- # add node to the end
- def add_end(self, data):
- # check if the node is empty
- if self.last is None:
- return self.add_to_empty(data)
- # allocate memory to the new node and add data to the node
- newNode = Node(data)
- # store the address of the last node to next of newNode
- newNode.next = self.last.next
- # point the current last node to the newNode
- self.last.next = newNode
- # make newNode as the last node
- self.last = newNode
- return self.last
- # insert node after a specific node
- def add_after(self, data, item):
- # check if the list is empty
- if self.last is None:
- return None
- new_node = Node(data)
- p = self.last.next
- while p:
- # if the item is found, place new_node after it
- if p.data == item:
- # make the next of the current node as the next of new_node
- new_node.next = p.next
- # put new_node to the next of p
- p.next = new_node
- if p == self.last:
- self.last = new_node
- return self.last
- else:
- return self.last
- p = p.next
- if p == self.last.next:
- print(item, "The given node is not present in the list")
- break
- # delete a node
- def delete_node(self, last, key):
- # If linked list is empty
- if last is None:
- return
- # If the list contains only a single node
- if last.data == key and last.next == last:
- last = None
- temp = last
- d = None
- # if last node is to be deleted
- if last.data == key:
- # find the node before the last node
- while temp.next != last:
- temp = temp.next
- # point temp node to the next of last i.e. first node
- temp.next = last.next
- last = temp.next
- # travel to the node to be deleted
- while temp.next != last and temp.next.data != key:
- temp = temp.next
- # if node to be deleted was found
- if temp.next.data == key:
- d = temp.next
- temp.next = d.next
- return last
- def traverse(self):
- if self.last is None:
- print("The list is empty")
- return
- new_node = self.last.next
- while new_node:
- print(new_node.data, end=" ")
- new_node = new_node.next
- if new_node == self.last.next:
- break
- if __name__ == "__main__":
- circular = CircularLinkedList()
- # add node to the circular linked list
- last = circular.add_to_empty("Glen")
- last = circular.add_end("Espe")
- last = circular.add_front("Ralph")
- last = circular.add_after("Frieda", "Ralph")
- print("The node of the circular linked list: ")
- circular.traverse()
- print()
- # delete last node of the circular linked list
- last = circular.delete_node(last, "Espe")
- print("\nThe node of the circular linked list after deletion: ")
- circular.traverse()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement