Mục lục bài viết
Cập Nhật: 2021-12-17 08:17:05,Bạn Cần kiến thức và kỹ năng về Double-ended linked list Python. Bạn trọn vẹn có thể lại Thảo luận ở phía dưới để Ad đc lý giải rõ ràng hơn.
Hello programmers, you will get insights into all that you need to know about the doubly linked list in Python in this article. We will discuss what doubly linked list is and where they are used in python programming. We will also look at creating a doubly linked list in python and performing various operations on it. So without any further delay, lets begin with the topic.
Tóm lược đại ý quan trọng trong bài
Before we understand the creation and operations performed over the doubly linked list in python, Let me brief you about what is a doubly linked list and where they are important.
Contents
A doubly linked list in Python is a linked data structure with a set of sequentially linked nodes. Each node has three fields two link fields, which are references to the previous and next nodes address in the sequence. In addition, one data field referring to the data of that particular node.
Data Part: stores the data.
Prev Part: stores the address of the previous node.
Next Part: stores the address of the next node.
Since there is no node before the first node and after the last node, the prev pointer of the first node and next pointer of the last node points to NULL.
Use of Doubly Linked List used in python: Doubly linked lists are very helpful where both front and back navigation is required. It allows traversal of the list in either direction. A doubly linked list requires changing more links than a singly linked list while adding or removing nodes. But, the operations are potentially simpler and efficient as it does not require keeping track of the previous node, unlike the singly linked list. It has various applications to implement Undo and Redo functions.
Things we will learn in details about Doubly Linked list in Python are :
class Node:
def __init__(self,data):
self.data = data;
self.nref = None;
self.pref = None;
class DoublyLinkedList:
def __init__(self):
self.head = None;
self.tail = None;
def addNode(self, data):
newNode = Node(data);
if(self.head == None):
self.head = self.tail = newNode;
self.head.nref = None;
self.tail.pref = None;
else:
self.tail.next = newNode;
newNode.nref = self.tail;
self.tail = newNode;
self.tail.pref = None;
def display(self):
current = self.head;
if(self.head == None):
print(“List is empty”);
return;
print(“Nodes of doubly linked list: “);
while(current != None):
print(current.data),;
current = current.next;
dList = DoublyLinkedList();
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);
dList.display();
Output:
Nodes of doubly linked list: 1 2 3 4 5
Creating a doubly-linked list:-In this example, we define a class with three thành viên variables: data, nref, and pref. The data variable will store the data for the node. The nref holds the reference to the next node, while pref holds the reference to the previous node in the doubly linked list. Then a DoublyLinkedList class containing different doubly linked list related functions is created. The two nodes of the doubly linked list class initially point to null.
Inserting Nodes to a doubly-linked list:- The addNode() function adds new nodes to the list. Firstly, it checks if the head is null; it inserts the node as the head. If the head is not null, new node attaches to the end of the list. Lastly, the new nodes previous pointer now points to the tail, and the new node itself becomes the tail.
Display a doubly-linked list: display() function shows all the nodes present in the list. The new node current points to the head. All the nodes are printed till the current data points to null. In each iteration, the current will point to the next node.
def delete_at_start(self):
if self.start_node is None:
print(“The list has no element to delete”)
return
if self.start_node.nref is None:
self.start_node = None
return
self.start_node = self.start_node.nref
self.start_pref = None;
def delete_at_end(self):
if self.start_node is None:
print(“The list has no element to delete”)
return
if self.start_node.nref is None:
self.start_node = None
return
n = self.start_node
while n.nref is not None:
n = n.nref
n.pref.nref = None
Deleting from first: At first, the code checks whether the list has one element or its empty. If it has only a single element, it is deleted by setting the previous reference of that node to None. In the case of the list which has multiple elements, then the value of the start node is set to the next node, and then the previous reference of the start node is set to None.
Deleting from the end: Just like for deleting an element from the first, the list is first if it is empty or contains a single element. In the case of a single node, the start node is just set to None. If the list has multiple elements, the list is iterated till the last node is reached. Then the next reference of the node previous node is None, which basically removes the last node.
def traverse_list(self):
if self.start_node is None:
print(“List has no element”)
return
else:
n = self.start_node
while n is not None:
print(n.data , ” “)
n = n.nref
Traversing a doubly linked list is quite similar to that of tarversing a singly linked list. The traverse_list() function is added to the DoubleLinkedList class added earlier. The function first checks if the list is empty. If not, it prints the data of all nodes while traversing the list.
def reverse_linked_list(self):
if self.start_node is None:
print(“The list has no element to delete”)
return
p. = self.start_node
q = p..nref
p..nref = None
p..pref = q
while q is not None:
q.pref = q.nref
q.nref = p.
p. = q
q = q.pref
self.start_node = p.
In the above example, the next reference of the start node is set none because the first node becomes the last node in the reversed list. The previous reference of the last node should is also set to None since it becomes the previous node. Lastly, The next references of all the other nodes in the original list are swapped with the previous references.
The doubly linked list in Python is very useful especially once you need to perform a lot of inserts and delete operations. The hyperlinks to the previous and following nodes make it quite simple to add and delete new components without keeping tabs on their previous and next nodes.
In this guide, we saw how a doubly linked list could be implemented with Python.
– Một số Keyword tìm kiếm nhiều : ” Review Double-ended linked list Python tiên tiến và phát triển nhất , Share Link Cập nhật Double-ended linked list Python “.
You trọn vẹn có thể để lại Comments nếu gặp yếu tố chưa hiểu nhé.
#Doubleended #linked #list #Python