-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_list.c
91 lines (82 loc) · 1.81 KB
/
merge_list.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
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
struct node{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE f1=NULL,f2=NULL;
int n1,n2;
NODE getnode(){
NODE ntemp;
ntemp = (NODE)malloc(sizeof(struct node));
printf("enter the data:");
scanf("%d",&ntemp->info);
ntemp->link=NULL;
return ntemp;
}
void insert_rear(NODE *first){
NODE cur;
struct node *temp = getnode();
if(*first==NULL){
*first=temp;
temp->link=NULL;
return;
}
else{
cur=*first;
while(cur->link != NULL){
cur=cur->link;
}
cur->link=temp;
temp->link=NULL;
return;
}
}
void display(NODE first){
NODE cur = first;
while(cur != NULL){
printf("%d\t",cur->info);
cur=cur->link;
}
}
void main(){
printf("Enter the number of nodes in list1:");
scanf("%d",&n1);
printf("Enter the number of nodes in list2:");
scanf("%d",&n2);
printf("list1:\n");
for(int i=0;i<n1;i++){
insert_rear(&f1);
}
printf("list2:\n");
for(int i=0;i<n2;i++){
insert_rear(&f2);
}
NODE cur=f1;
while(cur->link != NULL){
cur=cur->link;
}
cur->link=f2;
n1=n1+n2;
for(int i=0;i<n1-1;i++){
NODE cur1=f1;
NODE cur2=f1->link;
for(int j=0;j<n1-i-1;j++){
if(cur2->info < cur1->info){
int temp=cur1->info;
cur1->info=cur2->info;
cur2->info=temp;
}
else{
cur1=cur1->link;
cur2=cur2->link;
}
}
}
printf("sorted and meged list:\n");
display(&f1);
printf("\n");
}