Tutorial Doubly Linked List in Python - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Doubly Linked List in Python (/showthread.php?tid=8004) |
Doubly Linked List in Python - Covid-19 - 07-30-2022 Note: It is important to have knowledge of single linked list before continuing... Doubly Linked List: Doubly Linked List is a type of linked list. The single linked list only stores the address of the next node but in the doubly linked list the list stores two addresses one of the previous node and one of the next node. The doubly linked list can be traversed in both directions and insertion and deletion of nodes is easy and simple as compared to single linked list. The implementation of Doubly linked list is as follows Code: class Node: Now we will see the operations that we can perform on the double linked list first we will write a traverse method for a doubly linked list that will traverse it in both directions forward and backward. Code: def showInfo(self): First we will go from head node to the last and using the next address and then we will traverse it from last node to first using the previous node address. So now as we can see how we can traverse a Doubly linked list and now we will learn about the inserting of node in doubly linked list first we will talk about inserting at beginning of list so here is the implementation Code: def insert_begin(self, new_data): In this code we created a node and in its next portion we stored the address of first node so that it now becomes the first if the head node was not none means it has some data in it so we will add new node in its previous address portion Now we will see how can we add a node after a specific node, So here is the implementation Code: def insert_after(self, prev_node, new_data): In this we check two conditions, the first one the previous node that we wanted to add a new node after it exists or not. And the second is whether it is the last node or not. If it is the last node, leave it as it is. if it is not then add the reference / address of the new node to the previous address portion of the next node. Now lastly if we want to append the doubly linked list means if we want to add a node at the last of the node. Code: def append(self, new_data): Now we will learn how we can delete a specific node from a doubly linked list. As I said before that deletion is easy in doubly linked lists so here is the implementation. Code: def delete_node(self, dele): Before deletion we want to keep it in mind that we have to import a method called gc (Garbage Collector) . It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory. So here in last there is the summary of all methods and codes of doubly linked list Code: import gc That’s it for now see you in next thread. "Keep Good care of your Health Allah Hafiz and Good Bye Till the next post" |