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.
Python code
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
Day 49 Of #100DaysOfCode and #Python
— Durga Pokharel (@mathdurga) February 11, 2021
Advanced Linked List Data Structure In Python#CodeNewbie #womenintech #DEVCommunity #beginner #100DaysOfCode #WomenWhoCode #Python pic.twitter.com/nVQ566RggE
Top comments (1)
I was looking at this code, from yesterday, dev.to/iamdurga/day-48-of-100dayso...
On day 48, the Node objects were created outside and passed to the SLinkedList but here, you pass the data and a new Node is created when you call push..
So far so good.. thumbs up..