DEV Community

loading...

Day 48 Of 100DaysOfCode: Linked List

iamdurga profile image Durga Pokharel ・2 min read

Today is my 48th day of 100daysOfCode and python learning. Today I learned about capstone: Retrieving, Processing, and Visualizing Data
from Coursera.

Also learned about linked list data structure in python. A linked list is a sequence of data elements, which are connected together through link.

Python Code

A linked list is created by using node class. We create a node object and create a another class to use this node object. We pass appropriate value through the node object to point to the next data elements.
python code is,

class Node:
    def __init__(self, dataval=None):
        self.dataval = dataval
        self.nextval = None

class SLinkedList:
    def __init__(self):
        self.headval = None

    def listprint(self):
        printval = self.headval
        while printval is not None:
            print (printval.dataval)
            printval = printval.nextval

list = SLinkedList()
list.headval = Node("Sun")
e2 = Node("Mon")
e3 = Node("Tue")
e4 = Node("Wed")
e5 = Node("Fri")
e6 = Node("Sat")


list.headval.nextval = e2
list.headval.nextval = e2
list.headval.nextval = e2
list.headval.nextval = e2


e2.nextval = e3
e3.nextval = e4
e4.nextval = e5
e5.nextval = e6

list.listprint()
Enter fullscreen mode Exit fullscreen mode

Output of the above program is

Sun
Mon
Tue
Wed
Fri
Sat

Enter fullscreen mode Exit fullscreen mode

Discussion (1)

pic
Editor guide
Collapse
otumianempire profile image
Otu Michael

52 days more..