<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sri Ramanuja Seva</title>
    <description>The latest articles on DEV Community by Sri Ramanuja Seva (@sriramanujaseva).</description>
    <link>https://dev.to/sriramanujaseva</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4266270%2F62fdf74e-b9e5-4d1e-81fe-2b607165e24e.png</url>
      <title>DEV Community: Sri Ramanuja Seva</title>
      <link>https://dev.to/sriramanujaseva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sriramanujaseva"/>
    <language>en</language>
    <item>
      <title>Introduction to Linked Lists — Creation and Traversal</title>
      <dc:creator>Sri Ramanuja Seva</dc:creator>
      <pubDate>Thu, 26 Nov 2026 10:21:22 +0000</pubDate>
      <link>https://dev.to/sriramanujaseva/introduction-to-linked-lists-creation-and-traversal-2lmn</link>
      <guid>https://dev.to/sriramanujaseva/introduction-to-linked-lists-creation-and-traversal-2lmn</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyuquzs49l4u0wnneyvdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyuquzs49l4u0wnneyvdq.png" alt=" " width="799" height="81"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Data Structures &amp;amp; Algorithms · A Beginner's Guide&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction to &lt;em&gt;Linked Lists&lt;/em&gt; — Creation and Traversal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From arrays to nodes, pointers, linked-list creation, and traversal in C.&lt;/p&gt;

&lt;p&gt;Earlier, we looked at &lt;strong&gt;arrays&lt;/strong&gt;, which are data structures where elements are stored in contiguous memory locations. Here, we shall look at another data structure where elements &lt;strong&gt;need not&lt;/strong&gt; be stored in contiguous memory locations. This data structure is called a &lt;strong&gt;linked list&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  At a glance · Contents &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Click a topic to jump to that section.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduction: From Arrays to Linked Lists&lt;/li&gt;
&lt;li&gt;Structure of Each Node of a Linked List&lt;/li&gt;
&lt;li&gt;Advantages of Linked Lists over Arrays&lt;/li&gt;
&lt;li&gt;Advantages of Arrays over Linked Lists&lt;/li&gt;
&lt;li&gt;C Program: Create and Display a Linked List&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Introduction: From Arrays to Linked Lists &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;There are &lt;strong&gt;3 elements&lt;/strong&gt; in this array, having the string data values: &lt;strong&gt;“Rama”&lt;/strong&gt;, &lt;strong&gt;“Krishna”&lt;/strong&gt;, &lt;strong&gt;“Govinda”&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array A: elements are stored one after another&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7073eks055j12g9wobln.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F7073eks055j12g9wobln.png" alt=" " width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that the data values are stored in contiguous memory locations.&lt;/p&gt;

&lt;p&gt;Each element is allocated &lt;strong&gt;16 bytes&lt;/strong&gt; of memory in this illustration. The elements are stored one after the other in contiguous memory locations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;A[0]&lt;/code&gt; occupies the address range &lt;strong&gt;1200 to 1215&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;A[1]&lt;/code&gt; occupies the address range &lt;strong&gt;1216 to 1231&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;A[2]&lt;/code&gt; occupies the address range &lt;strong&gt;1232 to 1247&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If there were an &lt;code&gt;A[3]&lt;/code&gt;, it would occupy the address range &lt;strong&gt;1248 to 1263&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The addresses are illustrative. Here, we are imagining something like &lt;code&gt;char A[3][16]&lt;/code&gt;. So each array element is allotted a 16-byte block, even though the actual stored string may use fewer bytes. The purpose here is to show that the array elements occupy contiguous memory locations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;strong&gt;linked list&lt;/strong&gt; differs from an array in that the elements, called &lt;strong&gt;nodes&lt;/strong&gt;, need not be stored in contiguous memory locations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked-list nodes may occupy different memory locations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fat38nv85fsffvjw2yhub.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fat38nv85fsffvjw2yhub.png" alt=" " width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first node, &lt;strong&gt;“Rama”&lt;/strong&gt;, is stored at address &lt;strong&gt;1200&lt;/strong&gt;. The 2nd node is at &lt;strong&gt;1104&lt;/strong&gt;, and the 3rd node at &lt;strong&gt;1264&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As we can see, the nodes are not stored in contiguous memory locations, unlike arrays.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The addresses above are only examples to illustrate non-contiguous storage in a linked list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;↑ Back to Contents&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Structure of Each Node of a Linked List &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; In arrays, we can easily compute the memory location where a particular element is stored, from the starting address of the array.&lt;/p&gt;

