-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_singly_ll.c
82 lines (73 loc) · 1.48 KB
/
merge_singly_ll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Write a C Program to Merge two Singly Linked List and Display the elements of Merged List.
// Input:
// Number of Elements in List1 - m
// m Elements in List1
// Number of Elements in List2 - n
// n Elements in List2
// Output:
// m+n Elements from List1 and List2
#include <stdio.h>
#include <stdlib.h>
struct node
{
int element;
struct node *next;
};
typedef struct node node;
void insert(node *list, int x)
{
node *newnode = malloc(sizeof(node));
newnode->element = x;
newnode->next = NULL;
node *position = list;
while (position->next != NULL)
{
position = position->next;
}
position->next = newnode;
}
void merge(node *list1, node *list2)
{
node *position = list1;
while (position->next != NULL)
{
position = position->next;
}
position->next = list2->next;
free(list2);
}
void traverse(node *list)
{
node *position = list;
while (position->next != NULL)
{
position = position->next;
printf("%d\n", position->element);
}
printf("\n");
}
int main()
{
int x, y;
scanf("%d", &x);
node *list1 = malloc(sizeof(node));
list1->next = NULL;
node *list2 = malloc(sizeof(node));
list2->next = NULL;
while (x--)
{
int t;
scanf("%d", &t);
insert(list1, t);
}
scanf("%d", &y);
while (y--)
{
int t;
scanf("%d", &t);
insert(list2, t);
}
merge(list1, list2);
traverse(list1);
return 0;
}