{"id":17118,"date":"2023-07-05T09:34:57","date_gmt":"2023-07-05T09:34:57","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=17118"},"modified":"2023-07-05T09:56:58","modified_gmt":"2023-07-05T09:56:58","slug":"c-program-for-insertion-and-deletion-in-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/","title":{"rendered":"C Program For Insertion and Deletion in Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg\" alt=\"\" \/><\/p>\n<p>Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data. Their dynamic nature and flexibility make them an essential tool for various applications. One of the key operations performed on linked lists is the insertion and deletion of elements.<\/p>\n<p>In this article, we will delve into the world of linked lists and explore an efficient C program that demonstrates how to perform insertion and deletion operations. By understanding the concepts behind these operations and studying the implementation in C, readers will gain valuable insights into the power and versatility of linked lists.<\/p>\n<h2>What is a Linked List in C?<\/h2>\n<p>A linked list in C is a linear data structure consisting of nodes, where each node contains a data element and a reference (or pointer) to the next node in the list. Unlike arrays, linked lists do not require contiguous memory allocation, allowing for dynamic memory allocation during runtime.<\/p>\n<p>In the diagram above, each node contains two components: the data field, which stores the actual value or information, and the next field, which points to the next node in the list. The last node in the list has a &quot;NULL&quot; value in its next field, indicating the end of the list.<\/p>\n<p>By following the next pointers, we can traverse the linked list and access or modify its elements. This dynamic structure allows for efficient insertion and deletion operations at any position within the list, as nodes can be easily rearranged by updating the appropriate pointers.<\/p>\n<h2>Insertion in Linked List in C<\/h2>\n<p>Before we delve into the code, let&#8217;s briefly recap the different types of insertions that can be performed in a linked list:<\/p>\n<ul>\n<li>\n<p><strong>Insertion at the Beginning:<\/strong> In this operation, a new node is added at the beginning of the list, and it becomes the new head of the list.<\/p>\n<\/li>\n<li>\n<p><strong>Insertion at the End:<\/strong> Here, a new node is appended to the end of the list, becoming the new tail node.<\/p>\n<\/li>\n<li>\n<p><strong>Insertion at a Specific Position:<\/strong> This operation involves adding a new node at a particular position within the list, shifting the existing nodes accordingly.<\/p>\n<\/li>\n<\/ul>\n<h2>Insertion at the Beginning:<\/h2>\n<pre><code>void insertAtBeginning(struct Node** head, int value) {\n    \/\/ Create a new node\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n\n    \/\/ Set the value of the new node\n    newNode->data = value;\n\n    \/\/ Point the new node to the current head\n    newNode->next = *head;\n\n    \/\/ Update the head to point to the new node\n    *head = newNode;\n}<\/code><\/pre>\n<h2>Insertion at the End:<\/h2>\n<pre><code>void insertAtEnd(struct Node** head, int value) {\n    \/\/ Create a new node\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    struct Node* temp = *head;\n\n    \/\/ Set the value of the new node\n    newNode->data = value;\n\n    \/\/ Make the new node the tail\n    newNode->next = NULL;\n\n    \/\/ If the list is empty, make the new node the head\n    if (*head == NULL) {\n        *head = newNode;\n        return;\n    }\n\n    \/\/ Traverse to the end of the list\n    while (temp->next != NULL) {\n        temp = temp->next;\n    }\n\n    \/\/ Append the new node at the end\n    temp->next = newNode;\n}<\/code><\/pre>\n<h2>Insertion at a Specific Position:<\/h2>\n<pre><code>void insertAtPosition(struct Node** head, int value, int position) {\n    \/\/ Create a new node\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\n    struct Node* temp = *head;\n\n    \/\/ Set the value of the new node\n    newNode->data = value;\n\n    \/\/ Traverse to the desired position\n    for (int i = 1; i < position - 1; i++) {\n        if (temp->next != NULL) {\n            temp = temp->next;\n        } else {\n            printf(\"Invalid position\\n\");\n            return;\n        }\n    }\n\n    \/\/ Insert the new node at the desired position\n    newNode->next = temp->next;\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_17122 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_17122 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_17122 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li.active > a, #tab_container_17122 .wpsm_nav-tabs > li.active > a:hover, #tab_container_17122 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:hover , #tab_container_17122 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_17122 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_17122 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_17122 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_17122 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_17122 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17122 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_17122 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17122 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_17122 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_17122\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_17122\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_17122_1\" aria-controls=\"tabs_desc_17122_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_17122\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_17122_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n\r\nvoid insertAtBeginning(struct Node** head, int value) {\r\n    \/\/ Create a new node\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n\r\n    \/\/ Set the value of the new node\r\n    newNode-&gt;data = value;\r\n\r\n    \/\/ Point the new node to the current head\r\n    newNode-&gt;next = *head;\r\n\r\n    \/\/ Update the head to point to the new node\r\n    *head = newNode;\r\n}\r\n\r\nvoid deleteAtBeginning(struct Node** head) {\r\n    if (*head == NULL) {\r\n        printf(\"Linked list is already empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    struct Node* temp = *head;\r\n    *head = (*head)-&gt;next;\r\n    free(temp);\r\n}\r\n\r\nvoid deleteAtEnd(struct Node** head) {\r\n    if (*head == NULL) {\r\n        printf(\"Linked list is already empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    struct Node* temp = *head;\r\n    struct Node* prev = NULL;\r\n\r\n    while (temp-&gt;next != NULL) {\r\n        prev = temp;\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    if (prev == NULL) {\r\n        *head = NULL;\r\n    } else {\r\n        prev-&gt;next = NULL;\r\n    }\r\n\r\n    free(temp);\r\n}\r\n\r\nvoid deleteAtPosition(struct Node** head, int position) {\r\n    if (*head == NULL) {\r\n        printf(\"Linked list is already empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    struct Node* temp = *head;\r\n    struct Node* prev = NULL;\r\n\r\n    if (position == 1) {\r\n        *head = temp-&gt;next;\r\n        free(temp);\r\n        return;\r\n    }\r\n\r\n    for (int i = 1; temp != NULL &amp;&amp; i &lt; position; i++) {\r\n        prev = temp;\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    if (temp == NULL) {\r\n        printf(\"Invalid position.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    prev-&gt;next = temp-&gt;next;\r\n    free(temp);\r\n}\r\n\r\nvoid displayList(struct Node* head) {\r\n    struct Node* temp = head;\r\n\r\n    \/\/ Traverse and print the list\r\n    while (temp != NULL) {\r\n        printf(\"%d \", temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(\"&#92;n\");\r\n}\r\n\r\nint main() {\r\n    struct Node* head = NULL;\r\n\r\n    \/\/ Insert elements at the beginning\r\n    insertAtBeginning(&amp;head, 3);\r\n    insertAtBeginning(&amp;head, 2);\r\n    insertAtBeginning(&amp;head, 1);\r\n\r\n    \/\/ Display the original list\r\n    printf(\"Linked List: \");\r\n    displayList(head);\r\n\r\n    \/\/ Delete node at the beginning\r\n    deleteAtBeginning(&amp;head);\r\n\r\n    \/\/ Display the list after deletion at the beginning\r\n    printf(\"Linked List after deletion at the beginning: \");\r\n    displayList(head);\r\n\r\n    \/\/ Delete node at the end\r\n    deleteAtEnd(&amp;head);\r\n\r\n    \/\/ Display the list after deletion at the end\r\n    printf(\"Linked List after deletion at the end: \");\r\n    displayList(head);\r\n\r\n    \/\/ Delete node at a specific position\r\n    deleteAtPosition(&amp;head, 1);\r\n\r\n    \/\/ Display the list after deletion at a specific position\r\n    printf(\"Linked List after deletion at a specific position: \");\r\n    displayList(head);\r\n\r\n    return 0;\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_17122 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_17122 a\"),jQuery(\"#tab-content_17122\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n\n    temp->next = newNode;\n}<\/code><\/pre>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_17121 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_17121 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_17121 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_17121 .wpsm_nav-tabs > li.active > a, #tab_container_17121 .wpsm_nav-tabs > li.active > a:hover, #tab_container_17121 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_17121 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_17121 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_17121 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_17121 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_17121 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_17121 .wpsm_nav-tabs > li > a:hover , #tab_container_17121 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_17121 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_17121 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_17121 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_17121 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_17121 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_17121 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_17121 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17121 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_17121 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17121 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_17121 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_17121\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_17121\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_17121_1\" aria-controls=\"tabs_desc_17121_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_17121\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_17121_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n\r\nvoid insertAtBeginning(struct Node** head, int value) {\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode-&gt;data = value;\r\n    newNode-&gt;next = *head;\r\n    *head = newNode;\r\n}\r\n\r\nvoid insertAtEnd(struct Node** head, int value) {\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    struct Node* temp = *head;\r\n    newNode-&gt;data = value;\r\n    newNode-&gt;next = NULL;\r\n\r\n    if (*head == NULL) {\r\n        *head = newNode;\r\n        return;\r\n    }\r\n\r\n    while (temp-&gt;next != NULL) {\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    temp-&gt;next = newNode;\r\n}\r\n\r\nvoid insertAtPosition(struct Node** head, int value, int position) {\r\n    if (position &lt;= 0) {\r\n        printf(\"Invalid position&#92;n\");\r\n        return;\r\n    }\r\n\r\n    if (position == 1 || *head == NULL) {\r\n        insertAtBeginning(head, value);\r\n        return;\r\n    }\r\n\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode-&gt;data = value;\r\n    struct Node* temp = *head;\r\n    int count = 1;\r\n\r\n    while (count &lt; position - 1 &amp;&amp; temp-&gt;next != NULL) {\r\n        temp = temp-&gt;next;\r\n        count++;\r\n    }\r\n\r\n    if (count &lt; position - 1) {\r\n        printf(\"Invalid position&#92;n\");\r\n        return;\r\n    }\r\n\r\n    newNode-&gt;next = temp-&gt;next;\r\n    temp-&gt;next = newNode;\r\n}\r\n\r\nvoid displayLinkedList(struct Node* head) {\r\n    struct Node* temp = head;\r\n\r\n    if (temp == NULL) {\r\n        printf(\"Linked list is empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    while (temp != NULL) {\r\n        printf(\"%d -&gt; \", temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    printf(\"NULL&#92;n\");\r\n}\r\n\r\nint main() {\r\n    struct Node* head = NULL;\r\n\r\n    \/\/ Insertion at the beginning\r\n    insertAtBeginning(&amp;head, 10);\r\n    insertAtBeginning(&amp;head, 20);\r\n    insertAtBeginning(&amp;head, 30);\r\n\r\n    printf(\"Linked list after insertion at the beginning: \");\r\n    displayLinkedList(head);\r\n\r\n    \/\/ Insertion at the end\r\n    insertAtEnd(&amp;head, 40);\r\n    insertAtEnd(&amp;head, 50);\r\n\r\n    printf(\"Linked list after insertion at the end: \");\r\n    displayLinkedList(head);\r\n\r\n    \/\/ Insertion at a specific position\r\n    insertAtPosition(&amp;head, 25, 2);\r\n    insertAtPosition(&amp;head, 35, 4);\r\n\r\n    printf(\"Linked list after insertion at specific positions: \");\r\n    displayLinkedList(head);\r\n\r\n    return 0;\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_17121 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_17121 a\"),jQuery(\"#tab-content_17121\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>Linked list after insertion at the beginning: 30 -> 20 -> 10 -> NULL\nLinked list after insertion at the end: 30 -> 20 -> 10 -> 40 -> 50 -> NULL\nLinked list after insertion at specific positions: 30 -> 25 -> 20 -> 35 -> 10 -> 40 -> 50 -> NULL<\/code><\/pre>\n<h2>Time Complexity Analysis:<\/h2>\n<ul>\n<li>\n<p><strong>Insertion at the beginning:<\/strong> The time complexity for inserting a node at the beginning of the linked list is O(1). It involves creating a new node, updating the pointers, and reassigning the head, all of which take constant time.<\/p>\n<\/li>\n<li>\n<p><strong>Insertion at the end:<\/strong> To insert a node at the end of the linked list, we need to traverse the entire list to reach the last node. Therefore, the time complexity for this operation is O(n), where n is the number of elements in the list.<\/p>\n<\/li>\n<\/ul>\n<p>&#8211;<strong>Insertion at a specific position:<\/strong> Similar to insertion at the end, inserting a node at a specific position requires traversing the list to reach the desired position. Thus, the time complexity is also O(n), where n is the number of elements in the list.<\/p>\n<h2>Deletion in Linked List in C<\/h2>\n<p>Before we delve into the code, let&#8217;s briefly recap the different types of deletions that can be performed in a linked list:<\/p>\n<ul>\n<li>\n<p><strong>Deletion at the Beginning:<\/strong> In this operation, the first node (head) of the list is removed, and the second node becomes the new head.<\/p>\n<\/li>\n<li>\n<p><strong>Deletion at the End:<\/strong> Here, the last node (tail) of the list is removed, and the second-to-last node becomes the new tail.<\/p>\n<\/li>\n<li>\n<p><strong>Deletion at a Specific Position:<\/strong> This operation involves removing a node at a particular position within the list, and then adjusting the pointers of the adjacent nodes accordingly.<\/p>\n<\/li>\n<\/ul>\n<h2>Deletion at the Beginning:<\/h2>\n<pre><code>void deleteAtBeginning(struct Node** head) {\n    if (*head == NULL) {\n        printf(\"Linked list is already empty.\\n\");\n        return;\n    }\n\n    struct Node* temp = *head;\n    *head = (*head)->next;\n    free(temp);\n}<\/code><\/pre>\n<h2>Deletion at the End:<\/h2>\n<pre><code>void deleteAtEnd(struct Node** head) {\n    if (*head == NULL) {\n        printf(\"Linked list is already empty.\\n\");\n        return;\n    }\n\n    struct Node* temp = *head;\n    struct Node* prev = NULL;\n\n    while (temp->next != NULL) {\n        prev = temp;\n        temp = temp->next;\n    }\n\n    if (prev == NULL) {\n        *head = NULL;\n    } else {\n        prev->next = NULL;\n    }\n\n    free(temp);\n}<\/code><\/pre>\n<h2>Deletion at a Specific Position:<\/h2>\n<pre><code>void deleteAtPosition(struct Node** head, int position) {\n    if (*head == NULL) {\n        printf(\"Linked list is already empty.\\n\");\n        return;\n    }\n\n    struct Node* temp = *head;\n    struct Node* prev = NULL;\n\n    if (position == 1) {\n        *head = temp->next;\n        free(temp);\n        return;\n    }\n\n    for (int i = 1; temp != NULL && i < position; i++) {\n        prev = temp;\n        temp = temp->next;\n    }\n\n    if (temp == NULL) {\n        printf(\"Invalid position.\\n\");\n        return;\n    }\n\n    prev->next = temp->next;\n    free(temp);\n}<\/code><\/pre>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_17122 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_17122 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_17122 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li.active > a, #tab_container_17122 .wpsm_nav-tabs > li.active > a:hover, #tab_container_17122 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li > a:hover , #tab_container_17122 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_17122 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_17122 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_17122 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_17122 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_17122 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_17122 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_17122 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17122 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_17122 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17122 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_17122 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_17122\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_17122\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_17122_1\" aria-controls=\"tabs_desc_17122_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_17122\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_17122_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n\r\nvoid insertAtBeginning(struct Node** head, int value) {\r\n    \/\/ Create a new node\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n\r\n    \/\/ Set the value of the new node\r\n    newNode-&gt;data = value;\r\n\r\n    \/\/ Point the new node to the current head\r\n    newNode-&gt;next = *head;\r\n\r\n    \/\/ Update the head to point to the new node\r\n    *head = newNode;\r\n}\r\n\r\nvoid deleteAtBeginning(struct Node** head) {\r\n    if (*head == NULL) {\r\n        printf(\"Linked list is already empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    struct Node* temp = *head;\r\n    *head = (*head)-&gt;next;\r\n    free(temp);\r\n}\r\n\r\nvoid deleteAtEnd(struct Node** head) {\r\n    if (*head == NULL) {\r\n        printf(\"Linked list is already empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    struct Node* temp = *head;\r\n    struct Node* prev = NULL;\r\n\r\n    while (temp-&gt;next != NULL) {\r\n        prev = temp;\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    if (prev == NULL) {\r\n        *head = NULL;\r\n    } else {\r\n        prev-&gt;next = NULL;\r\n    }\r\n\r\n    free(temp);\r\n}\r\n\r\nvoid deleteAtPosition(struct Node** head, int position) {\r\n    if (*head == NULL) {\r\n        printf(\"Linked list is already empty.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    struct Node* temp = *head;\r\n    struct Node* prev = NULL;\r\n\r\n    if (position == 1) {\r\n        *head = temp-&gt;next;\r\n        free(temp);\r\n        return;\r\n    }\r\n\r\n    for (int i = 1; temp != NULL &amp;&amp; i &lt; position; i++) {\r\n        prev = temp;\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    if (temp == NULL) {\r\n        printf(\"Invalid position.&#92;n\");\r\n        return;\r\n    }\r\n\r\n    prev-&gt;next = temp-&gt;next;\r\n    free(temp);\r\n}\r\n\r\nvoid displayList(struct Node* head) {\r\n    struct Node* temp = head;\r\n\r\n    \/\/ Traverse and print the list\r\n    while (temp != NULL) {\r\n        printf(\"%d \", temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(\"&#92;n\");\r\n}\r\n\r\nint main() {\r\n    struct Node* head = NULL;\r\n\r\n    \/\/ Insert elements at the beginning\r\n    insertAtBeginning(&amp;head, 3);\r\n    insertAtBeginning(&amp;head, 2);\r\n    insertAtBeginning(&amp;head, 1);\r\n\r\n    \/\/ Display the original list\r\n    printf(\"Linked List: \");\r\n    displayList(head);\r\n\r\n    \/\/ Delete node at the beginning\r\n    deleteAtBeginning(&amp;head);\r\n\r\n    \/\/ Display the list after deletion at the beginning\r\n    printf(\"Linked List after deletion at the beginning: \");\r\n    displayList(head);\r\n\r\n    \/\/ Delete node at the end\r\n    deleteAtEnd(&amp;head);\r\n\r\n    \/\/ Display the list after deletion at the end\r\n    printf(\"Linked List after deletion at the end: \");\r\n    displayList(head);\r\n\r\n    \/\/ Delete node at a specific position\r\n    deleteAtPosition(&amp;head, 1);\r\n\r\n    \/\/ Display the list after deletion at a specific position\r\n    printf(\"Linked List after deletion at a specific position: \");\r\n    displayList(head);\r\n\r\n    return 0;\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_17122 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_17122 a\"),jQuery(\"#tab-content_17122\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output<\/strong><\/p>\n<pre><code>Linked List: 1 2 3 \nLinked List after deletion at the beginning: 2 3 \nLinked List after deletion at the end: 2 \nLinked List after deletion at a specific position: <\/code><\/pre>\n<p><strong>Time Complexity Analysis:<\/strong><\/p>\n<p>The time complexity of deletion in a linked list depends on the type of deletion operation being performed.<\/p>\n<h2>Deletion at the Beginning:<\/h2>\n<p>The deletion at the beginning operation involves updating the head pointer to point to the second node in the list and freeing the memory of the deleted node.<br \/>\nSince this operation only requires a constant number of steps, the time complexity is O(1).<\/p>\n<h2>Deletion at the End:<\/h2>\n<p>Deletion at the end requires traversing the entire linked list until reaching the second-to-last node.<br \/>\nTherefore, the time complexity for deletion at the end is O(n), where n is the number of nodes in the linked list.<\/p>\n<h2>Deletion at a Specific Position:<\/h2>\n<p>To delete a node at a specific position, we need to traverse the list until reaching the desired position, adjusting the pointers accordingly.<br \/>\nIn the worst case scenario, where the position is at the end of the list, the time complexity is O(n).<br \/>\nHowever, for positions closer to the beginning, the time complexity can be closer to O(1).<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn this article, we explored the concepts of insertion and deletion in linked lists using the C programming language. We covered the code implementation for inserting nodes at the beginning, end, and specific positions in a linked list. We also discussed the code implementation for deleting nodes from the beginning, end, and specific positions in a linked list. Understanding these operations is crucial for effectively working with linked lists and managing data dynamically.<\/p>\n<p>By studying the provided code examples and the time complexity analysis, readers should have gained a solid understanding of how to perform insertion and deletion operations in a linked list. They can now apply this knowledge to build more complex programs that require efficient data manipulation.<\/p>\n<h2>FAQs (Frequently Asked Questions):<\/h2>\n<p><strong>Q1: What is a linked list?<\/strong><br \/>\nA linked list is a data structure consisting of a sequence of nodes, where each node contains a data element and a reference (or pointer) to the next node in the sequence. It allows dynamic memory allocation and provides flexibility in storing and accessing data.<\/p>\n<p><strong>Q2: When should I use a linked list?<\/strong><br \/>\nLinked lists are useful in scenarios where you need to perform frequent insertions and deletions of elements, as they provide efficient time complexity for these operations. They are also valuable when the size of the data is unknown or when there is a need for efficient memory allocation.<\/p>\n<p><strong>Q3: What is the difference between an array and a linked list?<\/strong><br \/>\nArrays are fixed-size structures that store elements in contiguous memory locations, whereas linked lists are dynamic structures that store elements in separate nodes with pointers linking them. Arrays offer fast random access but are less efficient for insertions and deletions, while linked lists provide efficient insertions and deletions but slower random access.<\/p>\n<p><strong>Q4: What are the time complexities for insertion and deletion in a linked list?<\/strong><br \/>\nInsertion at the beginning: O(1)<br \/>\nInsertion at the end: O(n)<br \/>\nInsertion at a specific position: O(n)<br \/>\nDeletion at the beginning: O(1)<br \/>\nDeletion at the end: O(n)<br \/>\nDeletion at a specific position: O(n)<\/p>\n<p><strong>Q5: Can I insert and delete elements in a doubly linked list using similar techniques?<\/strong><br \/>\nYes, the techniques used for insertion and deletion in a singly linked list can be applied to a doubly linked list as well. The main difference is that in a doubly linked list, each node has references to both the next and previous nodes, allowing more efficient deletion operations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data. Their dynamic nature and flexibility make them an essential tool for various applications. One of the key operations performed on linked lists is the insertion and deletion of elements. In this article, we will delve into the [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[150],"tags":[],"class_list":["post-17118","post","type-post","status-publish","format-standard","hentry","category-linked-list-interview-problems"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C Program For Insertion and Deletion in Linked List<\/title>\n<meta name=\"description\" content=\"Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Program For Insertion and Deletion in Linked List\" \/>\n<meta property=\"og:description\" content=\"Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-05T09:34:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-05T09:56:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"C Program For Insertion and Deletion in Linked List\",\"datePublished\":\"2023-07-05T09:34:57+00:00\",\"dateModified\":\"2023-07-05T09:56:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\"},\"wordCount\":1222,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg\",\"articleSection\":[\"Linked List Interview Problems\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\",\"name\":\"C Program For Insertion and Deletion in Linked List\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg\",\"datePublished\":\"2023-07-05T09:34:57+00:00\",\"dateModified\":\"2023-07-05T09:56:58+00:00\",\"description\":\"Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interview Questions\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/interview-questions\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Linked List Interview Problems\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/interview-questions\/linked-list-interview-problems\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"C Program For Insertion and Deletion in Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Program For Insertion and Deletion in Linked List","description":"Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"C Program For Insertion and Deletion in Linked List","og_description":"Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data.","og_url":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-07-05T09:34:57+00:00","article_modified_time":"2023-07-05T09:56:58+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"C Program For Insertion and Deletion in Linked List","datePublished":"2023-07-05T09:34:57+00:00","dateModified":"2023-07-05T09:56:58+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/"},"wordCount":1222,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg","articleSection":["Linked List Interview Problems"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/","name":"C Program For Insertion and Deletion in Linked List","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg","datePublished":"2023-07-05T09:34:57+00:00","dateModified":"2023-07-05T09:56:58+00:00","description":"Linked lists are fundamental data structures used in computer science and programming to efficiently store and manipulate data.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1688549456531-C%20Program%20For%20Insertion%20and%20Deletion%20in%20Linked%20List.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/c-program-for-insertion-and-deletion-in-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Interview Questions","item":"https:\/\/prepbytes.com\/blog\/category\/interview-questions\/"},{"@type":"ListItem","position":3,"name":"Linked List Interview Problems","item":"https:\/\/prepbytes.com\/blog\/category\/interview-questions\/linked-list-interview-problems\/"},{"@type":"ListItem","position":4,"name":"C Program For Insertion and Deletion in Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/17118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=17118"}],"version-history":[{"count":2,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/17118\/revisions"}],"predecessor-version":[{"id":17126,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/17118\/revisions\/17126"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=17118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=17118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=17118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}