&lt;p&gt;For example, consider an array &lt;code&gt;A&lt;/code&gt; which starts at address &lt;strong&gt;1400&lt;/strong&gt;. Each element is allocated &lt;strong&gt;8 bytes&lt;/strong&gt; of storage. Therefore, we can confidently say that element &lt;code&gt;A[i]&lt;/code&gt; is stored starting at address &lt;strong&gt;1400 + (8 × i)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, in order to access (read the value, or modify, or whatever) element &lt;code&gt;A[i]&lt;/code&gt;, we can directly go to memory location &lt;strong&gt;1400 + (8 × i)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about linked lists?&lt;/strong&gt; Since the nodes are not stored in contiguous locations, and seem to be stored at arbitrary locations, how would we access any specific node?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; We make each node point to the next node; that is, each node stores the &lt;strong&gt;address of the next node&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Each node has &lt;strong&gt;two fields&lt;/strong&gt; or &lt;strong&gt;two members&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;data&lt;/strong&gt; — this is what we saw earlier itself; for example, “Rama”, “Krishna”, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;next pointer&lt;/strong&gt; — pointer to the next node in the linked list; that is, the &lt;strong&gt;memory address of the next node&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the individual nodes in a linked list are &lt;strong&gt;“linked”&lt;/strong&gt; by way of the next pointer field, which is present in every node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Three nodes linked by their next pointers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2e760l0km96e8bn8bgqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2e760l0km96e8bn8bgqu.png" alt=" " width="800" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each node in the illustration above has a &lt;strong&gt;data value&lt;/strong&gt; and a &lt;strong&gt;next pointer&lt;/strong&gt;. By means of the next pointer, we can go from one node to the next.&lt;/p&gt;

&lt;p&gt;There is no node after node 3. Hence, the &lt;strong&gt;next pointer&lt;/strong&gt; is marked as &lt;strong&gt;NULL&lt;/strong&gt;, signifying the last node of the linked list.&lt;/p&gt;

&lt;h3&gt;
  
  
  What exactly is stored in each next pointer?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The same nodes, now showing the stored addresses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjotz8w5o425639lioep7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjotz8w5o425639lioep7.png" alt=" " width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node 1 is stored starting at memory address &lt;strong&gt;1200&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;next pointer&lt;/strong&gt; field of node 1 has the address &lt;strong&gt;1104&lt;/strong&gt;, signifying that node 2 is stored at that location.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;next pointer&lt;/strong&gt; field of node 2 has the address &lt;strong&gt;1264&lt;/strong&gt;, indicating that node 3 is stored at that location.&lt;/li&gt;
&lt;li&gt;Node 3 is the last node in the list. Hence, the &lt;strong&gt;next pointer&lt;/strong&gt; field of node 3 is represented as &lt;strong&gt;NULL&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In linked lists, there is typically a &lt;strong&gt;head pointer&lt;/strong&gt; pointing to the first node of the list. We shall explain this further in the C program below.&lt;/p&gt;

&lt;p&gt;↑ Back to Contents&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Advantages of Linked Lists over Arrays &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; Why do we need linked lists? Are arrays not sufficient?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. Efficient insertion and deletion at the beginning
&lt;/h3&gt;

&lt;p&gt;Let's say we want to insert an element at index 0 of an array already having several elements. We would need to &lt;strong&gt;shift all elements (to the right)&lt;/strong&gt; to insert this element. Hence, insertion takes &lt;strong&gt;O(n) time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, deletion at the beginning of an array also takes &lt;strong&gt;O(n) time&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Array: insert at the beginning
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Shift existing elements&lt;/strong&gt; to the right before placing the new element. Time: &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Linked list: insert at the beginning
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Update the new node's next pointer and head.&lt;/strong&gt; Time: &lt;strong&gt;O(1)&lt;/strong&gt;, when the new node is available.&lt;/p&gt;

&lt;p&gt;In contrast, inserting a node at the beginning of a linked list takes &lt;strong&gt;O(1) time&lt;/strong&gt;. The same goes for deleting a node at the beginning.&lt;/p&gt;

&lt;p&gt;To insert a node at the beginning, we simply need to update the &lt;strong&gt;next pointer&lt;/strong&gt; of a single node (and also need to update the &lt;strong&gt;head pointer&lt;/strong&gt;, which we shall explain later).&lt;/p&gt;

