-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array Data Structure.c
86 lines (76 loc) · 2.42 KB
/
Array Data Structure.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
/* Array Data Structure implementation
Date created: Saturday; February 01, 2023 */
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int array[MAX_SIZE];
int n = 0;
void add(int value) {
if (n < MAX_SIZE) {
array[n++] = value;
printf("Value added successfully!\n");
} else {
printf("Array is full. Cannot add more values.\n");
}
}
void update(int index, int value) {
if (index >= 0 && index < n) {
array[index] = value;
printf("Value updated successfully!\n");
} else {
printf("Invalid index. Cannot update value.\n");
}
}
void delete(int index) {
if (index >= 0 && index < n) {
for (int i = index; i < n - 1; i++) {
array[i] = array[i + 1];
}
n--;
printf("Value deleted successfully!\n");
} else {
printf("Invalid index. Cannot delete value.\n");
}
}
int main() {
int decision, value, index;
while (1) {
printf("1. Add\n");
printf("2. Update\n");
printf("3. Delete\n");
printf("4. Exit\n");
printf("Enter your decision: ");
scanf("%d", &decision);
switch (decision) {
case 1:
printf("Enter the value to be added: ");
scanf("%d", &value);
add(value);
break;
case 2:
printf("Enter the index of the value to be updated: ");
scanf("%d", &index);
printf("Enter the new value: ");
scanf("%d", &value);
update(index, value);
break;
case 3:
printf("Enter the index of the value to be deleted: ");
scanf("%d", &index);
delete(index);
break;
case 4:
for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");
exit(0);
default:
printf("Invalid decision. Please enter a valid option.\n");
break;
}
}
return 0;
}
// This implementation uses a fixed-size array of size MAX_SIZE to store the values, and the n variable to keep track of the number of elements currently stored in the array. The add function adds a new value to the end of the array, the update function updates the value at a specific index, and the delete function removes the value at a specific index.
// In this code, the user is prompted to make a decision on what operation to perform (add, update, delete, or exit), and the appropriate function is called based on the decision. The program will keep prompting the user for a decision until they choose to exit the program.