Tutorial 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 Linked List in Python (/showthread.php?tid=8002) |
Linked List in Python - Covid-19 - 07-27-2022 Singly Linked List: Hi guys, I hope you all are doing well. In python tutorials I have mentioned a number of data structures like Lists, Dictionary, etc but in this thread I am going to tell you about a user defined data structure known as Linked List. as In python’s list thread I mentioned that python does not support arrays built-in; it has lists instead of arrays. Arrays are contiguous memory locations. But Linked Lists can be random in memory. Linked lists consist of nodes and each node stores two things: first data of that node and the second address of the next node. So if we code a linked list we have two create two classes one for nodes and one for linked list itself the first node is called head of linked list and the last node does not store any address it is represented as null. So now first of all we will create the classes for linked lists and its nodes. Code: class Node: So this is the basic structure of linked list in python We can perform the following function to any of data structure we will apply it one by one Traversing (Accessing each element of data structure) Searching Inserting Deleting So now first of all we will see how we can traverse each node of the linked list. So now I will create some nodes and link them and then I will traverse them. Code: def traversing(self): This method is used to traverse the code we created a temp variable and assign it to the first node of the class and then one by one we will go to each node of the linked list Now we will talk about how we can insert data into start of linked list Code: # inserting at the start of linked list In this way we can add nodes to end and at point in linked list Code: # inserting after a specified node In this code first we will check that the node exists that we want to add our new node after that and then we will create our node. And in last we will discuss about adding node at the end of linked list Code: # Inserting node at the end of Linked list In this method we will loop until the address specified by the node is Null in this way we can reach through the last address and we will assign it new_node Now we will talk about the searching and deleting a specified node. In deleting a node in python we will take care of two things the node we are searching for it is first node or not And this node exists or node. So now taking care of these things I am going to code a delete method for it and after that we will create a program that demonstrates all these methods at once. Once we get the node we want to delete then we will remove all its relations and assign it as null. Code: # deleting node Now I am going to give you all that code in form of one program so you can check how each these method works Code: # Creating a class for node So that’s it for now "Keep Good care of your Health Allah Hafiz and Good Bye Till the next post" RE: Linked List in Python - Mr.Kurd - 07-28-2022 Thanks bro, I usually use List and ArrayList a lot in Java. They are very flexible and easy to use RE: Linked List in Python - Covid-19 - 07-30-2022 (07-28-2022, 07:19 AM)Mr.Kurd Wrote: Thanks bro, I usually use List and ArrayList a lot in Java. Obviously List is easy to use but Linked list has advantages in memory management |