&lt;p&gt;Similarly, deletion at the beginning of the list involves only updating a &lt;strong&gt;next pointer&lt;/strong&gt; and the &lt;strong&gt;head pointer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Hence, for insertions and deletions, linked lists are &lt;strong&gt;much more efficient&lt;/strong&gt; than ordinary arrays.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important distinction:&lt;/strong&gt; The O(1) advantage applies to insertion and deletion &lt;em&gt;at the beginning&lt;/em&gt; (and certain positions where the relevant pointers are already available). Finding an arbitrary position first may take &lt;strong&gt;O(n)&lt;/strong&gt; time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. No requirement for one large contiguous memory block
&lt;/h3&gt;

&lt;p&gt;Array elements are stored in contiguous memory locations. Hence, in order to accommodate a very large array, we need a &lt;strong&gt;sufficiently large empty block of memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In contrast, &lt;strong&gt;linked-list nodes need not be stored in contiguous memory locations&lt;/strong&gt;. Hence, it is easier to accommodate individual nodes at various suitable memory locations.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Convenient dynamic growth and shrinking
&lt;/h3&gt;

&lt;p&gt;For ordinary arrays in languages like C, the size of the array (that is, the number of elements) is usually specified at the time of creation itself. Dynamically extending the array (that is, adding more elements than the capacity) is &lt;strong&gt;not that simple&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linked lists&lt;/strong&gt; can readily, easily dynamically grow or shrink. No need to allocate any space beforehand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Languages like C++ and Python provide &lt;strong&gt;vectors and lists&lt;/strong&gt;, which are arrays that can be dynamically resized. (In C++, the corresponding container is &lt;code&gt;vector&lt;/code&gt;; in Python, it is &lt;code&gt;list&lt;/code&gt;.)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;↑ Back to Contents&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Advantages of Arrays over Linked Lists &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; So, linked lists appear quite advantageous compared to arrays. &lt;strong&gt;Why then use arrays at all?&lt;/strong&gt; Are not linked lists sufficient?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Answer: Advantages of arrays over linked lists:&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  1. Direct access to elements through their indices
&lt;/h3&gt;

&lt;p&gt;Array elements are stored in contiguous memory locations. Hence, we know that the address of an element at index &lt;code&gt;i&lt;/code&gt; can be directly computed from the base address of the array and the size of an element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: array A begins at address 1400, with 8 bytes per element&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm1bx0o8lmohfkdvppdpb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm1bx0o8lmohfkdvppdpb.png" alt=" " width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Address of A[i] = 1400 + (8 × i)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Address of element at index i = (Base address of array) + (i × size of an element)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thus, accessing &lt;code&gt;A[i]&lt;/code&gt; takes &lt;strong&gt;O(1) time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In contrast, to access a node at index &lt;code&gt;i&lt;/code&gt; of a &lt;strong&gt;linked list&lt;/strong&gt;, we must start at the &lt;strong&gt;head&lt;/strong&gt; and traverse node-by-node until we reach the desired node. Therefore, accessing an arbitrary node in a linked list takes &lt;strong&gt;O(n) time&lt;/strong&gt; in the worst case.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Less memory overhead
&lt;/h3&gt;

&lt;p&gt;In arrays, we mainly just store the &lt;strong&gt;data values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In contrast, in linked lists, we also need to store a &lt;strong&gt;pointer (the next pointer)&lt;/strong&gt; along with the data, for every node. Plus, padding may also be involved. Hence, &lt;strong&gt;more memory per node&lt;/strong&gt; is required.&lt;/p&gt;

&lt;p&gt;For example, in a 64-bit system where integer size is &lt;strong&gt;4 bytes&lt;/strong&gt; and pointer size is &lt;strong&gt;8 bytes&lt;/strong&gt;:&lt;/p&gt;

&lt;h4&gt;
  
  
  Array element
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;data · 4 bytes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Exactly 4 bytes per &lt;code&gt;int&lt;/code&gt; element&lt;/p&gt;

&lt;h4&gt;
  
  
  Linked-list node
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;data 4 B + pointer 8 B / + padding 4 B&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Typically 16 bytes per node, excluding allocator overhead&lt;/p&gt;

&lt;p&gt;Hence, for the same number of integer data values, a linked list will generally require more memory than an ordinary array.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Much more cache-friendly
&lt;/h3&gt;

