Program to find intersect node in merged linked list
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
int visited:1;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->visited = 0;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void MergeTwoList (struct node **head1, struct node **head2)
{
struct node *temp = *head1;
struct node *merge = *head2;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = merge->next->next->next;
return;
}
void FindIntersectNode (struct node **head1, struct node **head2, struct node **join)
{
struct node *temp = *head1;
while (temp->next != NULL) {
temp->visited = 1;
temp = temp->next;
}
temp = *head2;
while (temp->next != NULL) {
if (temp->visited) {
*join = temp;
return;
}
temp = temp->next;
}
return;
}
int main(void)
{
int i;
struct node *head1 = NULL, *head2 = NULL;
struct node *join = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head1, i);
}
for (i = 11; i <= 20; i++) {
AppendToList(&head2, i);
}
MergeTwoList(&head1, &head2);
printf("Merged Head1 List \r\n");
PrintList(&head1);
printf("Merged Head2 List \r\n");
PrintList(&head2);
FindIntersectNode(&head1, &head2, &join);
printf("Joined Node %d \r\n", join->data);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
int visited:1;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->visited = 0;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void MergeTwoList (struct node **head1, struct node **head2)
{
struct node *temp = *head1;
struct node *merge = *head2;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = merge->next->next->next;
return;
}
void FindIntersectNode (struct node **head1, struct node **head2, struct node **join)
{
struct node *temp = *head1;
while (temp->next != NULL) {
temp->visited = 1;
temp = temp->next;
}
temp = *head2;
while (temp->next != NULL) {
if (temp->visited) {
*join = temp;
return;
}
temp = temp->next;
}
return;
}
int main(void)
{
int i;
struct node *head1 = NULL, *head2 = NULL;
struct node *join = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head1, i);
}
for (i = 11; i <= 20; i++) {
AppendToList(&head2, i);
}
MergeTwoList(&head1, &head2);
printf("Merged Head1 List \r\n");
PrintList(&head1);
printf("Merged Head2 List \r\n");
PrintList(&head2);
FindIntersectNode(&head1, &head2, &join);
printf("Joined Node %d \r\n", join->data);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all: mergenode
mergenode: mergenode.o
${CC} ${CFLAGS} -o $@ mergenode.o
clean:
-@rm *.o
distclean: clean
-@rm mergenode
Output:
Merged Head1 List
10
9
8
7
6
5
4
3
2
1
17
16
15
14
13
12
11
Merged Head2 List
20
19
18
17
16
15
14
13
12
11
Joined Node 17
No comments:
Post a Comment