-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopying_One List_to_another.c
89 lines (83 loc) · 1.65 KB
/
Copying_One List_to_another.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
// Write a C Program to copy the elements of one List to another List using Linked List Concept.
// Input:
// m - Elements in List1
// m Elements of List1
// Output:
// m Elements of List2 copied from List1
#include<stdio.h>
#include<stdlib.h>
struct node
{
int element;
struct node *next;
};
typedef struct node node;
node *position;
void insert(node *list,int e)
{
node *newnode=malloc(sizeof(node));
newnode->element=e;
if(list->next==NULL)
{
list->next=newnode;
newnode->next=NULL;
position=newnode;
}
else
{
newnode->next=NULL;
position->next=newnode;
position=newnode;
}
}
void copy(node *list1,node *list2)
{
node *position=list1->next;
node *pos;
while(position!=NULL)
{
node *newnode=malloc(sizeof(node));
newnode->element=position->element;
newnode->next=NULL;
if(list2->next==NULL)
{
list2->next=newnode;
pos=newnode;
}
else
{
pos->next=newnode;
pos=newnode;
}
position=position->next;
}
}
void display(node *list)
{
if(list->next!=NULL)
{
node *position=list->next;
while(position!=NULL)
{
printf("%d\n",position->element);
position=position->next;
}
}
}
int main()
{
int n,m;
node *list1=malloc(sizeof(node));
list1->next=NULL;
node *list2=malloc(sizeof(node));
list2->next=NULL;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&m);
insert(list1,m);
}
copy(list1,list2);
display(list2);
return 0;
}