-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14concatLastN.c
More file actions
executable file
·94 lines (94 loc) · 2.11 KB
/
14concatLastN.c
File metadata and controls
executable file
·94 lines (94 loc) · 2.11 KB
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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int countNodes(struct node*);
void accept(struct node**,struct node**);
int concatList(struct node**,struct node**,int);
int insertLast(struct node**,int);
void display(struct node*);
int main(){
int no;
struct node *head1=NULL,*head2=NULL;
accept(&head1,&head2);
printf("Input source linked list: ");
display(head1);
printf("\nInput destination linked list: ");
display(head2);
printf("Number of Elements to be linked.\n");
scanf(" %d",&no);
concatList(&head2,&head1,no);
printf("\nOutput destination linked list: ");
display(head2);
return 0;
}
int countNodes(struct node* head){
struct node* temp=head;
int cnt=1;
while(temp->next!=NULL){
temp=temp->next;
cnt++;
}
return cnt;
}
void accept(struct node **head1,struct node **head2){
int itr1,num,data;
printf("Enter number of nodes to be created for List -1.\n");
scanf("%d",&num);
for(itr1=0;itr1<num;itr1++){
printf("Enter data of node %d\n",itr1+1);
scanf(" %d",&data);
insertLast(head1,data);
}
printf("Enter number of nodes to be created for List -2.\n");
scanf(" %d",&num);
for(itr1=0;itr1<num;itr1++){
printf("Enter data of node %d\n",itr1+1);
scanf(" %d",&data);
insertLast(head2,data);
}
}
int insertLast(struct node **head,int data){
struct node *temp=*head;
struct node *newNode=(struct node*)malloc(sizeof(struct node));
newNode->data=data;
newNode->next=NULL;
if(*head==NULL){
*head=newNode;
}else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode;
}
return 0;
}
int concatList(struct node **src,struct node **dest,int no){
struct node *temp=*dest;
int cnt=countNodes(*dest);
int num=cnt-no;
if(no>cnt){
printf("Max %d elements can be linked.\n",cnt);
exit(1);
}
while(temp->next!=NULL && num!=0){
temp=temp->next;
num--;
}
while(temp->next!=NULL){
insertLast(src,temp->data);
temp=temp->next;
}
insertLast(src,temp->data);
return 0;
}
void display(struct node *head){
struct node *temp=head;
while(temp->next!=NULL){
printf("|%d|->",temp->data);
temp=temp->next;
}
printf("|%d|\n",temp->data);
}