-
Notifications
You must be signed in to change notification settings - Fork 0
/
sllconcatenate.c
72 lines (67 loc) · 1.43 KB
/
sllconcatenate.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
//Program to concatenate 2 S.L.L
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
//function to create a linked list
struct node * append(int ar[],int n)
{
struct node *first,*last,*p;
first=(struct node*)malloc(sizeof(struct node));
first->data=ar[0];
first->next=NULL;
last=first;
for(int i=1;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=ar[i];
p->next=NULL;
last->next=p;
last=p;
}
return first;
}
//function to concatenate a linked list
struct node * concatenate(struct node *a, struct node *b)
{
struct node *p;
p=a;
while(p->next!=NULL)
{
p=p->next;
}
p->next=b;
return a;
}
//function to display a list
void display(struct node *a)
{
struct node *p;
p=a;
while(p)
{
printf("%d -> ", p->data);
p=p->next;
}
}
int main()
{
int l1=5,l2=6;
int ai[]={1,2,3,4,5};
struct node *a=append(ai,l1);
printf("\nThe list a: ");
display(a);
int bi[]={6,7,8,9,10,11};
struct node *b=append(bi,l2);
printf("\nThe list a: ");
display (b);
//list 'a' & 'b' will be concatenated and result will be in 'res'.
struct node *res=concatenate(a,b);
//displaying the result
printf("\nThe resultant list res: ");
display(res);
return 0;
}