-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSinglyLinkedList.c
117 lines (103 loc) · 2.85 KB
/
SinglyLinkedList.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *create_list(struct node *start);
void display(struct node *start);
struct node *addatbeg(struct node *start,int data);
struct node *addatend(struct node *start,int data);
void EvenOdd(struct node *start1,struct node **start2,struct node **start3 );
int main()
{
struct node *start1=NULL,*start2=NULL,*start3=NULL;
start1=create_list(start1);
EvenOdd(start1, &start2, &start3);
printf("\nOriginal List L1 is :\n");
display(start1);
printf("Odd Numbers List L2 is :\n");
display(start2);
printf("Even Numbers List L3 is :\n");
display(start3);
return 0;
}
void EvenOdd(struct node *start1,struct node **start2,struct node **start3 )
{
struct node *p1=start1, *p2=*start2, *p3=*start3;
while(p1!=NULL)
{
if( (p1->info)%2 != 0 )
{
if(*start2==NULL)
*start2 = addatbeg(*start2,p1->info);
else
*start2 = addatend(*start2,p1->info);
}
else
{
if(*start3==NULL)
*start3 = addatbeg(*start3,p1->info);
else
*start3 = addatend(*start3,p1->info);
}
p1=p1->link;
}
}
struct node *create_list(struct node *start)
{
int i,n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
start=NULL;
if(n==0)
return start;
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
start=addatbeg(start,data);
for(i=2;i<=n;i++)
{
printf("\nEnter the element to be inserted : ");
scanf("%d",&data);
start=addatend(start,data);
}
return start;
}
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("Empty\n");
return;
}
p=start;
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n\n");
}
struct node *addatbeg(struct node *start,int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}
struct node *addatend(struct node *start,int data)
{
struct node *p,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
}