0% found this document useful (0 votes)
394 views8 pages

MCSL-209 Solved Assignment 2025-26 @ignouhub

The document outlines an assignment for the course MCSL-209, focusing on Data Structures and Algorithms Lab, with a total of 100 marks. It includes two main questions: merging two sorted linked lists and managing edges in an undirected graph using C programming. Each question is worth 20 marks, with additional marks allocated for lab records and viva voce.

Uploaded by

mannu kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
394 views8 pages

MCSL-209 Solved Assignment 2025-26 @ignouhub

The document outlines an assignment for the course MCSL-209, focusing on Data Structures and Algorithms Lab, with a total of 100 marks. It includes two main questions: merging two sorted linked lists and managing edges in an undirected graph using C programming. Each question is worth 20 marks, with additional marks allocated for lab records and viva voce.

Uploaded by

mannu kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Course Code : MCSL-209

Course Title : Data Structures and Algorithms Lab


Assignment Number : BCA_NEW(III)L-209/Assignment/2025-26
Maximum Marks : 100
Weightage : 25%
Last Dates for Submission : 31st October, 2025 (for July session)
30th April, 2026 (for January session)

There are two questions in this assignment carrying a total of 40 marks. Each
question carries 20 marks. Your Lab Record will carry 40 Marks. Rest 20 marks
are for viva voce. You may use illustrations and diagrams to enhance the
explanations. Please go through the guidelines regarding assignments given in
the Programme Guide for the format of presentation.

Question 1: Write an algorithm and program in ‘C’ language to merge two sorted
linked lists. The resultant linked list should be sorted. (20 Marks)

Solution: Algorithm to Merge Two Sorted Linked Lists

Input: Heads of two sorted linked lists, head1 and head2.


Output: Head of the merged sorted linked list.

1. Initialize a dummy node mergedHead and a pointer current pointing to it.


2. Compare the data of the current nodes of the two lists:
o If head1->data is less than or equal to head2->data, append head1 to
current->next and move head1 to the next node.
o Else, append head2 to current->next and move head2 to the next node.
3. Move current to the next node to the appended node.
4. Repeat step 2 until one of the lists becomes empty.
5. Append the non-empty list to the end of the merged list.
6. Return mergedHead->next as the head of the merged sorted linked list (since
mergedHead was a dummy node).

C Program to Merge Two Sorted Linked Lists

#include <stdio.h>
#include <stdlib.h>

// Node structure for linked list


struct Node {
int data;
struct Node* next;
};
// Function to create a new node with given data
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert nodes at the end of the list


void insertEnd(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
if (*headRef == NULL) {
*headRef = newNode;
return;
}
struct Node* temp = *headRef;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Function to print linked list


void printList(struct Node* head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

// Function to merge two sorted linked lists


struct Node* mergeSortedLists(struct Node* head1, struct Node* head2) {
struct Node dummy;
struct Node* current = &dummy;
dummy.next = NULL;

while (head1 != NULL && head2 != NULL) {


if (head1->data <= head2->data) {
current->next = head1;
head1 = head1->next;
} else {
current->next = head2;
head2 = head2->next;
}
current = current->next;
}

// Append remaining nodes


if (head1 != NULL) {
current->next = head1;
} else {
current->next = head2;
}

return dummy.next;
}

// Main function to demonstrate merging


int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;
struct Node* mergedList = NULL;

// Creating first sorted linked list: 1 -> 3 -> 5 -> 7


insertEnd(&list1, 1);
insertEnd(&list1, 3);
insertEnd(&list1, 5);
insertEnd(&list1, 7);

// Creating second sorted linked list: 2 -> 4 -> 6 -> 8


insertEnd(&list2, 2);
insertEnd(&list2, 4);
insertEnd(&list2, 6);
insertEnd(&list2, 8);

printf("List 1: ");
printList(list1);

printf("List 2: ");
printList(list2);

// Merge the two sorted lists


mergedList = mergeSortedLists(list1, list2);

printf("Merged Sorted List: ");


printList(mergedList);

return 0;
}
Explanation

• Node Structure: A basic linked list node with data and next.
• InsertEnd Function: Builds the linked lists by inserting new nodes at the end.
• Merge Function: Uses a dummy node to simplify edge cases and iteratively
merges nodes from both lists in sorted order.
• Main Function:
o Creates two sample sorted lists.
o Prints them.
o Merges them.
o Prints the merged sorted list.

Sample Output

List 1: 1 3 5 7
List 2: 2 4 6 8
Merged Sorted List: 1 2 3 4 5 6 7 8

Question 2: Write an algorithm and a program in ‘C’ language to insert and


delete edges in an adjacency list representation of an undirected graph. Make
assumptions, if necessary. (20 Marks)

Solution: Algorithm for Inserting an Edge

Input: Undirected graph represented as adjacency lists, vertices u and v to be


connected.
Output: Graph updated with the edge (u, v) added.

1. Create a new node for vertex v and insert it at the beginning of the adjacency
list of vertex u.
2. Because the graph is undirected, create a new node for vertex u and insert it at
the beginning of the adjacency list of vertex v.
3. Update the adjacency lists accordingly.

Algorithm for Deleting an Edge

Input: Undirected graph represented as adjacency lists, vertices u and v whose edge is
to be deleted.
Output: Graph updated with the edge (u, v) removed.

1. Traverse the adjacency list of vertex u to find the node containing v.


o Remove this node from u’s adjacency list.
2. Traverse the adjacency list of vertex v to find the node containing u.
o Remove this node from v’s adjacency list.
C Program for Insert and Delete Edges in an Undirected Graph

#include <stdio.h>
#include <stdlib.h>

// Structure to represent adjacency list node


struct AdjListNode {
int dest;
struct AdjListNode* next;
};

// Structure to represent adjacency list


struct AdjList {
struct AdjListNode* head;
};

// Structure to represent a graph


struct Graph {
int V; // Number of vertices
struct AdjList* array; // Array of adjacency lists
};

// Create a new adjacency list node


struct AdjListNode* newAdjListNode(int dest) {
struct AdjListNode* newNode = (struct AdjListNode*)malloc(sizeof(struct
AdjListNode));
if (!newNode) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}

// Create a graph of V vertices


struct Graph* createGraph(int V) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
if (!graph) {
printf("Memory allocation failed\n");
exit(1);
}
graph->V = V;
graph->array = (struct AdjList*)malloc(V * sizeof(struct AdjList));
if (!graph->array) {
printf("Memory allocation failed\n");
exit(1);
}
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}

