Code Review Asked by Parvinder Singh Saini on December 21, 2021
This is my first big project. I missed ArrayList a lot in C and thought that it would a very good learning exercise if I implemented my own in C. I tried to take all the features from Java’s ArrayList and Python’s list. I’d be really glad if you guys could give me some reviews on it.
The only code that needs to be reviewed is ArrayList.h
. Demo.c
is just a demonstration of each function in ArrayList.h
.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
typedef struct List List;
typedef struct List* ListPtr;
struct List {
int capacity;
int size;
int *arr;
};
ListPtr initialiseWithCapacity(int initialCapacity) {
ListPtr a = malloc(sizeof *a);
a->capacity = initialCapacity;
a->arr = malloc(a->capacity * sizeof *(a->arr));
a->size = 0;
return a;
}
ListPtr initialise() {
return initialiseWithCapacity(10);
}
ListPtr initialiseWithArray(int arr[], int length) {
ListPtr a = initialiseWithCapacity(length);
for(int i = 0; i < length; i++)
a->arr[a->size++] = arr[i];
return a;
}
ListPtr values(int n, ...) {
va_list valist;
va_start(valist, n);
ListPtr a = initialiseWithCapacity(n);
for(int i = 0; i < n; i++) {
a->arr[a->size++] = va_arg(valist, int);
}
va_end(valist);
return a;
}
ListPtr range(int start, int stop, int step) {
if(step == 0)
return initialiseWithCapacity(0);
ListPtr a = initialiseWithCapacity( abs(stop - start) / abs(step) + 1 );
if(step > 0) {
for(int i = start; i < stop; i += step)
a->arr[a->size++] = i;
}
else {
for(int i = start; i > stop; i += step)
a->arr[a->size++] = i;
}
return a;
}
ListPtr slice(ListPtr a, int start, int stop, int step) {
if(step == 0 || start < 0 || stop < -1 || start >= a->size || stop >= a->size)
return initialiseWithCapacity(0);
ListPtr b = initialiseWithCapacity( abs(stop - start) / abs(step) + 1 );
if(step > 0) {
for(int i = start; i < stop; i += step)
b->arr[b->size++] = a->arr[i];
}
else {
for(int i = start; i > stop; i += step)
b->arr[b->size++] = a->arr[i];
}
return b;
}
bool clear(ListPtr a) {
free(a->arr);
a->arr = malloc(0);
a->capacity = 0;
a->size = 0;
return true;
}
bool ensureCapacity(ListPtr a, int minCapacity) {
if(minCapacity > a->capacity) {
a->capacity += (a->capacity >> 1);
if(a->capacity < minCapacity)
a->capacity = minCapacity;
a->arr = realloc(a->arr, a->capacity * sizeof *(a->arr));
}
return true;
}
bool trimToSize(ListPtr a) {
a->capacity = a->size;
a->arr = realloc(a->arr, a->capacity * sizeof *(a->arr));
return true;
}
bool fill(ListPtr a, int value, int n) {
ensureCapacity(a, n);
a->size = n;
for(int i = 0; i < n; i++) {
a->arr[i] = value;
}
return true;
}
bool append(ListPtr a, int n) {
ensureCapacity(a, a->size + 1);
a->arr[a->size++] = n;
return true;
}
bool extendWithArray(ListPtr a, int arr[], int length) {
ensureCapacity(a, a->size + length);
for(int i = 0; i < length; i++)
a->arr[a->size++] = arr[i];
return true;
}
bool extend(ListPtr a, ListPtr b) {
extendWithArray(a, b->arr, b->size);
return true;
}
bool insert(ListPtr a, int index, int n) {
if(index < 0)
index = a->size + index;
if(index > a->size || index < 0)
return false;
if(index == a->size) {
append(a, n);
return true;
}
ensureCapacity(a, a->size + 1);
for(int i = a->size; i > index; i--) {
a->arr[i] = a->arr[i-1];
}
a->arr[index] = n;
a->size++;
return true;
}
ListPtr copy(ListPtr a) {
ListPtr b = initialiseWithCapacity(a->capacity);
extendWithArray(b, a->arr, a->size);
return b;
}
int indexOf(ListPtr a, int value) {
for(int i = 0; i < a->size; i++) {
if(a->arr[i] == value)
return i;
}
return -1;
}
int lastIndexOf(ListPtr a, int value) {
for(int i = a->size - 1; i >= 0; i--) {
if(a->arr[i] == value)
return i;
}
return -1;
}
int binarySearch(ListPtr a, int value) {
int lower = 0, upper = a->size - 1, mid = 0;
while(lower <= upper) {
mid = (lower + upper) / 2;
if(a->arr[mid] == value)
return mid;
else if(a->arr[mid] < value)
lower = mid + 1;
else
upper = mid - 1;
}
return -1;
}
bool contains(ListPtr a, int value) {
return indexOf(a, value) >= 0;
}
bool isEmpty(ListPtr a) {
return a->size == 0;
}
bool isEqual(ListPtr a, ListPtr b) {
if(a->size != b->size)
return false;
for(int i = 0; i < a->size; i++) {
if(a->arr[i] != b->arr[i])
return false;
}
return true;
}
bool pop(ListPtr a, int index) {
if(index < 0)
index = a->size + index;
if(index >= a->size || index < 0)
return false;
for(int i = index; i < a->size-1; i++)
a->arr[i] = a->arr[i+1];
a->size--;
return true;
}
bool delete(ListPtr a, int value) {
int index = indexOf(a, value);
if(index == -1)
return false;
return pop(a, index);
}
int get(ListPtr a, int index) {
if(index < 0)
index = a->size + index;
if(index >= a->size || index < 0)
return -1;
return a->arr[index];
}
bool set(ListPtr a, int index, int value) {
if(index < 0)
index = a->size + index;
if(index >= a->size || index < 0)
return false;
a->arr[index] = value;
return true;
}
bool reverse(ListPtr a) {
for(int start = 0, stop = a->size-1; start < stop; start++, stop--) {
int t = a->arr[start];
a->arr[start] = a->arr[stop];
a->arr[stop] = t;
}
return true;
}
bool replace(ListPtr a, int oldValue, int newValue) {
for(int i = 0; i < a->size; i++) {
if(a->arr[i] == oldValue)
a->arr[i] = newValue;
}
return true;
}
/*Code for sorting begins*/
int comparisonFunctionAscending (const void *a, const void *b) {
return ( *(int*)a - *(int*)b );
}
int comparisonFunctionDescending (const void *a, const void *b) {
return ( *(int*)b - *(int*)a );
}
bool sort(ListPtr a) {
qsort(a->arr, a->size, sizeof(int), comparisonFunctionAscending);
return true;
}
bool sortReverse(ListPtr a) {
qsort(a->arr, a->size, sizeof(int), comparisonFunctionDescending);
return true;
}
/*Code for sorting ends*/
int count(ListPtr a, int value) {
int c = 0;
for(int i = 0; i < a->size; i++) {
if(a->arr[i] == value)
c++;
}
return c;
}
int sum(ListPtr a) {
int s = 0;
for(int i = 0; i < a->size; i++)
s += a->arr[i];
return s;
}
int len(ListPtr a) {
return a->size;
}
int cap(ListPtr a) {
return a->capacity;
}
int* toArray(ListPtr a) {
int *b = malloc(a->size * sizeof *b);
for(int i = 0; i < a->size; i++)
b[i] = a->arr[i];
return b;
}
char* toString(ListPtr a) {
int iMax = a->size - 1;
if(iMax == -1)
return "[]";
char *str = malloc( (a->size * 12 + 1) * sizeof *str );
strcpy(str, "[");
for(int i = 0; ; i++) {
char temp[11];
sprintf(temp, "%d", a->arr[i]);
strcat(str, temp);
if(i == iMax) {
strcat(str, "]");
return str;
}
strcat(str, ", ");
}
}
bool display(ListPtr a) {
printf("%s n", toString(a));
return true;
}
#include "ArrayList.h"
void initialiseDemo() {
printf("ntt =============== START DEMO - initialise() =============== nnn");
printf("Initialising List a with initialise() nn");
List *a = initialise();
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 5, 9 nn");
append(a, 5);
append(a, 9);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - initialise() =============== nnn");
}
void initialiseWithCapacityDemo() {
printf("ntt =============== START DEMO - initialiseWithCapacity() =============== nnn");
printf("Initialising List a with initialiseWithCapacity(0) nn");
List *a = initialiseWithCapacity(0);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 5, 4, 15, 9, 19 nn");
append(a, 5);
append(a, 4);
append(a, 15);
append(a, 9);
append(a, 19);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - initialiseWithCapacity() =============== nnn");
}
void initialiseWithArrayDemo() {
printf("ntt =============== START DEMO - initialiseWithArray() =============== nnn");
printf("Initialising int arr[] with numbers from 1 to 18 nn");
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
printf("Initialising List a with arr[] using initialiseWithArray(arr, 18) nn");
List *a = initialiseWithArray(arr, 18);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - initialiseWithArray() =============== nnn");
}
void valuesDemo() {
printf("ntt =============== START DEMO - values() =============== nnn");
printf("Initialising List a with values(5, 1, 2, 5, 9, 17) nn");
List *a = values(5, 1, 2, 5, 9, 17);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - values() =============== nnn");
}
void rangeDemo() {
printf("ntt =============== START DEMO - range() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising List b with range(10, -1, -2) nn");
List *b = range(10, -1, -2);
printf("t b = %s n", toString(b));
printf("t Number of elements = %d n", len(b));
printf("t Capacity of a = %d nn", cap(b));
printf("toString( range(5, 51, 5) ) = %s n", toString( range(5, 51, 5) ));
printf("nntt =============== END DEMO - range() =============== nnn");
}
void sliceDemo() {
printf("ntt =============== START DEMO - slice() =============== nnn");
printf("Initialising List a with range(10, 21, 1) nn");
List *a = range(10, 21, 1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("toString( slice(a, 2, 6, 1) ) = %s nn", toString( slice(a, 2, 6, 1) ));
printf("toString( slice(a, 10, -1, -2) ) = %s nn", toString( slice(a, 10, -1, -2) ));
printf("nntt =============== END DEMO - slice() =============== nnn");
}
void clearDemo() {
printf("ntt =============== START DEMO - clear() =============== nnn");
printf("Initialising List a with range(0, 11, 1) nn");
List *a = range(0, 11, 1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Removing all elements from List a using clear(a) nn");
clear(a);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 5 nn");
append(a, 5);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - clear() =============== nnn");
}
void ensureCapacityDemo() {
printf("ntt =============== START DEMO - ensureCapacity() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Ensuring a minimum capacity of 2 using ensureCapacity(a, 2) nn");
ensureCapacity(a, 2);
printf("t Capacity of a = %d nn", cap(a));
printf("Ensuring a minimum capacity of 6 using ensureCapacity(a, 6) nn");
ensureCapacity(a, 6);
printf("t Capacity of a = %d nn", cap(a));
printf("Ensuring a minimum capacity of 7 using ensureCapacity(a, 7) nn");
ensureCapacity(a, 7);
printf("t Capacity of a = %d nn", cap(a));
printf("Ensuring a minimum capacity of 20 using ensureCapacity(a, 20) nn");
ensureCapacity(a, 20);
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - ensureCapacity() =============== nnn");
}
void trimToSizeDemo() {
printf("ntt =============== START DEMO - trimToSize() =============== nnn");
printf("Initialising List a with initialise() nn");
List *a = initialise();
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising int arr[] with numbers from 1 to 6 nn");
int arr[] = {1, 2, 3, 4, 5, 6};
printf("Extending List a with arr[] using extendWithArray(a, arr, 6) nn");
extendWithArray(a, arr, 6);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Trimming Unused Space in List a using trimToSize(a) nn");
trimToSize(a);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - trimToSize() =============== nnn");
}
void fillDemo() {
printf("ntt =============== START DEMO - fill() =============== nnn");
printf("Initialising List a with initialise() nn");
List *a = initialise();
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Filling List a with 20 occurences of 5 using fill(a, 5, 20) nn");
fill(a, 5, 20);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Filling List a with 7 occurences of 19 using fill(a, 19, 7) nn");
fill(a, 19, 7);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - fill() =============== nnn");
}
void appendDemo() {
printf("ntt =============== START DEMO - append() =============== nnn");
printf("Initialising List a with initialiseWithCapacity(0) nn");
List *a = initialiseWithCapacity(0);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 5, 4, 15, 9, 19 using append(a, n) nn");
append(a, 5);
append(a, 4);
append(a, 15);
append(a, 9);
append(a, 19);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - append() =============== nnn");
}
void extendWithArrayDemo() {
printf("ntt =============== START DEMO - extendWithArray() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising int arr[] with numbers from 1 to 6 nn");
int arr[] = {1, 2, 3, 4, 5, 6};
printf("Extending List a with arr[] using extendWithArray(a, arr, 6) nn");
extendWithArray(a, arr, 6);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - extendWithArray() =============== nnn");
}
void extendDemo() {
printf("ntt =============== START DEMO - extend() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising List b with range(1, 11, 2) nn");
List *b = range(1, 11, 2);
printf("t b = %s n", toString(b));
printf("t Number of elements = %d n", len(b));
printf("t Capacity of b = %d nn", cap(b));
printf("Extending List a with List b using extend(a, b) nn");
extend(a, b);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Extending List a using extend(a, range(90, 96, 2)) nn");
extend(a, range(90, 96, 2));
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - extend() =============== nnn");
}
void insertDemo() {
printf("ntt =============== START DEMO - insert() =============== nnn");
printf("Initialising List a with range(0, 10, 1) nn");
List *a = range(0, 10, 1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Inserting 99 at 0th index using insert(a, 0, 99) nn");
insert(a, 0, 99);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Inserting 88 at the end using insert(a, len(a), 88) nn");
insert(a, len(a), 88);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Inserting 55 at 5th index using insert(a, 5, 55) nn");
insert(a, 5, 55);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Inserting 22 at the last index using insert(a, -1, 22) nn");
insert(a, -1, 22);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - insert() =============== nnn");
}
void copyDemo() {
printf("ntt =============== START DEMO - copy() =============== nnn");
printf("Initialising List a with range(0, 10, 2) nn");
List *a = range(0, 10, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising List b with copy(a) nn");
List *b = copy(a);
printf("t b = %s n", toString(b));
printf("t Number of elements = %d n", len(b));
printf("t Capacity of b = %d n", cap(b));
printf("nntt =============== END DEMO - copy() =============== nnn");
}
void indexOfDemo() {
printf("ntt =============== START DEMO - indexOf() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Searching for the first occurrence of numbers from 0 to 10 using indexOf(a, i) nn");
for(int i = 0; i < 11; i++) {
int index = indexOf(a, i);
if(index < 0)
printf("t %d not found in a n", i);
else
printf("t %d found at index %d n", i, index);
}
printf("nntt =============== END DEMO - indexOf() =============== nnn");
}
void lastIndexOfDemo() {
printf("ntt =============== START DEMO - lastIndexOf() =============== nnn");
printf("Initialising int arr[] with {0, 1, 2, 2, 3, 4, 5, 6, 2, 7, 8, 9} nn");
int arr[] = {0, 1, 2, 2, 3, 4, 5, 6, 2, 7, 8, 9};
printf("Initialising List a with arr[] using initialiseWithArray(arr, 12) nn");
List *a = initialiseWithArray(arr, 12);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Searching for the last occurrence of 2, 10 using lastIndexOf(a, i) nn");
printf("t Last index of 2 = %d n", lastIndexOf(a, 2));
printf("t Last index of 10 = %d n", lastIndexOf(a, 10));
printf("nntt =============== END DEMO - lastIndexOf() =============== nnn");
}
void binarySearchDemo() {
printf("ntt =============== START DEMO - binarySearch() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Searching for the index of numbers from 0 to 10 using binarySearch(a, i) nn");
for(int i = 0; i < 11; i++) {
int index = binarySearch(a, i);
if(index < 0)
printf("t %d not found in a n", i);
else
printf("t %d found at index %d n", i, index);
}
printf("nntt =============== END DEMO - binarySearch() =============== nnn");
}
void containsDemo() {
printf("ntt =============== START DEMO - contains() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Checking for the presence of numbers from 0 to 10 using contains(a, i) nn");
for(int i = 0; i < 11; i++) {
if(contains(a, i))
printf("t a contains %d n", i);
else
printf("t a doesn't contain %d n", i);
}
printf("nntt =============== END DEMO - contains() =============== nnn");
}
void isEmptyDemo() {
printf("ntt =============== START DEMO - isEmpty() =============== nnn");
printf("Initialising List a with range(0, 11, 1) nn");
List *a = range(0, 11, 1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Checking if a is empty using isEmpty(a) nn");
if(isEmpty(a))
printf("t a is empty nn");
else
printf("t a is not empty nn");
printf("Removing all elements from List a using clear(a) nn");
clear(a);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Checking if a is empty using isEmpty(a) nn");
if(isEmpty(a))
printf("t a is empty n");
else
printf("t a is not empty n");
printf("nntt =============== END DEMO - isEmpty() =============== nnn");
}
void isEqualDemo() {
printf("ntt =============== START DEMO - isEqual() =============== nnn");
printf("Initialising List a with range(0, 10, 2) nn");
List *a = range(0, 10, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising List b with copy(a) nn");
List *b = copy(a);
printf("t b = %s n", toString(b));
printf("t Number of elements = %d n", len(b));
printf("t Capacity of b = %d nn", cap(b));
printf("Checking if List a is equal to List b using isEqual(a, b) nn");
if(isEqual(a, b))
printf("t a is equal to b nn");
else
printf("t a is not equal to b nn");
printf("Appending 0 to List b using append(b, 0) nn");
append(b, 0);
printf("t b = %s n", toString(b));
printf("t Number of elements = %d n", len(b));
printf("t Capacity of b = %d nn", cap(b));
printf("Checking if List a is equal to List b using isEqual(a, b) nn");
if(isEqual(a, b))
printf("t a is equal to b nn");
else
printf("t a is not equal to b nn");
printf("Appending 0 to List a using append(a, 0) nn");
append(a, 0);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Checking if List a is equal to List b using isEqual(a, b) nn");
if(isEqual(a, b))
printf("t a is equal to b n");
else
printf("t a is not equal to b n");
printf("nntt =============== END DEMO - isEqual() =============== nnn");
}
void popDemo() {
printf("ntt =============== START DEMO - pop() =============== nnn");
printf("Initialising List a with range(0, 11, 1) nn");
List *a = range(0, 11, 1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Removing first element of a using pop(a, 0) nn");
pop(a, 0);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Removing last element of a using pop(a, -1) nn");
pop(a, -1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Trying to remove value at index 10 from a using pop(a, 10) nn");
if(pop(a, 10))
printf("t a = %s nn", toString(a));
else
printf("t index %d not found in a nn", 10);
printf("Trying to remove value at index 2 from a using pop(a, 2) nn");
if(pop(a, 2))
printf("t a = %s nn", toString(a));
else
printf("t index %d not found in a nn", 10);
printf("Removing value at 2nd last index using pop(a, -2) nn");
pop(a, -2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - pop() =============== nnn");
}
void deleteDemo() {
printf("ntt =============== START DEMO - delete() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Trying to delete numbers from 0 to 10 using delete(a, i) nn");
for(int i = 0; i < 11; i++) {
if(delete(a, i))
printf("t %d deleted from a n", i);
else
printf("t a doesn't contain %d n", i);
}
printf("n");
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - delete() =============== nnn");
}
void getDemo() {
printf("ntt =============== START DEMO - get() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Trying to print the values at indexes 0 to 10 using get(a, i) nn");
for(int i = 0; i < 11; i++) {
printf("t Value at index %d = %d n", i, get(a, i));
}
printf("nntt =============== END DEMO - get() =============== nnn");
}
void setDemo() {
printf("ntt =============== START DEMO - set() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Trying to double the value of all elements using set(a, i, get(a, i) * 2) nn");
for(int i = 0; i < 11; i++) {
if(set(a, i, get(a, i) * 2))
printf("t Doubling value at index %d successful n", i);
else
printf("t Index %d not found n", i);
}
printf("n");
printf("a after doubling nn");
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - set() =============== nnn");
}
void reverseDemo() {
printf("ntt =============== START DEMO - reverse() =============== nnn");
printf("Initialising List a with range(0, 11, 1) nn");
List *a = range(0, 11, 1);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Reversing the order of elements using reverse(a) nn");
reverse(a);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - reverse() =============== nnn");
}
void replaceDemo() {
printf("ntt =============== START DEMO - replace() =============== nnn");
printf("Initialising int arr[] with {0, 1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 2, 6, 2} nn");
int arr[] = {0, 1, 2, 3, 4, 5, 6, 1, 2, 2, 3, 4, 2, 6, 2};
printf("Initialising List a with arr[] using initialiseWithArray(arr, 15) nn");
List *a = initialiseWithArray(arr, 15);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Replacing all occurrences of 2 with 99 using replace(a, 2, 99) nn");
replace(a, 2, 99);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - replace() =============== nnn");
}
void sortDemo() {
printf("ntt =============== START DEMO - sort() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Extending List a using extend(a, range(1, 11, 2)) nn");
extend(a, range(1, 11, 2));
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Sorting the elements of a in increasing order using sort(a) nn");
sort(a);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - sort() =============== nnn");
}
void sortReverseDemo() {
printf("ntt =============== START DEMO - sortReverse() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Extending List a using extend(a, range(1, 11, 2)) nn");
extend(a, range(1, 11, 2));
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Sorting the elements of a in increasing order using sortReverse(a) nn");
sortReverse(a);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - sortReverse() =============== nnn");
}
void countDemo() {
printf("ntt =============== START DEMO - count() =============== nnn");
printf("Initialising int arr[] with {1, 2, 2, 3, 3, 3, 5, 5, 5, 5} nn");
int arr[] = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5};
printf("Initialising List a with arr[] using initialiseWithArray(arr, 10) nn");
List *a = initialiseWithArray(arr, 10);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Counting the frequency of numbers from 0 to 5 using count(a, i) nn");
for(int i = 0; i <= 5; i++)
printf("t %d is present %d times n", i, count(a, i));
printf("nntt =============== END DEMO - count() =============== nnn");
}
void sumDemo() {
printf("ntt =============== START DEMO - sum() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Calculating sum of all elements of a using sum(a) nn");
printf("t Sum(a) = %d n", sum(a));
printf("nntt =============== END DEMO - sum() =============== nnn");
}
void lenDemo() {
printf("ntt =============== START DEMO - len() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 5, 9 nn");
append(a, 5);
append(a, 9);
printf("t a = %s n", toString(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Finding new length of a using len(a) nn");
printf("t Len(a) = %d n", len(a));
printf("nntt =============== END DEMO - len() =============== nnn");
}
void capDemo() {
printf("ntt =============== START DEMO - cap() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 5 nn");
append(a, 5);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d nn", len(a));
printf("Finding new capacity of a using cap(a) nn");
printf("t Cap(a) = %d n", cap(a));
printf("nntt =============== END DEMO - cap() =============== nnn");
}
void toArrayDemo() {
printf("ntt =============== START DEMO - toArray() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("t a = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Initialising int arr[] with elements of List a using toArray(a) nn");
int *arr = toArray(a);
int arrLength = len(a);
printf("t arr[] = ");
for(int i = 0; i < arrLength; i++)
printf("%d ", arr[i]);
printf("n");
printf("nntt =============== END DEMO - toArray() =============== nnn");
}
void toStringDemo() {
printf("ntt =============== START DEMO - toString() =============== nnn");
printf("Initialising List a with range(1000000000, 1000000009, 1) nn");
List *a = range(1000000000, 1000000009, 1);
printf("t toString(a) = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d nn", cap(a));
printf("Appending 1111111111 nn");
append(a, 1111111111);
printf("t toString(a) = %s n", toString(a));
printf("t Number of elements = %d n", len(a));
printf("t Capacity of a = %d n", cap(a));
printf("nntt =============== END DEMO - toString() =============== nnn");
}
void displayDemo() {
printf("ntt =============== START DEMO - display() =============== nnn");
printf("Initialising List a with range(0, 11, 2) nn");
List *a = range(0, 11, 2);
printf("Displaying the elements of a using display(a) nn");
printf("t ");
display(a);
printf("n");
printf("Appending 5 nn");
append(a, 5);
printf("Displaying the elements of a using display(a) nn");
printf("t ");
display(a);
printf("nntt =============== END DEMO - display() =============== nnn");
}
int main(void) {
initialiseDemo();
initialiseWithCapacityDemo();
initialiseWithArrayDemo();
valuesDemo();
rangeDemo();
sliceDemo();
clearDemo();
ensureCapacityDemo();
trimToSizeDemo();
fillDemo();
appendDemo();
extendWithArrayDemo();
extendDemo();
insertDemo();
copyDemo();
indexOfDemo();
lastIndexOfDemo();
binarySearchDemo();
containsDemo();
isEmptyDemo();
isEqualDemo();
popDemo();
deleteDemo();
getDemo();
setDemo();
reverseDemo();
replaceDemo();
sortDemo();
sortReverseDemo();
countDemo();
sumDemo();
lenDemo();
capDemo();
toArrayDemo();
toStringDemo();
displayDemo();
return 0;
}
(This was a comment, but then I had more thoughts...)
I have some stylistic things for you to think about, but can't tell you what's "better".
What you've defined is an API for manipulating an ArrayList. I feel like it also shows that you've come from an OOP language, but may not have all the things you want out of your C implementation.
One of the first things I'd do is actually split this into an implementation (in a .c
file) and an API definition (in a .h
file). In practice, this means you'd have declarations of functions that you want to expose in the .h
file, and then you'd have definitions of those functions in the corresponding .c
file.
The other very important thing you can do is comment your exposed API.
ArrayList.h
/*
* Create an ArrayList with specified initial capacity (can be resized).
*/
ListPtr initialiseWithCapacity(int initialCapacity);
/*
* Create an ArrayList using the first `length` elements of the given array.
*/
ListPtr initialiseWithArray(int arr[], int length);
ArrayList.c
ListPtr initialiseWithCapacity(int initialCapacity) {
ListPtr a = malloc(sizeof *a);
a->capacity = initialCapacity;
a->arr = malloc(a->capacity * sizeof *(a->arr));
a->size = 0;
return a;
}
ListPtr initialiseWithArray(int arr[], int length) {
ListPtr a = initialiseWithCapacity(length);
for(int i = 0; i < length; i++)
a->arr[a->size++] = arr[i];
return a;
}
Once you've separated your implementation and API definition, another thing you could consider consider is whether or not you want to actually expose the real type of ListPtr
to the end-user, or if you want it to be an opaque pointer to a list.
The reason I say this is because the code looks like it's trying to adopt OOP-style encapsulation, but it also exposes the underlying List *
.
In your case, it'd be:
ArrayList.h
/*
* Note that List isn't defined in this file!
*
* The user of your API now has to use your initialisation functions to get a ListPtr type.
* This has advantages:
* - You can replace the underlying implementation without breaking user code
* - You can add happily add extra internal stuff that you don't want the user
* to play with (e.g. do you really want them changing the size field?)
* - You clarify your expectation of how your API is used
*/
typedef struct *List ListPtr;
ArrayList.c
struct List {
int capacity;
int size;
int *arr;
}
See this answer for a much more involved and better description.
static
functionsAnother benefit of splitting your API definition and implementation is that you can hide away the functions you don't want your user to mess with. That's what static
is useful for with functions (see here for more details).
In your case, I haven't considered in much detail, but at the least I'd make your int comparisonFunctionAscending (...)
and int comparisonFunctionDescending (...)
functions static.
Another OOPism here is that a lot of these functions look like methods. That's certainly ok if it's what you're going for (see the previous sections). However, if you're not hiding the implementation of ListPtr
, then I think it'd be easier for a C programmer to read:
if (my_list->len != 0) {
...do stuff...
}
rather than:
if (is_empty(my_list)) {
...do stuff...
}
This is contentious, but it's worth considering if you want to avoid early returns (i.e. just return once from your function at the very end). There's no right or wrong answer, but you should think about what is easiest for you.
void
functionsYou have some odd functions that return bool
(e.g. bool clear(...)
), which always return true
. Just use void
, or even better, consider some error handling and return some type saying whether the operation succeeded or not.
Answered by Kyle_S-C on December 21, 2021
(s)size_t
Use size_t
(It is in <stddef.h>
in standard C) or ssize_t
(<sys/types.h>
in POSIX systems) for raw sizes in bytes.
ptrdiff_t
Use ptrdiff_t
(<stddef.h>
) for element counts such as List.size
or array indices such as [i]
.
Don't restrict the data to int
.
Use void *arr
instead so that the user can fill the array with any data.
If you do this, then many of the specific functions for sorting, summing, etc, should be removed, as the data will not be known. The user should write its own loops for summing, use the standard qsort()
with a custom comparison function for sorting, etc.
You are using very generic names for functions such as clear()
. If you want to have this as a library, you may want to prefix all functions with something like arrlist_
to avoid name clashing.
(void)
Empty parentheses in function definitions mean an implicit (void)
, but in function prototypes mean an unspecified number of parameters, which if I remember correctly is a deprecated feature. It's better to explicitly use (void)
always and avoid any problems.
Answered by alx on December 21, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP