psbhatnagar revised this gist 3 weeks ago. Go to revision
1 file changed, 174 insertions
exp4.c(file created)
| @@ -0,0 +1,174 @@ | |||
| 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 | + | } | |
Newer
Older