Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node:
- def __init__(self, data):
- self.previous = None
- self.data = data
- self.next = None
- class DoublyLinkedList:
- def __init__(self):
- self.head = None
- self.start_node = None
- self.last_node = None
- def append(self, data):
- if self.last_node is None:
- self.head = Node(data)
- self.last_node = self.head
- else:
- new_node = Node(data)
- self.last_node.next = new_node
- new_node.previous = self.last_node
- new_node.next = None
- self.last_node = new_node
- def display(self, Type):
- if Type == "Left->Right":
- current = self.head
- while current is not None:
- print(current.data, end = ' ')
- current = current.next
- print()
- else:
- current = self.last_node
- while current is not None:
- print(current.data, end = ' ')
- current = current.previous
- print()
- if __name__ == "__main__":
- L = DoublyLinkedList()
- L.append(1)
- L.append("Nishat")
- L.append("9")
- L.append(5)
- L.display("Left->Right")
- L.display("Right->Left")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement