I will write here dll dot insertion at the beginning and let's suppose I'm inserting a value here too and if i want to do the forward traversal then i will just copy and paste this so on running you can see that i am getting the output but once again i have to write the print to see the output into the next line so if you see here you can see that creation of new node has been done and the data element is true now what if i want to do the backward traversal you will see that my logic will work for backward traversal also so if I'm writing here then you can see that i can do the backward traversal also so you can see that i can also do the backward traversal with this logic so this is the basic idea about insertion at the beginning now let's see the insertion at the end so let's suppose this is my n1 node this is my n2 node and this is my n3 node and this is the node that i will create let me give the name as any here so how to create a node.
When I'm writing here any is equal to node and I'm passing data inside this node so what will happen here any node will be created and any dot next will be none and any dot previous will be none initially when I'm creating this node I'm not linking this node to any other node now after this let's see understand the logic so before understanding the logic let me connect these nodes so initially my n1 dot previous will be none and n3 dot previous will be also none and this is my any node right i am not connecting this any node with this doubly linked list as of now so let's see the logic here so initially once again i will create a temporary variable a and inside that, i will assign self. head once again the same logic because i will use this a for the iteration so i don't want to change the head value now i will use a while loop so i will write here a dot next is none so a dot next is what this is my head right a is equal to self. head this is my head so a dot next is pointing to n2 so it's none so this is my a dot next so a dot next is none right so my a is equal to a dot next so my a will jump here similarly once again this is a while loop again a dot next is none right because a dot next is containing the address of n3 node.
Once again this while loop will run a is equal to a dot next so my new a will be coming here now once again if you see a dot next is not none but here a dot next is none right because as of now this is not linked with any node so this while loop will end here so after that what i will do here a dot next is none my here right i will assign any into a dot next so here instead of none any will be connected right this is my any right now any now coming to the any any dot previous is none right so what i will do here once again i will here write any dot previous is equal to a when i'm writing here any dot previous is equal to a that means any dot previous this is the previous pointer of any so it is connecting with n3 that means any dot previous will contain the address of n3 node so this is the basic logic for insertion at end now let's see the coding example so let's see the coding part so here just like i have created insertion at the beginning function once again i will create a function df and i will write here insertion at end and i will pass the parameter here data now what will happen here i will create a node any is equal to node and i will pass data here right inside a node now after creating node once again i have to create a temporary variable.
I will create a temporary variable a is equal to self. head now since i have to insert at the end that means i have to traverse through each node right so for that i will using a while loop so i'll write here while a dot next is not none i will assign a value is equal to a dot next now after this once i am traversing through each node what i will do i have to connect the last node so after this how to connect the last node so i'll write here a dot next is equal to n of e right and i will also write here any dot previous is equal to a so the connection has been done now now let me execute this so for executing what i will do here let me copy this and i will paste here and what's the name here insertion at end so instead of beginning i will write here end and instead of two value let's suppose at end i want to do the insertion right so let me give the value as 25 here and if i'm executing it then you can see that i'm getting the output since i have to give the space here to get the output in the next line so i'll use print function so you can see that a node has been inserted whose data is 25 so this is the logic for insertion at end now let's see the insertion at any specified node so this is my node n1 this is my node n2 and this is my node n3 and this is my node n4 right.
If you see here then n1 dot previous will be pointing to none and then n1 dot next will be equal to n2 similarly n2 dot previous will be equal to n1 so in this way there will be a connection and here n4 dot next will be equal to none so here i want to insert the node so let's suppose this is the position one two three and four so what i will do here i will insert a node at position three so my position is equal to 3 so for inserting a node at particular position first i have to create a node so how to create a node so I'll write here nib is equal to node and then i will pass data inside this so my node has been created this is my node which will be having a data and here an ib dot previous will be equal to none and an ib dot next will be also equal to none initially right so this is my creation of node now my head will be always pointing to the first node so this is my head.
I will write here a is equal to self dot head which will be pointing to n1 initially so after this what i have to do so if i want to insert a node at any particular position so this is a linked list i have to traverse right if i want to insert a node at position 3 so i have to go through position one two two then only i can jump to two to three right so that's why I've used a for loop here and I've written here position minus one so the position is here three so three minus one will be two so for i in range one comma two.
Post a Comment
If you require any additional clarification, please contact me.