{"id":146,"date":"2024-01-16T18:41:32","date_gmt":"2024-01-16T18:41:32","guid":{"rendered":"https:\/\/onlinepythoncompiler.com\/blog\/?p=146"},"modified":"2024-01-16T18:41:32","modified_gmt":"2024-01-16T18:41:32","slug":"building-a-doublylinkedlist-in-python-append-method","status":"publish","type":"post","link":"https:\/\/onlinepythoncompiler.com\/blog\/building-a-doublylinkedlist-in-python-append-method\/","title":{"rendered":"Building a Doublylinkedlist in Python &#8211; &#8211; Append Method"},"content":{"rendered":"<html>\n<head>\n<title>Building a Doubly Linked List in Python &#8211; Append Method<\/title>\n<\/head>\n<body>\n\n<p>In this article, we will explore how to build a doubly linked list in Python and focus on the append method. A doubly linked list is a data structure that consists of a sequence of nodes, where each node contains both the data and a reference to the next and previous nodes. This allows for efficient insertion and deletion operations at both the beginning and end of the list.<\/p>\n<div>\n                    <figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/cdn-uploads\/gq\/2014\/03\/DLL_add_front1.png\" alt=\"Building a Doublylinkedlist in Python - - Append Method  \"\/><\/figure>\n                    \n                    \n                        <p>Credit: www.geeksforgeeks.org <\/p>\n                    \n                    <\/div><h2>What is a Doubly Linked List?<\/h2>\n<p>A doubly linked list is quite similar to a singly linked list, with the main difference being that each node in a doubly linked list contains a reference to both the next and previous nodes. This bidirectional connection between nodes provides added flexibility and allows for traversal in both directions.<\/p>\n<div>\n                    <figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/static.javatpoint.com\/python\/programs\/images\/python-program-to-insert-a-new-node-at-the-end-of-the-doubly-linked-list.png\" alt=\"Building a Doublylinkedlist in Python - - Append Method  \"\/><\/figure>\n                    \n                    \n                        <p>Credit: www.javatpoint.com <\/p>\n                    \n                    <\/div><h2>Implementation in Python<\/h2>\n<p>Let&#8217;s begin by defining a Node class that will represent each node in our doubly linked list:<\/p>\n<pre><code><table>\n<tr>\n<th>class Node:<\/th>\n<\/tr>\n<tr>\n<td>def __init__(self, data=None, prev=None, next=None):<\/td>\n<\/tr>\n<tr>\n<td>self.data = data<\/td>\n<\/tr>\n<tr>\n<td>self.prev = prev<\/td>\n<\/tr>\n<tr>\n<td>self.next = next<\/td>\n<\/tr>\n<\/table><\/code><\/pre>\n<p>Now, let&#8217;s create the DoublyLinkedList class, which will manage our doubly linked list:<\/p>\n<pre><code><table>\n<tr>\n<th>class DoublyLinkedList:<\/th>\n<\/tr>\n<tr>\n<td>def __init__(self):<\/td>\n<\/tr>\n<tr>\n<td>self.head = None<\/td>\n<\/tr>\n<\/table><\/code><\/pre>\n<div>\n    <figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n    <iframe loading=\"lazy\" title=\"Data Structures in Python: Doubly Linked Lists -- Append and Prepend\" width=\"720\" height=\"540\" src=\"https:\/\/www.youtube.com\/embed\/8kptHdreaTA?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n    <\/div><\/figure><br><\/div><h2>The Append Method<\/h2>\n<p>One of the fundamental operations in a linked list is appending a new node to the end. Let&#8217;s define an append method in our DoublyLinkedList class:<\/p>\n<pre><code><table>\n<tr>\n<th>def append(self, data):<\/th>\n<\/tr>\n<tr>\n<td>new_node = Node(data)<\/td>\n<\/tr>\n<tr>\n<td>if self.head is None:<\/td>\n<\/tr>\n<tr>\n<td>self.head = new_node<\/td>\n<\/tr>\n<tr>\n<td>else:<\/td>\n<\/tr>\n<tr>\n<td>current_node = self.head<\/td>\n<\/tr>\n<tr>\n<td>while current_node.next:<\/td>\n<\/tr>\n<tr>\n<td>current_node = current_node.next<\/td>\n<\/tr>\n<tr>\n<td>current_node.next = new_node<\/td>\n<\/tr>\n<tr>\n<td>new_node.prev = current_node<\/td>\n<\/tr>\n<\/table><\/code><\/pre>\n<p>The append method first checks if the head is empty. If it is, it assigns the new node as the head. Otherwise, it traverses the list until it reaches the last node and appends the new node after it. The new node&#8217;s previous reference is set to the current node.<\/p>\n<h2>Testing the Append Method<\/h2>\n<p>Let&#8217;s create an instance of the DoublyLinkedList class and test the append method:<\/p>\n<pre><code><table>\n<tr>\n<th># Creating a DoublyLinkedList instance<\/th>\n<\/tr>\n<tr>\n<td>my_list = DoublyLinkedList()<\/td>\n<\/tr>\n<tr>\n<th># Appending new nodes<\/th>\n<\/tr>\n<tr>\n<td>my_list.append(10)<\/td>\n<\/tr>\n<tr>\n<td>my_list.append(20)<\/td>\n<\/tr>\n<tr>\n<td>my_list.append(30)<\/td>\n<\/tr>\n<tr>\n<td>my_list.append(40)<\/td>\n<\/tr>\n<tr>\n<th># Displaying the list<\/th>\n<\/tr>\n<tr>\n<td>current_node = my_list.head<\/td>\n<\/tr>\n<tr>\n<td>while current_node:<\/td>\n<\/tr>\n<tr>\n<td>print(current_node.data)<\/td>\n<\/tr>\n<tr>\n<td>current_node = current_node.next<\/td>\n<\/tr>\n<\/table><\/code><\/pre>\n<p>The output should be:<\/p>\n<pre><code>10\n20\n30\n40<\/code><\/pre>\n<p>As seen in the code snippet above, we create a DoublyLinkedList instance and append four new nodes to it. Finally, we traverse the list and display the data of each node.<\/p>\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions For Building A Doublylinkedlist In Python &#8211; &#8211; Append Method  <\/h2>\n\n\n<h3 class=\"wp-block-heading\">How Does The Append Method Work In Python&#8217;s Doubly Linked List?<\/h3>\n\n\n<p>The append method in Python&#8217;s Doubly Linked List allows you to add a new node at the end of the list. By setting the next pointer of the last node to the new node, the new node becomes the last node in the list.<\/p>\n\n\n<h3 class=\"wp-block-heading\">What Is The Advantage Of Using The Append Method In A Doubly Linked List?<\/h3>\n\n\n<p>The append method enables efficient addition of new nodes to the end of the list. This advantage is particularly useful when the list is frequently updated, as it avoids the need to traverse the entire list to reach the end node.<\/p>\n\n\n<h3 class=\"wp-block-heading\">Can The Append Method Be Used To Add A New Node Anywhere Within The Doubly Linked List?<\/h3>\n\n\n<p>No, the append method is specifically designed to add a new node at the end of the list. If you want to add a new node at a different position, you would need to use other methods such as insert or add, which are specifically implemented for that purpose.<\/p>\n\n\n<h3 class=\"wp-block-heading\">How Do I Implement The Append Method In Python&#8217;s Doubly Linked List?<\/h3>\n\n\n<p>To implement the append method, you first need to check if the list is empty. If it is, you simply assign the new node as the head. Otherwise, you traverse the list to reach the last node and set its next pointer to the new node.<\/p>\n\n<h2>Conclusion<\/h2>\n<p>In this article, we learned about building a doubly linked list in Python and focusing on the append method. A doubly linked list is a flexible data structure that allows for efficient insertion and deletion operations at both ends of the list. The append method enables us to add new nodes to the end of the list in a straightforward manner.<\/p>\n<\/body>\n<\/html>","protected":false},"excerpt":{"rendered":"<p>Building a Doubly Linked List in Python &#8211; Append Method In this article, we will explore how to build a doubly linked list in Python and focus on the append method. A doubly linked list is a data structure that consists of a sequence of nodes, where each node contains both the data and a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":145,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-146","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":1,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":158,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/posts\/146\/revisions\/158"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/media\/145"}],"wp:attachment":[{"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onlinepythoncompiler.com\/blog\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}