-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment4.c
83 lines (68 loc) · 1.75 KB
/
Experiment4.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
#include <stdio.h>
#define MAX_SIZE 100
// Define a structure for the list
struct ArrayList {
int data[MAX_SIZE];
int length;
};
// Initialize a new list
void initialize(struct ArrayList* list) {
list->length = 0;
}
// Insert an element at the end of the list
void insert(struct ArrayList* list, int value) {
if (list->length < MAX_SIZE) {
list->data[list->length] = value;
list->length++;
} else {
printf("List is full, cannot insert.\n");
}
}
// Remove an element at a specific index
void removeAt(struct ArrayList* list, int index) {
if (index >= 0 && index < list->length) {
for (int i = index; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
} else {
printf("Index out of bounds.\n");
}
}
// Get the element at a specific index
int get(struct ArrayList* list, int index) {
if (index >= 0 && index < list->length) {
return list->data[index];
} else {
printf("Index out of bounds.\n");
return -1;
}
}
// Print the list
void printList(struct ArrayList* list) {
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
struct ArrayList myList;
// Initialize the list
initialize(&myList);
// Insert elements
insert(&myList, 10);
insert(&myList, 20);
insert(&myList, 30);
printf("List: ");
printList(&myList);
// Remove an element
removeAt(&myList, 1);
printf("List after removing element at index 1: ");
printList(&myList);
// Get an element
int element = get(&myList, 0);
if (element != -1) {
printf("Element at index 0: %d\n", element);
}
return 0;
}