-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglyLinkedList.c
96 lines (84 loc) · 1.83 KB
/
singlyLinkedList.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *newnd()
{
struct node *new;
new = (struct node *)malloc(sizeof(*new));
return new;
}
void concat(struct node *p, struct node *q)
{
struct node *nd = p;
while (nd->next != NULL)
nd = nd->next;
nd->next = q;
}
int length(struct node *p)
{
int count = 0;
struct node *nd = p;
while (nd != NULL) {
count++;
nd = nd->next;
}
return count;
}
int main(void)
{
struct node *nd1, *nd2, *head1, *head2;
int i = 1, n;
nd1 = (struct node *)malloc(sizeof(*nd1));
nd2 = (struct node *)malloc(sizeof(*nd2));
head1 = (struct node *)malloc(sizeof(*head1));
head2 = (struct node *)malloc(sizeof(*head2));
printf("n : ");
scanf("%d", &n);
head1 = nd1; head2 = nd2;
while (1) {
nd1->data = i;
nd2->data = i + 10;
if (i == n) {
nd1->next = NULL;
nd2->next = NULL;
break;
}
nd1->next = newnd();
nd2->next = newnd();
nd1 = nd1->next;
nd2 = nd2->next;
i++;
}
nd1 = head1; nd2 = head2;
printf("list1 : [ ");
while (nd1 != NULL) {
printf("%d ", nd1->data);
nd1 = nd1->next;
}
printf("]\n"
"list2 : [ ");
while (nd2 != NULL) {
printf("%d ", nd2->data);
nd2 = nd2->next;
}
printf("]\n"
"Length\n"
"list1 : %d\n"
"list2 : %d\n", length(head1), length(head2));
concat(head1, head2);
nd1 = head1;
printf("----[Concat]----\n"
"list1 : [ ");
while (nd1 != NULL) {
printf("%d ", nd1->data);
nd1 = nd1->next;
}
printf("]\n"
"Length\n"
"list1 : %d\n", length(head1));
return 0;
}