Learn practical skills, build real-world projects, and advance your career

Linked list

class Node:
    def __init__(self,data):
        self.next=None
        self.data=data
class linkedlist:
    def __init__(self):
        self.head=None
    def prepend(self,data):
        new_node=Node(data)
        if self.head is None:
            self.head=new_node
            return 
        else:
            new_node.next=self.head
            self.head=new_node
    def append(self,data):
        new_node=Node(data)
        if self.head is None:
            self.head=new_node
            return
        else:
            cur=self.head
            while cur.next:
                cur=cur.next
            cur.next=new_node
    def print(self):
        cur=self.head
        while cur:
            print(cur.data,end=' ')
            cur=cur.next
            
    def middle(self,key,data):
        cur=self.head
        while cur:
            if cur.next is None and cur.data==key:
                self.append(data)
                return
            elif cur.data==key:
                new=Node(data)
                nxt=cur.next
                cur.next=new
                new.next=nxt
            cur=cur.next
    def delete(self,key):
        cur=self.head
        if cur and cur.data==key:
            self.head=cur.next
            cur=None
            return
        else:
            prev=None
            while cur and cur.data!=key:
                prev=cur
                cur=cur.next
            if cur is None:
                return
            prev.next=cur.next
            cur=None
            
    def delNode(self, k):
    # Code here
        cur=self.head
        c=1
        if cur and c==k:
            head=None
            return
        else:
            prev=None
            while cur and c!=k:
                prev=cur
                cur=cur.next
                c+=1
            if cur is None:
                return
            prev.next=cur.next
            cur=None

        
ll=linkedlist()
ll.append('mon')
ll.append('tue')
#ll.append(10)
ll.append('wed')
ll.prepend('thr')
ll.prepend(5)
ll.append('sat')
#ll.middle('',5)
#ll.delete('mon')
ll.print()
ll.delNode('mon')        
5 thr mon tue wed sat
ll=linkedlist()
#ll.append(1)
ll.append(2)
#ll.append(3)
ll.middle(2,5)
ll.print()
2 5
class Node:
    def __init__(self,data):
        self.next=None
        self.prev=None
        self.data=data
class double:
    def __init__(self):
        self.head=None
        
    def append(self,data):
        new_node=Node(data)
        cur=self.head
        if cur is None:
            self.head=new_node
            new_node.prev=None
            
        else:
            while cur.next:
                cur=cur.next
            cur.next=new_node
            new_node.prev=cur
            new_node.next=None
    def print(self):
        cur=self.head
        while cur:
            print(cur.data,end=' ')
            cur=cur.next
    def prepend(self,data):
        new_node=Node(data)
        cur=self.head
        if cur is None:
            self.head=new_node
            new_node.prev=None
        else:
            cur.prev=new_node
            new_node.next=cur
            self.head=new_node
            new_node.prev=None
    def add_after(self,key,data):
        cur=self.head
        while cur:
            if cur.next is None and cur.data==key:
                self.append(data)
                return
            elif cur.data==key:
                new=Node(data)
                nxt=cur.next
                cur.next=new
                new.prev=cur
                nxt.prev=new
                new.next=nxt
            cur=cur.next
    def add_before(self,key,data):
        cur=self.head
        while cur:
            if cur.prev is None and cur.data==key:
                self.prepend(data)
                return
            elif cur.data==key:
                    new=Node(data)
                    prev=cur.prev
                    prev.next=new
                    new.prev=prev
                    new.next=cur
                    cur.prev=new
            cur=cur.next
    
    def delete(self,key):
        cur=self.head
        while cur:
            #case1
            if cur.data==key is None and cur==self.head:
                if not cur.next:
                    cur=None
                    self.head=None
                    print('element deleted ')
                    return
                else:
                    #case2
                    nxt=cur.next
                    nxt.prev=None
                    cur.next=None
                    cur=None
                    self.head=nxt
                    return
            elif cur.data==key:
                #case3
                if cur.next:
                    nxt=cur.next
                    prev=cur.prev
                    prev.next=nxt
                    nxt.prev=prev
                    cur.next=None
                    cur.prev=None
                    cur=None
                    return
                else:
                    prev=cur.prev
                    prev.next=None
                    cur.prev=None
                    cur=None
                    return
            cur=cur.next
                
        
            
dll=double()
dll.append('tue')
dll.prepend('thr')
dll.append('mon')
#dll.prepend('wed')
#dll.add_before('mon','sat')
dll.delete('mon')
dll.print()
        
thr tue
dll=double()
dll.append(1)
dll.append(2)
dll.append(3)
dll.append(4)
dll.add_after(1,11)
dll.add_after(11,12)
dll.add_after(4,14)

dll.print()
        
1 11 12 2 3 4 14