exp4.c
· 4.6 KiB · C
Raw
/*
* Ex. No. 4: ARRAY IMPLEMENTATION OF LIST
* Course enrollment roster with dynamic array
* Supports: APPEND, INSERT, REMOVE, FIND, DEDUP, RESIZE
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ArrayList {
int *data;
int size;
int capacity;
};
/* Initialize list with capacity 2 */
void initList(struct ArrayList *list) {
list->capacity = 2;
list->size = 0;
list->data = (int *)malloc(list->capacity * sizeof(int));
}
/* Double capacity when full */
void resize(struct ArrayList *list) {
int newCapacity = list->capacity * 2;
int *newData = (int *)malloc(newCapacity * sizeof(int));
for (int i = 0; i < list->size; i++)
newData[i] = list->data[i];
free(list->data);
list->data = newData;
list->capacity = newCapacity;
}
/* APPEND(x): Add element at end */
void append(struct ArrayList *list, int x) {
if (list->size == list->capacity)
resize(list);
list->data[list->size] = x;
list->size++;
}
/* INSERT(pos, x): Insert at position pos */
void insert(struct ArrayList *list, int pos, int x) {
if (pos < 0 || pos > list->size) {
printf("Invalid position for INSERT\n");
return;
}
if (list->size == list->capacity)
resize(list);
for (int i = list->size; i > pos; i--)
list->data[i] = list->data[i - 1];
list->data[pos] = x;
list->size++;
}
/* REMOVE(pos): Remove element at position pos */
void removeAt(struct ArrayList *list, int pos) {
if (pos < 0 || pos >= list->size) {
printf("Invalid position for REMOVE\n");
return;
}
for (int i = pos; i < list->size - 1; i++)
list->data[i] = list->data[i + 1];
list->size--;
}
/* FIND(x): Return index of x, or -1 if not found */
int find(struct ArrayList *list, int x) {
for (int i = 0; i < list->size; i++) {
if (list->data[i] == x)
return i;
}
return -1;
}
/* DEDUP: Remove duplicates, keep first occurrence, preserve order */
void dedup(struct ArrayList *list) {
if (list->size <= 1) return;
int *temp = (int *)malloc(list->size * sizeof(int));
int tempSize = 0;
for (int i = 0; i < list->size; i++) {
int isDup = 0;
for (int j = 0; j < tempSize; j++) {
if (list->data[i] == temp[j]) {
isDup = 1;
break;
}
}
if (!isDup) {
temp[tempSize] = list->data[i];
tempSize++;
}
}
for (int i = 0; i < tempSize; i++)
list->data[i] = temp[i];
list->size = tempSize;
free(temp);
}
/* Display current roster */
void display(struct ArrayList *list) {
printf("Roster: ");
for (int i = 0; i < list->size; i++)
printf("%d ", list->data[i]);
printf("\n");
}
int main() {
struct ArrayList list;
initList(&list);
printf("=== Course Roster ===\n");
printf("Commands (one per line):\n");
printf(" APPEND x -> add x to end\n");
printf(" INSERT pos x -> insert x at index pos\n");
printf(" REMOVE pos -> remove element at pos\n");
printf(" FIND x -> print index of x (-1 if not found)\n");
printf(" DEDUP -> remove duplicates\n");
int M;
printf("How many commands will you enter? ");
scanf("%d", &M);
for (int cmdNum = 1; cmdNum <= M; cmdNum++) {
char cmd[10];
printf("\nCommand %d/%d> ", cmdNum, M);
scanf("%s", cmd);
if (strcmp(cmd, "APPEND") == 0) {
int x;
scanf("%d", &x);
append(&list, x);
printf("Appended %d. ", x);
display(&list);
}
else if (strcmp(cmd, "INSERT") == 0) {
int pos, x;
scanf("%d %d", &pos, &x);
insert(&list, pos, x);
printf("Inserted %d at index %d. ", x, pos);
display(&list);
}
else if (strcmp(cmd, "REMOVE") == 0) {
int pos;
scanf("%d", &pos);
removeAt(&list, pos);
printf("Removed index %d. ", pos);
display(&list);
}
else if (strcmp(cmd, "FIND") == 0) {
int x;
scanf("%d", &x);
int pos = find(&list, x);
printf("FIND(%d) = %d\n", x, pos);
}
else if (strcmp(cmd, "DEDUP") == 0) {
dedup(&list);
printf("Duplicates removed. ");
display(&list);
}
else {
printf("Unknown command: %s\n", cmd);
}
}
printf("\n=== Final roster ===\n");
display(&list);
free(list.data);
return 0;
}
| 1 | /* |
| 2 | * Ex. No. 4: ARRAY IMPLEMENTATION OF LIST |
| 3 | * Course enrollment roster with dynamic array |
| 4 | * Supports: APPEND, INSERT, REMOVE, FIND, DEDUP, RESIZE |
| 5 | */ |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | struct ArrayList { |
| 11 | int *data; |
| 12 | int size; |
| 13 | int capacity; |
| 14 | }; |
| 15 | |
| 16 | /* Initialize list with capacity 2 */ |
| 17 | void initList(struct ArrayList *list) { |
| 18 | list->capacity = 2; |
| 19 | list->size = 0; |
| 20 | list->data = (int *)malloc(list->capacity * sizeof(int)); |
| 21 | } |
| 22 | |
| 23 | /* Double capacity when full */ |
| 24 | void resize(struct ArrayList *list) { |
| 25 | int newCapacity = list->capacity * 2; |
| 26 | int *newData = (int *)malloc(newCapacity * sizeof(int)); |
| 27 | for (int i = 0; i < list->size; i++) |
| 28 | newData[i] = list->data[i]; |
| 29 | free(list->data); |
| 30 | list->data = newData; |
| 31 | list->capacity = newCapacity; |
| 32 | } |
| 33 | |
| 34 | /* APPEND(x): Add element at end */ |
| 35 | void append(struct ArrayList *list, int x) { |
| 36 | if (list->size == list->capacity) |
| 37 | resize(list); |
| 38 | list->data[list->size] = x; |
| 39 | list->size++; |
| 40 | } |
| 41 | |
| 42 | /* INSERT(pos, x): Insert at position pos */ |
| 43 | void insert(struct ArrayList *list, int pos, int x) { |
| 44 | if (pos < 0 || pos > list->size) { |
| 45 | printf("Invalid position for INSERT\n"); |
| 46 | return; |
| 47 | } |
| 48 | if (list->size == list->capacity) |
| 49 | resize(list); |
| 50 | for (int i = list->size; i > pos; i--) |
| 51 | list->data[i] = list->data[i - 1]; |
| 52 | list->data[pos] = x; |
| 53 | list->size++; |
| 54 | } |
| 55 | |
| 56 | /* REMOVE(pos): Remove element at position pos */ |
| 57 | void removeAt(struct ArrayList *list, int pos) { |
| 58 | if (pos < 0 || pos >= list->size) { |
| 59 | printf("Invalid position for REMOVE\n"); |
| 60 | return; |
| 61 | } |
| 62 | for (int i = pos; i < list->size - 1; i++) |
| 63 | list->data[i] = list->data[i + 1]; |
| 64 | list->size--; |
| 65 | } |
| 66 | |
| 67 | /* FIND(x): Return index of x, or -1 if not found */ |
| 68 | int find(struct ArrayList *list, int x) { |
| 69 | for (int i = 0; i < list->size; i++) { |
| 70 | if (list->data[i] == x) |
| 71 | return i; |
| 72 | } |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | /* DEDUP: Remove duplicates, keep first occurrence, preserve order */ |
| 77 | void dedup(struct ArrayList *list) { |
| 78 | if (list->size <= 1) return; |
| 79 | |
| 80 | int *temp = (int *)malloc(list->size * sizeof(int)); |
| 81 | int tempSize = 0; |
| 82 | |
| 83 | for (int i = 0; i < list->size; i++) { |
| 84 | int isDup = 0; |
| 85 | for (int j = 0; j < tempSize; j++) { |
| 86 | if (list->data[i] == temp[j]) { |
| 87 | isDup = 1; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | if (!isDup) { |
| 92 | temp[tempSize] = list->data[i]; |
| 93 | tempSize++; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | for (int i = 0; i < tempSize; i++) |
| 98 | list->data[i] = temp[i]; |
| 99 | list->size = tempSize; |
| 100 | free(temp); |
| 101 | } |
| 102 | |
| 103 | /* Display current roster */ |
| 104 | void display(struct ArrayList *list) { |
| 105 | printf("Roster: "); |
| 106 | for (int i = 0; i < list->size; i++) |
| 107 | printf("%d ", list->data[i]); |
| 108 | printf("\n"); |
| 109 | } |
| 110 | |
| 111 | int main() { |
| 112 | struct ArrayList list; |
| 113 | initList(&list); |
| 114 | |
| 115 | printf("=== Course Roster ===\n"); |
| 116 | printf("Commands (one per line):\n"); |
| 117 | printf(" APPEND x -> add x to end\n"); |
| 118 | printf(" INSERT pos x -> insert x at index pos\n"); |
| 119 | printf(" REMOVE pos -> remove element at pos\n"); |
| 120 | printf(" FIND x -> print index of x (-1 if not found)\n"); |
| 121 | printf(" DEDUP -> remove duplicates\n"); |
| 122 | |
| 123 | int M; |
| 124 | printf("How many commands will you enter? "); |
| 125 | scanf("%d", &M); |
| 126 | |
| 127 | for (int cmdNum = 1; cmdNum <= M; cmdNum++) { |
| 128 | char cmd[10]; |
| 129 | printf("\nCommand %d/%d> ", cmdNum, M); |
| 130 | scanf("%s", cmd); |
| 131 | |
| 132 | if (strcmp(cmd, "APPEND") == 0) { |
| 133 | int x; |
| 134 | scanf("%d", &x); |
| 135 | append(&list, x); |
| 136 | printf("Appended %d. ", x); |
| 137 | display(&list); |
| 138 | } |
| 139 | else if (strcmp(cmd, "INSERT") == 0) { |
| 140 | int pos, x; |
| 141 | scanf("%d %d", &pos, &x); |
| 142 | insert(&list, pos, x); |
| 143 | printf("Inserted %d at index %d. ", x, pos); |
| 144 | display(&list); |
| 145 | } |
| 146 | else if (strcmp(cmd, "REMOVE") == 0) { |
| 147 | int pos; |
| 148 | scanf("%d", &pos); |
| 149 | removeAt(&list, pos); |
| 150 | printf("Removed index %d. ", pos); |
| 151 | display(&list); |
| 152 | } |
| 153 | else if (strcmp(cmd, "FIND") == 0) { |
| 154 | int x; |
| 155 | scanf("%d", &x); |
| 156 | int pos = find(&list, x); |
| 157 | printf("FIND(%d) = %d\n", x, pos); |
| 158 | } |
| 159 | else if (strcmp(cmd, "DEDUP") == 0) { |
| 160 | dedup(&list); |
| 161 | printf("Duplicates removed. "); |
| 162 | display(&list); |
| 163 | } |
| 164 | else { |
| 165 | printf("Unknown command: %s\n", cmd); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | printf("\n=== Final roster ===\n"); |
| 170 | display(&list); |
| 171 | |
| 172 | free(list.data); |
| 173 | return 0; |
| 174 | } |
| 175 |