-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergingsortedll.c
97 lines (75 loc) · 1.66 KB
/
mergingsortedll.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
97
//ctrl shift h opens git-plus menu
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
typedef struct node NODE;
NODE* head1=NULL;
NODE* head2=NULL;
NODE* current=NULL;
NODE* temp=NULL;
int CreateList(NODE** head); //**pointer of pointer of type NODE
int MergeList();
int PrintList();
int main()
{
int choice;
printf("Do you want to create both linked list? Enter 1 for yes ");
scanf("%d",&choice);
if(choice==1)
{
CreateList(&head1);
CreateList(&head2);
}
choice=0;
printf("Enter 1 to merge and display the linked list ");
scanf("%d",&choice);
if(choice==1)
{
MergeList();
PrintList();
}
}
int CreateList(NODE** head)
{
int n,loop,value;
printf("How many nodes do you want to add? ");
scanf("%d",&n); // ASKING USER THE LENGTH OF THE LIST
for(loop=0;loop<n;loop++){
temp=(NODE*)malloc(sizeof(NODE));
temp->link=NULL;
printf("\n Enter value at node %d= ",loop+1);
scanf("%d",&temp->data);
if(*head==NULL)
{
*head=temp;//*head is used to access head1 and head2
}
else{
current->link=temp;
}
current=temp;
}//end of for
}
int MergeList()
{
current=(NODE*)malloc(sizeof(NODE));
current=head1;
while(current->link!=NULL)
{ printf("This is working\n");
current=current->link;
printf("%d -> \n",current->data);
}
current->link=head2;
printf("This is working\n");
}
int PrintList(){
current=(NODE*)malloc(sizeof(NODE));
current=head1;
while(current!=NULL){ //condition such that current will reach last block and become equal to NULL and then stop
printf("%d ->" ,current->data );
current=current->link;
}
}