&lt;p&gt;Array elements are stored in contiguous memory locations. When the CPU reads one element, it loads nearby elements into its cache as well.&lt;/p&gt;

&lt;p&gt;Hence, traversing an array can be done very quickly.&lt;/p&gt;

&lt;p&gt;In contrast, in a linked list, the nodes are scattered at different memory locations. Hence, cache performance is not that good.&lt;/p&gt;

&lt;p&gt;Therefore, even though traversal takes theoretically &lt;strong&gt;O(n)&lt;/strong&gt; time for both arrays and linked lists, it is quite faster in practice for arrays than linked lists.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Binary search on sorted arrays
&lt;/h3&gt;

&lt;p&gt;Binary search, which allows for very efficient &lt;strong&gt;O(log n)&lt;/strong&gt; searching, can be applied on &lt;strong&gt;sorted arrays&lt;/strong&gt; (by repeatedly exploring the middle-index element and reducing the search space by half).&lt;/p&gt;

&lt;p&gt;In contrast, there is no direct way to access the middle node of a linked list. Hence, conventional binary search won't be useful on linked lists.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Simpler to use
&lt;/h3&gt;

&lt;p&gt;For example, consider the code to traverse an array and a linked list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Array traversal and printing:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Linked-list traversal and printing:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see, the coding is &lt;strong&gt;much simpler in the case of arrays&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;↑ Back to Contents&lt;/p&gt;

&lt;h2&gt;
  
  
  5. C Program: Create and Display a Linked List &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;We shall write a &lt;strong&gt;simple C program&lt;/strong&gt; to create a linked list having &lt;strong&gt;3 nodes&lt;/strong&gt;, and then we'll display the contents of the list.&lt;/p&gt;

&lt;p&gt;Each node of our list will store an &lt;strong&gt;integer data value&lt;/strong&gt;. And we already know that each node stores a &lt;strong&gt;pointer to the next node&lt;/strong&gt; in the list.&lt;/p&gt;

&lt;p&gt;We'll use the &lt;strong&gt;struct data type&lt;/strong&gt; for each node. It will have &lt;strong&gt;two members&lt;/strong&gt;: an &lt;code&gt;int&lt;/code&gt; data value, and a pointer to the next node of the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;next&lt;/code&gt; is the pointer to the next node of the list. That is, &lt;code&gt;next&lt;/code&gt; stores the memory address of the next node in the list.&lt;/p&gt;

&lt;p&gt;Each node in the list is of type &lt;code&gt;struct Node&lt;/code&gt;. Therefore, &lt;code&gt;next&lt;/code&gt; is a pointer of type &lt;code&gt;struct Node *&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For linked lists, &lt;strong&gt;dynamic memory allocation&lt;/strong&gt; is typically used. In C, we do this using &lt;code&gt;malloc()&lt;/code&gt;, which is from &lt;code&gt;stdlib.h&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At times, when we use &lt;code&gt;malloc()&lt;/code&gt; to dynamically allocate memory, memory allocation may fail. We check for that, and we release any memory we may have previously allocated dynamically, and terminate the program with an error code.&lt;/p&gt;

&lt;p&gt;After successfully allocating memory, we need to populate the nodes with data. For our simple program, we'll use any 3 integer values.&lt;/p&gt;

&lt;p&gt;Then we need to link the nodes. We do this by properly assigning values to the &lt;strong&gt;next pointer&lt;/strong&gt; of each node. Note that the next pointer of the last node has the value &lt;strong&gt;NULL&lt;/strong&gt;, signifying the end of the list.&lt;/p&gt;

&lt;p&gt;In linked lists, there is typically a &lt;strong&gt;head pointer&lt;/strong&gt;, pointing to the first node of the list.&lt;/p&gt;

&lt;p&gt;For displaying the contents of the list, we start from &lt;code&gt;head&lt;/code&gt;, and traverse the list by means of the &lt;strong&gt;next pointer&lt;/strong&gt; in each node. We traverse until we encounter &lt;strong&gt;NULL&lt;/strong&gt; (indicating that there are no more nodes).&lt;/p&gt;

&lt;p&gt;We assigned memory dynamically for 3 nodes using &lt;code&gt;malloc()&lt;/code&gt;. So, after we've completed our use of the list, it is good programming practice to release the memory, to prevent memory leaks. We release the memory using &lt;code&gt;free()&lt;/code&gt;, which is from &lt;code&gt;stdlib.h&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete C program
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Program to illustrate the creation and traversal of a simple linked list.&lt;/span&gt;

