This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDS_PRACT2.c
121 lines (120 loc) · 2.3 KB
/
DS_PRACT2.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
#include <stdio.h>
int worker(int n, int a[]);
int search_1(int n,int a[]);
int delete_1(int n,int a[]);
int update_1(int n,int a[]);
int main(void)
{
// your code goes here
int i = 0, n, a[100] = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 0};
printf("Enter 1 for Searching, 2 for Deleting, 3 for Updating: \n");
scanf("%d", &n);
worker(n, a);
return 0;
}
int search_1(int n,int a[])
{
int t = 0, f = 0;
printf("Enter the number to be searched: \n");
scanf("%d", &t);
for (int i = 0; i < n; i++)
{
if (a[i] == t)
{
f = 1;
printf("%d exists at %dth position \n", a[i], i + 1);
}
}
if (f == 0)
{
printf("%d does not exist in the list \n", t);
}
return 0;
}
int update_1(int n,int a[])
{
int t = 0, f = 0;
printf("Enter the number to be updated: \n");
scanf("%d", &t);
for (int i = 0; i < n; i++)
{
if (a[i] == t)
{
printf("Enter the new value: \n");
scanf("%d", &a[i]);
f = 1;
break;
}
}
if (f == 0)
{
printf("%d does not exist in the list \n", t);
}
else
{
printf("Updated array is: \n");
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
return 0;
}
int delete_1(int n,int a[])
{
int t = 0, f = 0;
printf("Enter the number to be deleted: \n");
scanf("%d", &t);
// find array length
int i = 0;
for (; i < n; i++)
{
if (a[i] == t)
{
f = 1;
break;
}
}
if (f == 0)
{
printf("%d does not exist in the list \n", t);
}
else
{
for (int j = i; j < n; j++)
{
a[j] = a[j + 1];
}
a[n - 1] = 0;
printf("%d deleted \nNew List:", t);
for (int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
return 0;
}
int worker(int n, int a[])
{
int no = 0;
while (a[no] != 0)
{
no++;
}
switch (n)
{
case 1:
search_1(no,a);
break;
case 2:
delete_1(no,a);
break;
case 3:
update_1(no,a);
break;
default:
printf("Invalid Input");
break;
}
return 0;
}