Gadgets 4 Students Career Guide Free Tutorials  Go to Your University  Placement Preparation 
0 like 0 dislike
153 views
in RTU/BTU B.Tech(CSE-III Sem) DSA LAB by Goeduhub's Expert (7.6k points)
Program of doubly linked list

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

Program of Doubly linked list 

program : 

class node:

    def __init__(self, next=None, prev=None, value=None):

        self.next = next # reference to next node 

        self.prev = prev # reference to previous node

        self.value = value

class DoublyLinkedList:

    def __init__(self):

        self.start=None

    def insert_at_first(self, value): 

    # Node & assign the value 

        newnode = node(value = value) 

    # Make next of newnode as start and prevs as NULL 

        newnode.next = self.start 

        newnode.prev = None

    # change prev of start node to newnode  

        if self.start!=None: 

            self.start.prev = newnode  

    # move the start to point to the newnode 

            self.start = newnode  

    def insert_after(self, prev, value):

        if prev==None: # check if the given prev_node is NULL

            print("This node doesn't exist ") 

            return

        #allocate node  & assign value 

        newnode = node(value =value) 

        # Make next of new node as next of prevnode 

        newnode.next = prev.next

        # Make the next of prevnode as newnode  

        prev.next = newnode 

        # Make prevnode as previous of newnode 

        newnode.prev = prev  

        # Change previous of new_node's next node  

        if newnode.next is not None: 

            newnode.next.prev = newnode

    def insert_at_end(self, value): 

        # create node and assign value 

        newnode = node(value = value) 

        last = self.start 

# new node is going to be the last node, so make next of it as NULL 

        newnode.next = None

# If the Linked List is empty, then make the new node as head 

        if self.start is None: 

            newnode.prev = None

            self.start = newnode 

            return

        # Else traverse till the last node  

        while (last.next is not None): 

            last = last.next

        # Change the next of last node  

        last.next = newnode 

        # Make last node as previous of new node 

        newnode.prev = last 

    def delete_node(self, node_to_del):

        if self.start is None or node_to_del is None:

            return          

        # If node to be deleted is head node 

        if self.start == node_to_del: 

            self.start = node_to_del.next

        # Change next only if node to be deleted is NOT 

        # the last node 

        if node_to_del.next is not None: 

            node_to_del.next.prev = node_to_del.prev    

# Change prev only if node to be deleted is NOT first node 

        if node_to_del.prev is not None: 

            node_to_del.prev.next = node_to_del.next

    def display(self, node): 

        print("linked list ")

        while(node is not None): 

            print(node.value) 

            last = node 

            node = node.next

        print("\n reverse linked list ")

        while(last is not None): 

            print(last.value) 

            last = last.prev 

llist = DoublyLinkedList()  

llist.insert_at_end(6)  

llist.insert_at_end(2)

llist.insert_at_end(3) 

llist.insert_at_first(7) 

llist.insert_at_first(5) 

llist.insert_at_first(1)   

llist.insert_at_end(4)    

llist.insert_after(llist.start.next, 8)

llist.delete_node(llist.start)

llist.delete_node(llist.start.next)

llist.display(llist.start)

Output : 

linked list

 5 

2

 3

 4 

reverse linked list 

 4 

5


For more Rajasthan Technical University CSE-III Sem DSA Lab Experiments  CLICK HERE

     

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...