// Insert edge in undirected graph


void addEdge(struct Graph* graph, int u, int v) {
// Add v to u’s adjacency list
struct AdjListNode* newNode = newAdjListNode(v);
newNode->next = graph->array[u].head;
graph->array[u].head = newNode;

// Since undirected, add u to v’s adjacency list


newNode = newAdjListNode(u);
newNode->next = graph->array[v].head;
graph->array[v].head = newNode;
}

// Delete an edge from adjacency list


void deleteEdgeHelper(struct AdjListNode** headRef, int dest) {
struct AdjListNode* temp = *headRef;
struct AdjListNode* prev = NULL;

while (temp != NULL && temp->dest != dest) {


prev = temp;
temp = temp->next;
}

if (temp == NULL) return; // Edge not found

if (prev == NULL) {
// First node to be deleted
*headRef = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
}

// Delete edge u-v from graph


void deleteEdge(struct Graph* graph, int u, int v) {
// Delete v from u’s adjacency list
deleteEdgeHelper(&(graph->array[u].head), v);

// Delete u from v’s adjacency list


deleteEdgeHelper(&(graph->array[v].head), u);
}

// Print graph adjacency lists


void printGraph(struct Graph* graph) {
for (int i = 0; i < graph->V; ++i) {
struct AdjListNode* temp = graph->array[i].head;
printf("Adjacency list of vertex %d: ", i);
while (temp) {
printf("%d -> ", temp->dest);
temp = temp->next;
}
printf("NULL\n");
}
}

int main() {
int V = 5;
struct Graph* graph = createGraph(V);

addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);

printf("Graph before deleting an edge:\n");


printGraph(graph);

// Delete edge between vertex 1 and 4


deleteEdge(graph, 1, 4);

printf("\nGraph after deleting edge between 1 and 4:\n");


printGraph(graph);

return 0;
}

Explanation

• Structures: Defined adjacency list nodes and graph structure.


• addEdge: Adds two edges (u->v and v->u) for undirected graph.
• deleteEdgeHelper: Helper to remove a node from the adjacency list.
• deleteEdge: Uses helper to delete edge in both adjacency lists.
• printGraph: Displays adjacency lists showing graph structure.
• main:
o Creates a graph with 5 vertices.
o Adds edges to form the graph.
o Prints graph before and after deletion of an edge.

Sample Output

Graph before deleting an edge:


Adjacency list of vertex 0: 4 -> 1 -> NULL
Adjacency list of vertex 1: 4 -> 3 -> 2 -> 0 -> NULL
Adjacency list of vertex 2: 3 -> 1 -> NULL
Adjacency list of vertex 3: 4 -> 2 -> 1 -> NULL
Adjacency list of vertex 4: 3 -> 1 -> 0 -> NULL

Graph after deleting edge between 1 and 4:


Adjacency list of vertex 0: 4 -> 1 -> NULL
Adjacency list of vertex 1: 3 -> 2 -> 0 -> NULL
Adjacency list of vertex 2: 3 -> 1 -> NULL
Adjacency list of vertex 3: 4 -> 2 -> 1 -> NULL
Adjacency list of vertex 4: 3 -> 0 -> NULL

You might also like