Simple Linked List Program

Simple linked list operations

This program demonstrates basic operations of linked list. Student will be able to understand the basics of linked list. We are just inserting 3 elements in linked list. You can see basic operations of linked list by clicking here.

Python program on linked list

class Node:
    def __init__(self, data):
       self.data = data
       self.next = None
 # Author : ITVoyagers Website : itvoyagers.in
class LinkedList:
    def __init__(self):
        self.head = None
        self.last_node = None
        self.size=0
  # Author : ITVoyagers Website : itvoyagers.in
    def insert(self, data):
        newnode=Node(data)
        if self.head ==None:
            self.head=newnode
            self.tail=newnode
        else:
            newnode.next=self.head
            self.head=newnode
        print("node inserted successfully with data ",data,"at the beginning")
        self.size+=1
  # Author : ITVoyagers Website : itvoyagers.in
    def display(self):
        current = self.head
        while current.next is not None:
            print(current.data, end = ' ')
            current = current.next
        print(current.data)
  # Author : ITVoyagers Website : itvoyagers.in
k = LinkedList()
k.insert(61)
k.insert(62)
k.insert(63)
k.display()

Output :

node inserted successfully with data  61 at the beginning                                                               
node inserted successfully with data  62 at the beginning                                                               
node inserted successfully with data  63 at the beginning                                                               
63 62 61

Note : Please note that above program is compatible with Python 3 or higher version

Leave a Comment