#include
<stdio.h>
#include <stdlib.h>
struct
Node
{
int data;
struct Node* next;
};
typedef
struct Node Node;
void
append(Node** head, int data)
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
if (*head == NULL)
{
*head = new_node;
}
else
{
Node* current = *head;
while (current->next)
{
current = current->next;
}
current->next = new_node;
}
}
Node*
union_linked_lists(Node* list1, Node* list2)
{
Node* union_list = NULL;
Node* current1 = list1;
Node* current2 = list2;
Node* tail = NULL;
while (current1 || current2)
{
int value;
if (current1 && (!current2 || current1->data < current2->data))
{
value = current1->data;
current1 = current1->next;
}
else if (current2 && (!current1 || current2->data < current1->data))
{
value = current2->data;
current2 = current2->next;
}
else
{
value = current1->data;
current1 = current1->next;
current2 = current2->next;
}
append(&union_list, value);
}
return union_list;
}
void
display(Node* head)
{
Node* current = head;
while (current)
{
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int
main()
{
Node* list1 = NULL;
Node* list2 = NULL;
int num_elements1, num_elements2;
printf("Enter the number of elements for the first list: ");
scanf("%d", &num_elements1);
printf("Enter elements for the first list (sorted): ");
for (int i = 0; i < num_elements1; i++)
{
int element;
scanf("%d", &element);
append(&list1, element);
}
printf("Enter the number of elements for the second list: ");
scanf("%d", &num_elements2);
printf("Enter elements for the second list (sorted): ");
for (int i = 0; i < num_elements2; i++)
{
int element;
scanf("%d", &element);
append(&list2, element);
}
Node* union_list = union_linked_lists(list1, list2);
printf("Union of the linked lists:\n");
display(union_list);
return 0;
}
#include <stdlib.h>
{
int data;
struct Node* next;
};
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
if (*head == NULL)
{
*head = new_node;
}
else
{
Node* current = *head;
while (current->next)
{
current = current->next;
}
current->next = new_node;
}
}
{
Node* union_list = NULL;
Node* current1 = list1;
Node* current2 = list2;
Node* tail = NULL;
while (current1 || current2)
{
int value;
if (current1 && (!current2 || current1->data < current2->data))
{
value = current1->data;
current1 = current1->next;
}
else if (current2 && (!current1 || current2->data < current1->data))
{
value = current2->data;
current2 = current2->next;
}
else
{
value = current1->data;
current1 = current1->next;
current2 = current2->next;
}
append(&union_list, value);
}
return union_list;
}
{
Node* current = head;
while (current)
{
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
{
Node* list1 = NULL;
Node* list2 = NULL;
int num_elements1, num_elements2;
printf("Enter the number of elements for the first list: ");
scanf("%d", &num_elements1);
printf("Enter elements for the first list (sorted): ");
for (int i = 0; i < num_elements1; i++)
{
int element;
scanf("%d", &element);
append(&list1, element);
}
printf("Enter the number of elements for the second list: ");
scanf("%d", &num_elements2);
printf("Enter elements for the second list (sorted): ");
for (int i = 0; i < num_elements2; i++)
{
int element;
scanf("%d", &element);
append(&list2, element);
}
Node* union_list = union_linked_lists(list1, list2);
printf("Union of the linked lists:\n");
display(union_list);
return 0;
}
Output:
Enter the number of elements for the first list: 3
Enter elements for the first list (sorted): 1 2 3
Enter the number of elements for the second list: 3
Enter elements for the second list (sorted): 4 5 6
Union of the linked lists:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
No comments:
Post a Comment