{"id":2290,"date":"2025-12-13T15:07:14","date_gmt":"2025-12-13T15:07:14","guid":{"rendered":"https:\/\/www.ignoubcamca.com\/forum\/?p=2290"},"modified":"2025-12-18T17:40:24","modified_gmt":"2025-12-18T17:40:24","slug":"question-1-mcs-209-data-structures-and-algorithms-lab","status":"publish","type":"post","link":"https:\/\/www.ignoubcamca.com\/forum\/question-1-mcs-209-data-structures-and-algorithms-lab\/","title":{"rendered":"Question 1: MCS-209 Data Structures and Algorithms Lab"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Write an algorithm and program in \u2018C\u2019 language to merge two sorted linked lists. The resultant linked list should be sorted.<\/h2>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Algorithm to Merge Two Sorted Linked Lists<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start.<\/li>\n\n\n\n<li>Create a node structure with <code>data<\/code> and <code>next<\/code>.<\/li>\n\n\n\n<li>Create two sorted linked lists (<code>list1<\/code> and <code>list2<\/code>).<\/li>\n\n\n\n<li>Create a dummy node to store the merged list.<\/li>\n\n\n\n<li>Set a pointer <code>tail<\/code> to the dummy node.<\/li>\n\n\n\n<li>Compare the data of the current nodes of both lists:\n<ul class=\"wp-block-list\">\n<li>If data of <code>list1<\/code> \u2264 data of <code>list2<\/code>, attach <code>list1<\/code> node to <code>tail<\/code> and move <code>list1<\/code> forward.<\/li>\n\n\n\n<li>Else, attach <code>list2<\/code> node to <code>tail<\/code> and move <code>list2<\/code> forward.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Move <code>tail<\/code> to the newly added node.<\/li>\n\n\n\n<li>Repeat steps 6\u20137 until one list becomes empty.<\/li>\n\n\n\n<li>Attach the remaining nodes of the non-empty list to <code>tail<\/code>.<\/li>\n\n\n\n<li>The merged sorted list starts from <code>dummy-&gt;next<\/code>.<\/li>\n\n\n\n<li>Stop.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>C Program to Merge Two Sorted Linked Lists<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">C Program to Merge Two Sorted Linked Lists<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>\/\/ Definition of linked list node<br>struct Node {<br>    int data;<br>    struct Node* next;<br>};<br><br>\/\/ Function to create a new node<br>struct Node* createNode(int data) {<br>    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));<br>    newNode-&gt;data = data;<br>    newNode-&gt;next = NULL;<br>    return newNode;<br>}<br><br>\/\/ Function to merge two sorted linked lists<br>struct Node* mergeSortedLists(struct Node* list1, struct Node* list2) {<br>    struct Node dummy;<br>    struct Node* tail = &amp;dummy;<br>    dummy.next = NULL;<br><br>    while (list1 != NULL &amp;&amp; list2 != NULL) {<br>        if (list1-&gt;data &lt;= list2-&gt;data) {<br>            tail-&gt;next = list1;<br>            list1 = list1-&gt;next;<br>        } else {<br>            tail-&gt;next = list2;<br>            list2 = list2-&gt;next;<br>        }<br>        tail = tail-&gt;next;<br>    }<br><br>    \/\/ Attach remaining nodes<br>    if (list1 != NULL)<br>        tail-&gt;next = list1;<br>    else<br>        tail-&gt;next = list2;<br><br>    return dummy.next;<br>}<br><br>\/\/ Function to display the linked list<br>void display(struct Node* head) {<br>    while (head != NULL) {<br>        printf(\"%d -&gt; \", head-&gt;data);<br>        head = head-&gt;next;<br>    }<br>    printf(\"NULL\\n\");<br>}<br><br>\/\/ Main function<br>int main() {<br>    \/\/ Creating first sorted list: 1 -&gt; 3 -&gt; 5<br>    struct Node* list1 = createNode(1);<br>    list1-&gt;next = createNode(3);<br>    list1-&gt;next-&gt;next = createNode(5);<br><br>    \/\/ Creating second sorted list: 2 -&gt; 4 -&gt; 6<br>    struct Node* list2 = createNode(2);<br>    list2-&gt;next = createNode(4);<br>    list2-&gt;next-&gt;next = createNode(6);<br><br>    printf(\"List 1: \");<br>    display(list1);<br><br>    printf(\"List 2: \");<br>    display(list2);<br><br>    \/\/ Merging the lists<br>    struct Node* mergedList = mergeSortedLists(list1, list2);<br><br>    printf(\"Merged Sorted List: \");<br>    display(mergedList);<br><br>    return 0;<br>}<\/pre>\n\n\n\n<p>Output Example<\/p>\n\n\n\n<p><code>List 1: 1 -&gt; 3 -&gt; 5 -&gt; NULL<br>List 2: 2 -&gt; 4 -&gt; 6 -&gt; NULL<br>Merged Sorted List: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; NULL<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write an algorithm and program in \u2018C\u2019 language to merge two sorted linked lists. The<br \/>\nresultant linked list should be sorted.<\/p>\n","protected":false},"author":1,"featured_media":2352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,30],"tags":[],"class_list":["post-2290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bca-assignment","category-january-2026"],"_links":{"self":[{"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/posts\/2290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/comments?post=2290"}],"version-history":[{"count":2,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/posts\/2290\/revisions"}],"predecessor-version":[{"id":2310,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/posts\/2290\/revisions\/2310"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/media\/2352"}],"wp:attachment":[{"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/media?parent=2290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/categories?post=2290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ignoubcamca.com\/forum\/wp-json\/wp\/v2\/tags?post=2290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}