{"id":26667,"date":"2017-12-22T19:48:00","date_gmt":"2017-12-22T14:18:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26667"},"modified":"2017-12-22T19:48:00","modified_gmt":"2017-12-22T14:18:00","slug":"python-algorithm-introduction-linked-list-set","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-algorithm-introduction-linked-list-set\/","title":{"rendered":"Python Algorithm &#8211; Introduction for Linked List | Set"},"content":{"rendered":"<p>Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26683\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist.png\" alt=\"C Algorithm - Introduction for Linked List | Set 1\" width=\"759\" height=\"169\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist.png 759w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist-300x67.png 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/p>\n<p><strong>Why Linked List?<\/strong><br \/>\nArrays can be used to store linear data of similar types, but arrays have following limitations.<br \/>\n<strong>1)<\/strong> The size of the arrays is fixed: So we must know the upper limit on the number of elements in advance. Also, generally, the allocated memory is equal to the upper limit irrespective of the usage.<br \/>\n<strong>2)<\/strong> Inserting a new element in an array of elements is expensive, because room has to be created for the new elements and to create room existing elements have to shifted.<\/p>\n<p>For example, in a system if we maintain a sorted list of IDs in an array id[].<\/p>\n<p>id[] = [1000, 1010, 1050, 2000, 2040].<\/p>\n<p>And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).<br \/>\nDeletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Advantages over arrays<\/strong><br \/>\n<strong>1)<\/strong> Dynamic size<br \/>\n<strong>2)<\/strong> Ease of insertion\/deletion<\/p>\n<p><strong>Drawbacks:<\/strong><br \/>\n<strong>1)<\/strong> Random access is not allowed. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists.<br \/>\n<strong>2)<\/strong> Extra memory space for a pointer is required with each element of the list.<\/p>\n<p><strong>Representation in C:<\/strong><br \/>\nA linked list is represented by a pointer to the first node of the linked list. The first node is called head. If the linked list is empty, then value of head is NULL.<br \/>\nEach node in a list consists of at least two parts:<br \/>\n1) data<br \/>\n2) pointer to the next node<br \/>\nIn C, we can represent a node using structures. Below is an example of a linked list node with an integer data.<br \/>\nIn Java, LinkedList can be represented as a class and a Node as a separate class. The LinkedList class contains a reference of Node class type.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Python Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Node class<br\/>class Node:<br\/>  <br\/>    # Function to initialize the node object<br\/>    def __init__(self, data):<br\/>        self.data = data  # Assign data<br\/>        self.next = None  # Initialize <br\/>                          # next as null<br\/>  <br\/># Linked List class<br\/>class LinkedList:<br\/>    <br\/>    # Function to initialize the Linked <br\/>    # List object<br\/>    def __init__(self): <br\/>        self.head = None<\/code><\/pre> <\/div>\n<p><strong>First Simple Linked List in C<\/strong> Let us create a simple linked list with 3 nodes.<\/p>\n<p><strong>Python Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># A simple Python program to introduce a linked list<br\/> <br\/># Node class<br\/>class Node:<br\/> <br\/>    # Function to initialise the node object<br\/>    def __init__(self, data):<br\/>        self.data = data  # Assign data<br\/>        self.next = None  # Initialize next as null<br\/> <br\/> <br\/># Linked List class contains a Node object<br\/>class LinkedList:<br\/> <br\/>    # Function to initialize head<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/> <br\/># Code execution starts here<br\/>if __name__==&#039;__main__&#039;:<br\/> <br\/>    # Start with the empty list<br\/>    llist = LinkedList()<br\/> <br\/>    llist.head  = Node(1)<br\/>    second = Node(2)<br\/>    third  = Node(3)<br\/> <br\/>    &#039;&#039;&#039;<br\/>    Three nodes have been created.<br\/>    We have references to these three blocks as first,<br\/>    second and third<br\/> <br\/>    llist.head        second              third<br\/>         |                |                  |<br\/>         |                |                  |<br\/>    +----+------+     +----+------+     +----+------+<br\/>    | 1  | None |     | 2  | None |     |  3 | None |<br\/>    +----+------+     +----+------+     +----+------+<br\/>    &#039;&#039;&#039;<br\/> <br\/>    llist.head.next = second; # Link first node with second <br\/> <br\/>    &#039;&#039;&#039;<br\/>    Now next of first Node refers to second.  So they<br\/>    both are linked.<br\/> <br\/>    llist.head        second              third<br\/>         |                |                  |<br\/>         |                |                  |<br\/>    +----+------+     +----+------+     +----+------+<br\/>    | 1  |  o--------&gt;| 2  | null |     |  3 | null |<br\/>    +----+------+     +----+------+     +----+------+ <br\/>    &#039;&#039;&#039;<br\/> <br\/>    second.next = third; # Link second node with the third node<br\/> <br\/>    &#039;&#039;&#039;<br\/>    Now next of second Node refers to third.  So all three<br\/>    nodes are linked.<br\/> <br\/>    llist.head        second              third<br\/>         |                |                  |<br\/>         |                |                  |<br\/>    +----+------+     +----+------+     +----+------+<br\/>    | 1  |  o--------&gt;| 2  |  o--------&gt;|  3 | null |<br\/>    +----+------+     +----+------+     +----+------+ <br\/>    &#039;&#039;&#039;<\/code><\/pre> <\/div>\n<p><strong>Linked List Traversal<\/strong><br \/>\nIn the previous program, we have created a simple linked list with three nodes. Let us traverse the created list and print the data of each node. For traversal, let us write a general purpose function printList() that prints any given list.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Python Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># A simple Python program for traversal of a linked list<br\/> <br\/># Node class<br\/>class Node:<br\/> <br\/>    # Function to initialise the node object<br\/>    def __init__(self, data):<br\/>        self.data = data  # Assign data<br\/>        self.next = None  # Initialize next as null<br\/> <br\/> <br\/># Linked List class contains a Node object<br\/>class LinkedList:<br\/> <br\/>    # Function to initialize head<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/>    # This function prints contents of linked list<br\/>    # starting from head<br\/>    def printList(self):<br\/>        temp = self.head<br\/>        while (temp):<br\/>            print temp.data,<br\/>            temp = temp.next<br\/> <br\/> <br\/># Code execution starts here<br\/>if __name__==&#039;__main__&#039;:<br\/> <br\/>    # Start with the empty list<br\/>    llist = LinkedList()<br\/> <br\/>    llist.head  = Node(1)<br\/>    second = Node(2)<br\/>    third  = Node(3)<br\/> <br\/>    llist.head.next = second; # Link first node with second<br\/>    second.next = third; # Link second node with the third node<br\/> <br\/>    llist.printList()<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre> 1  2  3<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python Algorithm &#8211; Introduction for Linked List &#8211; Linked List &#8211; Like arrays, Linked List is a linear data structure. Unlike arrays<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,4148,79478],"tags":[79630,79636,79628,79627,79635,79624,72405,75623,73337,79631,79634,79623,79632,79633,73345,79629,79625,79626],"class_list":["post-26667","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-python","category-singly-linked-list","tag-c-linked-list-class-implementation","tag-c-linked-list-library","tag-circular-linked-list-in-data-structure","tag-doubly-linked-list-in-data-structure","tag-insertion-and-deletion-in-linked-list-in-c-program","tag-link-list-c","tag-linked-list-algorithm","tag-linked-list-in-data-structure-pdf","tag-linked-list-program-in-c","tag-linked-list-program-in-c-pdf","tag-linked-list-program-in-c-using-struct","tag-linked-list-tutorial","tag-singly-linked-list-c-code","tag-singly-linked-list-c-simple-program","tag-singly-linked-list-in-data-structure","tag-singly-linked-list-program-in-c-using-class","tag-types-of-linked-list","tag-types-of-linked-list-in-data-structure-pdf"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26667"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26667\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}