{"id":26649,"date":"2017-10-15T14:34:38","date_gmt":"2017-10-15T09:04:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26649"},"modified":"2017-10-15T14:34:38","modified_gmt":"2017-10-15T09:04:38","slug":"c-algorithm-introduction-linked-list-set-1","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-introduction-linked-list-set-1\/","title":{"rendered":"C Algorithm &#8211; Introduction for Linked List | Set 1"},"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>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p><strong>[ad type=\u201dbanner\u201d]<\/strong><\/p>\n<p><strong>Why Linked List?<\/strong><\/p>\n<p>Arrays 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<p><strong>[ad type=\u201dbanner\u201d]<\/strong><\/p>\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>[ad type=\u201dbanner\u201d]<\/strong><\/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<p><strong>[ad type=\u201dbanner\u201d]<\/strong><\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20A%20linked%20list%20node%0Astruct%20Node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20Node%20*next%3B%0A%7D%3B\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>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20A%20simple%20C%20program%20to%20introduce%0A%2F%2F%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0Astruct%20Node%20%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20Node%20*next%3B%0A%7D%3B%0A%20%0A%2F%2F%20Program%20to%20create%20a%20simple%20linked%20%0A%2F%2F%20list%20with%203%20nodes%0Aint%20main()%0A%7B%0A%20%20struct%20Node*%20head%20%3D%20NULL%3B%0A%20%20struct%20Node*%20second%20%3D%20NULL%3B%0A%20%20struct%20Node*%20third%20%3D%20NULL%3B%0A%20%20%20%0A%20%20%2F%2F%20allocate%203%20nodes%20in%20the%20heap%20%20%0A%20%20head%20%3D%20(struct%20Node*)malloc(sizeof(struct%20Node))%3B%20%0A%20%20second%20%3D%20(struct%20Node*)malloc(sizeof(struct%20Node))%3B%0A%20%20third%20%3D%20(struct%20Node*)malloc(sizeof(struct%20Node))%3B%0A%20%0A%20%20%2F*%20Three%20blocks%20have%20been%20allocated%20%20dynamically.%20%0A%20%20%20%20%20We%20have%20pointers%20to%20these%20three%20blocks%20as%20first%2C%20second%20and%20third%20%20%20%20%20%0A%20%20%20%20%20%20%20head%20%20%20%20%20%20%20%20%20%20%20second%20%20%20%20%20%20%20%20%20%20%20third%0A%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%7C%0A%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%7C%0A%20%20%20%20%2B\u2014%2B\u2014\u2013%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%0A%20%20%20%20%7C%20%23%20%20%7C%20%23%20%20%7C%20%20%20%20%20%7C%20%23%20%20%7C%20%23%20%20%7C%20%20%20%20%20%7C%20%20%23%20%7C%20%20%23%20%7C%0A%20%20%20%20%2B\u2014%2B\u2014\u2013%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%0A%20%20%20%20%0A%20%20%20%23%20represents%20any%20random%20value.%0A%20%20%20Data%20is%20random%20because%20we%20haven%E2%80%99t%20assigned%20anything%20yet%20%20*%2F%0A%20%20%20%0A%20%20head-%3Edata%20%3D%201%3B%20%2F%2Fassign%20data%20in%20first%20node%0A%20%20head-%3Enext%20%3D%20second%3B%20%2F%2F%20Link%20first%20node%20with%20the%20second%20node%0A%20%20%20%0A%20%20%2F*%20data%20has%20been%20assigned%20to%20data%20part%20of%20first%20block%20(block%20%0A%20%20%20%20pointed%20by%20head).%20%20And%20next%20pointer%20of%20first%20block%20points%20to%0A%20%20%20%20second.%20%20So%20they%20both%20are%20linked.%0A%20%0A%20%20%20%20%20%20%20head%20%20%20%20%20%20%20%20%20%20second%20%20%20%20%20%20%20%20%20third%0A%20%20%20%20%20%20%20%20%7C%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%7C%0A%20%20%20%20%20%20%20%20%7C%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%7C%0A%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%20%20%20%20%20%2B\u2014\u2013%2B\u2014-%2B%0A%20%20%20%20%7C%201%20%20%7C%20o\u2014\u2013%3E%7C%20%23%20%20%7C%20%23%20%20%7C%20%20%20%20%20%7C%20%20%23%20%20%7C%20%23%20%20%7C%0A%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%20%20%20%20%20%2B\u2014\u2013%2B\u2014-%2B%20%20%20%20%0A%20%20*%2F%20%0A%20%20%20%0A%20%20second-%3Edata%20%3D%202%3B%20%2F%2Fassign%20data%20to%20second%20node%0A%20%20second-%3Enext%20%3D%20third%3B%20%2F%2F%20Link%20second%20node%20with%20the%20third%20node%0A%20%20%20%0A%20%20%2F*%20data%20has%20been%20assigned%20to%20data%20part%20of%20second%20block%20(block%20pointed%20by%0A%20%20%20%20%20second).%20And%20next%20pointer%20of%20the%20second%20block%20points%20to%20third%20block.%20%20%0A%20%20%20%20So%20all%20three%20blocks%20are%20linked.%0A%20%20%20%0A%20%20%20%20%20%20%20head%20%20%20%20%20%20%20%20%20second%20%20%20%20%20%20%20%20%20third%0A%20%20%20%20%20%20%20%20%7C%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%7C%0A%20%20%20%20%20%20%20%20%7C%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%7C%0A%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%0A%20%20%20%20%7C%201%20%20%7C%20o\u2014\u2013%3E%7C%202%20%7C%20o\u2014\u2013%3E%20%7C%20%20%23%20%7C%20%20%23%20%7C%0A%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014-%2B\u2014-%2B%20%20%20%20%20%20*%2F%20%20%20%0A%20%20%20%0A%20%20third-%3Edata%20%3D%203%3B%20%2F%2Fassign%20data%20to%20third%20node%0A%20%20third-%3Enext%20%3D%20NULL%3B%0A%20%20%20%0A%20%20%2F*%20data%20has%20been%20assigned%20to%20data%20part%20of%20third%20block%20(block%20pointed%0A%20%20%20%20by%20third).%20And%20next%20pointer%20of%20the%20third%20block%20is%20made%20NULL%20to%20indicate%0A%20%20%20%20that%20the%20linked%20list%20is%20terminated%20here.%0A%20%0A%20%20%20%20%20We%20have%20the%20linked%20list%20ready.%20%20%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20head%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%20%0A%20%20%20%20%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%0A%20%20%20%20%20%20%20%20%7C%201%20%20%7C%20o\u2014\u2013%3E%7C%20%202%20%20%7C%20o\u2014\u2013%3E%20%7C%20%203%20%7C%20NULL%20%7C%0A%20%20%20%20%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%2B\u2014%2B\u2014%2B%20%20%20%20%20%20%20%2B\u2014-%2B\u2014\u2014%2B%20%20%20%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20%0A%20%20%20%20Note%20that%20only%20head%20is%20sufficient%20to%20represent%20the%20whole%20list.%20%20We%20can%20%0A%20%20%20%20traverse%20the%20complete%20list%20by%20following%20next%20pointers.%20%20%20%20*%2F%20%20%20%20%20%0A%20%0A%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>[ad type=\u201dbanner\u201d]<\/strong><\/p>\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<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20A%20simple%20C%20program%20for%20traversal%20of%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%0Astruct%20Node%20%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20Node%20*next%3B%0A%7D%3B%0A%20%0A%2F%2F%20This%20function%20prints%20contents%20of%20linked%20list%20starting%20from%20%0A%2F%2F%20the%20given%20node%0Avoid%20printList(struct%20Node%20*n)%0A%7B%0A%20%20while%20(n%20!%3D%20NULL)%0A%20%20%7B%0A%20%20%20%20%20printf(%22%20%25d%20%22%2C%20n-%3Edata)%3B%0A%20%20%20%20%20n%20%3D%20n-%3Enext%3B%0A%20%20%7D%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20struct%20Node*%20head%20%3D%20NULL%3B%0A%20%20struct%20Node*%20second%20%3D%20NULL%3B%0A%20%20struct%20Node*%20third%20%3D%20NULL%3B%0A%20%20%20%0A%20%20%2F%2F%20allocate%203%20nodes%20in%20the%20heap%20%20%0A%20%20head%20%20%3D%20(struct%20Node*)malloc(sizeof(struct%20Node))%3B%20%0A%20%20second%20%3D%20(struct%20Node*)malloc(sizeof(struct%20Node))%3B%0A%20%20third%20%20%3D%20(struct%20Node*)malloc(sizeof(struct%20Node))%3B%0A%20%20%0A%20%20head-%3Edata%20%3D%201%3B%20%2F%2Fassign%20data%20in%20first%20node%0A%20%20head-%3Enext%20%3D%20second%3B%20%2F%2F%20Link%20first%20node%20with%20second%20%20%20%0A%20%20%0A%20%20second-%3Edata%20%3D%202%3B%20%2F%2Fassign%20data%20to%20second%20node%0A%20%20second-%3Enext%20%3D%20third%3B%20%20%0A%20%20%0A%20%20third-%3Edata%20%3D%203%3B%20%2F%2Fassign%20data%20to%20third%20node%0A%20%20third-%3Enext%20%3D%20NULL%3B%0A%20%20%20%0A%20%20printList(head)%3B%0A%20%20%0A%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Introduction for Linked List | Set 1 &#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":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[79630,79636,79628,79627,79635,79624,72405,75623,73337,79631,79634,79623,79632,79633,73345,79629,79625,79626],"class_list":["post-26649","post","type-post","status-publish","format-standard","hentry","category-linked-list","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\/26649","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=26649"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26649\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}