-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab10_3.c
131 lines (126 loc) · 2.54 KB
/
lab10_3.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*Write a C code to delete all prime numbers present in a doubly linked list.
For example, if input: 5->6->11->4->12->16, then output 6->4->12->16.*/
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next,*prev;
}*first;
void insert(int);
int prime(int);
void delete();
void display();
int main()
{
int n,a;
printf("Enter the number of nodes\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter data\n");
scanf("%d",&a);
insert(a);
}
printf("Double linkedlist:\n");
display();
delete();
printf("Updated double linkedlist:\n");
display();
return 0;
}
void insert(int x)
{
struct node *temp,*ptr;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
if(first==NULL)
{
first=temp;
}
else
{
ptr=first;
while(ptr->next!=NULL)
ptr=ptr->next;
temp->prev=ptr;
ptr->next=temp;
}
}
int prime(int a)
{
int c = 0;
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
c++;
break;
}
}
if (c > 0)
{
return 0;
}
else
{
return 1;
}
}
void display()
{
struct node*ptr;
ptr=first;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void delete()
{
struct node*ptr,*ptr1,*ptr3;
ptr=first;
while(ptr!=NULL)
{
if(prime(ptr->data)==1)
{
if(ptr->prev==NULL)
{
first=first->next;
first->prev=NULL;
ptr1=ptr;
ptr1=NULL;
free(ptr1);
ptr=first;
}
else if(ptr->next==NULL)
{
ptr1=first;
while(ptr1->next->next!=NULL)
ptr1=ptr1->next;
ptr1->next=NULL;
ptr=NULL;
free(ptr);
}
else
{
ptr3=first;
while(ptr3->next!=ptr)
ptr3=ptr3->next;
ptr1=ptr->next;
ptr3->next=ptr->next;
ptr1->prev=ptr->prev;
ptr3=ptr;
ptr3=NULL;
free(ptr3);
ptr=ptr1;
}
}
else
{
ptr=ptr->next;
}
}
}