Linked list in Python

Linked list in Python

·

2 min read

A well known data structure from C, Java is linked list ie..the linear collection of data elements whose order is not given by their pyhsical placement memory.Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.

Actuallt, python is a beginner's language yes, it's true because implementing data strcture in Python is very easy than C and Java.This is the code snippet that implements the traversal in linked list.

    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("First")
      e2 = Node("Second")
      e3 = Node("Third")
     # Link first Node to second node
     list.headval.nextval = e2
     # Link second Node to third node
     e2.nextval = e3
     list.listprint()

     #Output:

         First
         Second
         Third

Screenshot (421).png