Skip to content

Commit 7460bcb

Browse files
Create Copying_One List_to_another.c
1 parent 0fb432f commit 7460bcb

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Copying_One List_to_another.c

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Write a C Program to copy the elements of one List to another List using Linked List Concept.
2+
3+
// Input:
4+
5+
// m - Elements in List1
6+
7+
// m Elements of List1
8+
9+
// Output:
10+
11+
// m Elements of List2 copied from List1
12+
13+
#include<stdio.h>
14+
#include<stdlib.h>
15+
struct node
16+
{
17+
int element;
18+
struct node *next;
19+
};
20+
typedef struct node node;
21+
node *position;
22+
void insert(node *list,int e)
23+
{
24+
node *newnode=malloc(sizeof(node));
25+
newnode->element=e;
26+
if(list->next==NULL)
27+
{
28+
list->next=newnode;
29+
newnode->next=NULL;
30+
position=newnode;
31+
}
32+
else
33+
{
34+
newnode->next=NULL;
35+
position->next=newnode;
36+
position=newnode;
37+
}
38+
}
39+
void copy(node *list1,node *list2)
40+
{
41+
node *position=list1->next;
42+
node *pos;
43+
while(position!=NULL)
44+
{
45+
node *newnode=malloc(sizeof(node));
46+
newnode->element=position->element;
47+
newnode->next=NULL;
48+
if(list2->next==NULL)
49+
{
50+
list2->next=newnode;
51+
pos=newnode;
52+
}
53+
else
54+
{
55+
pos->next=newnode;
56+
pos=newnode;
57+
}
58+
position=position->next;
59+
}
60+
}
61+
void display(node *list)
62+
{
63+
if(list->next!=NULL)
64+
{
65+
node *position=list->next;
66+
while(position!=NULL)
67+
{
68+
printf("%d\n",position->element);
69+
position=position->next;
70+
}
71+
}
72+
}
73+
int main()
74+
{
75+
int n,m;
76+
node *list1=malloc(sizeof(node));
77+
list1->next=NULL;
78+
node *list2=malloc(sizeof(node));
79+
list2->next=NULL;
80+
scanf("%d",&n);
81+
for(int i=0;i<n;i++)
82+
{
83+
scanf("%d",&m);
84+
insert(list1,m);
85+
}
86+
copy(list1,list2);
87+
display(list2);
88+
return 0;
89+
}

0 commit comments

Comments
 (0)