Today is my 49th day of #100DaysOfCode and #python. Today I studied more about data structure. Advanced liked list. It is similar to the single linked list. Unlike in single linked list we can not traverse in forward and backward direction but in doubly linked list we can.
Doubly linked list contains a link element called first and last. Each link carries a data fields and two linked fields called next and prev.
We create a node class with three member variable item, data next and previous. Data variable will store the actual data for the node. The next store the reference to the next node and prev stores the reference to the privious node.
class Node: def __init__(self, data): self.data = data self.next = None self.prev = None class doubly_linked_list: def __init__(self): self.head = None def push(self, NewVal): NewNode = Node(NewVal) NewNode.next = self.head if self.head is not None: self.head.prev = NewNode self.head = NewNode def listprint(self, node): while (node is not None): print(node.data), last = node node = node.next dllist = doubly_linked_list() dllist.push(12) dllist.push(8) dllist.push(62) dllist.listprint(dllist.head)
We need to create the doubly linked list class, which contains different doubly linked list related functions.
Output of the above function is,
62 8 12