&lt;span class="cp"&gt;#include &amp;lt;stdio.h&amp;gt; //for printf()&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdlib.h&amp;gt; //for malloc() [dynamic memory allocation] and free()&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;//struct to represent each node of the linked list.&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//each node has an int data value&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//pointer to the next node in the list.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//we shall create a simple linked list having 3 nodes.&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//pointers to the 3 nodes;&lt;/span&gt;
    &lt;span class="c1"&gt;//head is the pointer to the first node of the list.&lt;/span&gt;

    &lt;span class="c1"&gt;//dynamically allocate memory for each node.&lt;/span&gt;
    &lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;third&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//check if memory allocation failed. IF so, terminate the program.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;third&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Memory allocation failed! Program terminating.\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;//free any memory that may have allocated; free() is from stdlib.h.&lt;/span&gt;
        &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//error code to indicate that the program terminated midway.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//assign data values to the 3 nodes&lt;/span&gt;
    &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//assign some sample int values to the 3 nodes.&lt;/span&gt;
    &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//make the nodes point from one to the next node.&lt;/span&gt;
    &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//first node points to the second.&lt;/span&gt;
    &lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//second node points to the third.&lt;/span&gt;
    &lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;//there is no node after the third node.&lt;/span&gt;

    &lt;span class="c1"&gt;//make the head point to the first node.&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;//linked list of 3 nodes has been successfully created and populated with data.&lt;/span&gt;
    &lt;span class="c1"&gt;//now we shall display its contents.&lt;/span&gt;

    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//assigning a pointer for traversing the list.&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//while there are still nodes to explore in the list.&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;//for displaying arrow mark in between the values.&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//need not display arrow mark after last node.&lt;/span&gt;
            &lt;span class="nb"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" --&amp;gt; "&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curr&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//move on to the next node of the list.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//release the allocated memory.&lt;/span&gt;
    &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;third&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10 --&amp;gt; 5 --&amp;gt; 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In the event of allocation failure:&lt;/strong&gt; The program instead displays &lt;code&gt;Memory allocation failed! Program terminating.&lt;/code&gt; and returns status code &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Interactive visualizer: Create and traverse the list
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Interactive version:&lt;/strong&gt; &lt;a href="https://sriramanujaseva.com/2026/11/19/introduction-to-linked-lists-in-c-create-and-traverse/#llp-c-program" rel="noopener noreferrer"&gt;Open the complete step-by-step visualizer on Sri Ramanuja Seva&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The diagram below is a static snapshot from the interactive visualizer. The original site provides the Next, Previous, Play/Pause and Reset controls.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx4t6qgc8r6pvf1foytrd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx4t6qgc8r6pvf1foytrd.png" alt=" " width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Original visualizer instructions:&lt;/em&gt; Follow the actual operations of the C program, from allocation to traversal and memory release. Use Next, Previous, Play/Pause, or Reset.&lt;/p&gt;

&lt;p&gt;The diagrams use illustrative node addresses. Since the program allocates memory dynamically, the actual addresses can vary from one execution to another.&lt;/p&gt;

&lt;p&gt;The interactive demonstration uses &lt;strong&gt;the same three values (10, 5 and 20)&lt;/strong&gt; and follows the same allocation, linking, traversal and release stages as the C program.&lt;/p&gt;

&lt;p&gt;↑ Back to Contents&lt;/p&gt;




&lt;h2&gt;
  
  
  More on DSA
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://sriramanujaseva.com/2026/09/10/insert-element-in-an-array-learn-simple-algorithms/" rel="noopener noreferrer"&gt;Insert Element in an Array&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sriramanujaseva.com/2026/08/24/how-to-find-minimum-in-array-learn-algorithms-easily/" rel="noopener noreferrer"&gt;Find Minimum in an Array&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://sriramanujaseva.com/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2hpyupmfwtjzs0msn0ar.png" alt=" " width="800" height="66"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://sriramanujaseva.com/2026/11/19/introduction-to-linked-lists-in-c-create-and-traverse/" rel="noopener noreferrer"&gt;Sri Ramanuja Seva&lt;/a&gt;. Visit the original article for the interactive linked-list visualizer.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>computerscience</category>
      <category>datastructures</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
