{"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=\u201dbanner\u201d]\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=\u201dbanner\u201d]\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Node%20class%0Aclass%20Node%3A%0A%20%20%0A%20%20%20%20%23%20Function%20to%20initialize%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%20%23%20Assign%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%20%20%23%20Initialize%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20next%20as%20null%0A%20%20%0A%23%20Linked%20List%20class%0Aclass%20LinkedList%3A%0A%20%20%20%20%0A%20%20%20%20%23%20Function%20to%20initialize%20the%20Linked%20%0A%20%20%20%20%23%20List%20object%0A%20%20%20%20def%20__init__(self)%3A%20%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20A%20simple%20Python%20program%20to%20introduce%20a%20linked%20list%0A%20%0A%23%20Node%20class%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialise%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%20%23%20Assign%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%20%20%23%20Initialize%20next%20as%20null%0A%20%0A%20%0A%23%20Linked%20List%20class%20contains%20a%20Node%20object%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%0A%23%20Code%20execution%20starts%20here%0Aif%20__name__%3D%3D\u2019__main__\u2019%3A%0A%20%0A%20%20%20%20%23%20Start%20with%20the%20empty%20list%0A%20%20%20%20llist%20%3D%20LinkedList()%0A%20%0A%20%20%20%20llist.head%20%20%3D%20Node(1)%0A%20%20%20%20second%20%3D%20Node(2)%0A%20%20%20%20third%20%20%3D%20Node(3)%0A%20%0A%20%20%20%20\u201d\u2019%0A%20%20%20%20Three%20nodes%20have%20been%20created.%0A%20%20%20%20We%20have%20references%20to%20these%20three%20blocks%20as%20first%2C%0A%20%20%20%20second%20and%20third%0A%20%0A%20%20%20%20llist.head%20%20%20%20%20%20%20%20second%20%20%20%20%20%20%20%20%20%20%20%20%20%20third%0A%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%0A%20%20%20%20%7C%201%20%20%7C%20None%20%7C%20%20%20%20%20%7C%202%20%20%7C%20None%20%7C%20%20%20%20%20%7C%20%203%20%7C%20None%20%7C%0A%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%0A%20%20%20%20\u201d\u2019%0A%20%0A%20%20%20%20llist.head.next%20%3D%20second%3B%20%23%20Link%20first%20node%20with%20second%20%0A%20%0A%20%20%20%20\u201d\u2019%0A%20%20%20%20Now%20next%20of%20first%20Node%20refers%20to%20second.%20%20So%20they%0A%20%20%20%20both%20are%20linked.%0A%20%0A%20%20%20%20llist.head%20%20%20%20%20%20%20%20second%20%20%20%20%20%20%20%20%20%20%20%20%20%20third%0A%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%0A%20%20%20%20%7C%201%20%20%7C%20%20o\u2014\u2014\u2013%3E%7C%202%20%20%7C%20null%20%7C%20%20%20%20%20%7C%20%203%20%7C%20null%20%7C%0A%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%0A%20%20%20%20\u201d\u2019%0A%20%0A%20%20%20%20second.next%20%3D%20third%3B%20%23%20Link%20second%20node%20with%20the%20third%20node%0A%20%0A%20%20%20%20\u201d\u2019%0A%20%20%20%20Now%20next%20of%20second%20Node%20refers%20to%20third.%20%20So%20all%20three%0A%20%20%20%20nodes%20are%20linked.%0A%20%0A%20%20%20%20llist.head%20%20%20%20%20%20%20%20second%20%20%20%20%20%20%20%20%20%20%20%20%20%20third%0A%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%0A%20%20%20%20%7C%201%20%20%7C%20%20o\u2014\u2014\u2013%3E%7C%202%20%20%7C%20%20o\u2014\u2014\u2013%3E%7C%20%203%20%7C%20null%20%7C%0A%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%0A%20%20%20%20\u201d'\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\n<p><strong>Python Programming:<\/strong><\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20A%20simple%20Python%20program%20for%20traversal%20of%20a%20linked%20list%0A%20%0A%23%20Node%20class%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialise%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%20%23%20Assign%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%20%20%23%20Initialize%20next%20as%20null%0A%20%0A%20%0A%23%20Linked%20List%20class%20contains%20a%20Node%20object%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20This%20function%20prints%20contents%20of%20linked%20list%0A%20%20%20%20%23%20starting%20from%20head%0A%20%20%20%20def%20printList(self)%3A%0A%20%20%20%20%20%20%20%20temp%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20(temp)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20temp.data%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%0A%20%0A%20%0A%23%20Code%20execution%20starts%20here%0Aif%20__name__%3D%3D\u2019__main__\u2019%3A%0A%20%0A%20%20%20%20%23%20Start%20with%20the%20empty%20list%0A%20%20%20%20llist%20%3D%20LinkedList()%0A%20%0A%20%20%20%20llist.head%20%20%3D%20Node(1)%0A%20%20%20%20second%20%3D%20Node(2)%0A%20%20%20%20third%20%20%3D%20Node(3)%0A%20%0A%20%20%20%20llist.head.next%20%3D%20second%3B%20%23%20Link%20first%20node%20with%20second%0A%20%20%20%20second.next%20%3D%20third%3B%20%23%20Link%20second%20node%20with%20the%20third%20node%0A%20%0A%20%20%20%20llist.printList()\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}