DEV Community

Lancelot03
Lancelot03

Posted on

Linked List Part-2

Linked List Insertion at the front--

Insertion at the front

class Node:
    def __init__(self, data):
        self.data=data
        self.ref=None 
class LinkedList:
    def __int__(self):
        self.head=None

    def begin(self,new_data):    
        q1=Node(new_data)
        q1.ref=self.head
        self.head=q1
Enter fullscreen mode Exit fullscreen mode

Insertion at the Middle--

Insertion at the Middle

def inbet(self,prev,new_data):
        if prev.ref is None:
            print('LL is empty')
            return
        q4 = Node(new_data)
        q4.ref=prev.ref
        prev.ref=q4
Enter fullscreen mode Exit fullscreen mode

Insertion at the End--

Insertion at the End

def last(self,new_data):
        q2=Node(new_data)
        if self.head==None:
            self.head=q2
            return
        temp=self.head
        while(temp.ref):
            temp=temp.ref

        temp.ref=q2
Enter fullscreen mode Exit fullscreen mode

Printing Of Linked List--

def print_ll(self):
        if self.head is None:
            print("Linked list is empty")
            return
        n=self.head
        while n:
            print(n.data)
            n=n.ref
Enter fullscreen mode Exit fullscreen mode

Function Calling--

h1=Node(2)
h2=Node(3)

k1=LinkedList()
k1.head=h1
h1.ref=h2

k1.begin(1)
k1.inbet(k1.head.ref,5)
k1.last(8)

k1.print_ll()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)