{"id":26650,"date":"2017-12-22T19:45:51","date_gmt":"2017-12-22T14:15:51","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26650"},"modified":"2017-12-22T19:45:51","modified_gmt":"2017-12-22T14:15:51","slug":"java-algorithm-introduction-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-introduction-linked-list\/","title":{"rendered":"Java 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><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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/> <br\/>    \/* Linked list Node*\/<br\/>    class Node<br\/>    {<br\/>        int data;<br\/>        Node next;<br\/>          <br\/>        \/\/ Constructor to create a new node<br\/>        \/\/ Next is by default initialized<br\/>        \/\/ as null<br\/>        Node(int d) {data = d;}<br\/>    }<br\/>}<\/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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ A simple Java program to introduce a linked list<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/> <br\/>    \/* Linked list Node.  This inner class is made static so that<br\/>       main() can access it *\/<br\/>    static class Node {<br\/>        int data;<br\/>        Node next;<br\/>        Node(int d)  { data = d;  next=null; } \/\/ Constructor<br\/>    }<br\/> <br\/>    \/* method to create a simple linked list with 3 nodes*\/<br\/>    public static void main(String[] args)<br\/>    {<br\/>        \/* Start with the empty list. *\/<br\/>        LinkedList llist = new LinkedList();<br\/> <br\/>        llist.head  = new Node(1);<br\/>        Node second = new Node(2);<br\/>        Node third  = new Node(3);<br\/> <br\/>        \/* Three nodes have been allocated  dynamically.<br\/>          We have refernces to these three blocks as first,  <br\/>          second and third<br\/> <br\/>          llist.head        second              third<br\/>             |                |                  |<br\/>             |                |                  |<br\/>         +----+------+     +----+------+     +----+------+<br\/>         | 1  | null |     | 2  | null |     |  3 | null |<br\/>         +----+------+     +----+------+     +----+------+ *\/<br\/> <br\/>        llist.head.next = second; \/\/ Link first node with the second node<br\/> <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\/> <br\/>        second.next = third; \/\/ Link second node with the third node<br\/> <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\/>    }<br\/>}<\/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>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ A simple Java program for traversal of a linked list<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/> <br\/>    \/* Linked list Node.  This inner class is made static so that<br\/>       main() can access it *\/<br\/>    static class Node {<br\/>        int data;<br\/>        Node next;<br\/>        Node(int d)  { data = d;  next=null; } \/\/ Constructor<br\/>    }<br\/> <br\/>    \/* This function prints contents of linked list starting from head *\/<br\/>    public void printList()<br\/>    {<br\/>        Node n = head;<br\/>        while (n != null)<br\/>        {<br\/>            System.out.print(n.data+&quot; &quot;);<br\/>            n = n.next;<br\/>        }<br\/>    }<br\/> <br\/>    \/* method to create a simple linked list with 3 nodes*\/<br\/>    public static void main(String[] args)<br\/>    {<br\/>        \/* Start with the empty list. *\/<br\/>        LinkedList llist = new LinkedList();<br\/> <br\/>        llist.head       = new Node(1);<br\/>        Node second      = new Node(2);<br\/>        Node third       = new Node(3);<br\/> <br\/>        llist.head.next = second; \/\/ Link first node with the second node<br\/>        second.next = third; \/\/ Link first node with the second node<br\/> <br\/>        llist.printList();<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre> 1  2  3<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java 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":[2139,79476,79478],"tags":[79630,79636,79628,79627,79635,79624,72405,75623,73337,79631,79634,79623,79632,79633,73345,79629,79625,79626],"class_list":["post-26650","post","type-post","status-publish","format-standard","hentry","category-java","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\/26650","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=26650"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26650\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}