ArraList in C. The CODE implements the following function: Create an ArrayList; Display an ArrayList; Inverse an ArrayList; Sort an ArrayList; Get a value in ArrayList; Get Maximum and Minimum value in ArrayList; Append / Insert / Delete value in ArrayList.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h>

#define arrayList aL

struct arrayList {
	int * pBase;
	int len;
	int cnt;
};

void init(struct aL * arr, int length);
void show(struct aL arr);
void inversion(struct aL * arr);
void sort(struct aL * arr);
int get(struct aL arr, int pos);
int maxA(struct aL arr);
int minA(struct aL arr);
bool append(struct aL * arr, int value);
bool insert(struct aL * arr, int pos, int value);
bool del(struct aL * arr, int pos);
bool isEmpty(struct aL arr);
bool isFull(struct aL arr);

int main(void) {
	struct aL array;
	init(&array, 6);
	append(&array, 1);
	append(&array, 2);
	append(&array, 3);
	append(&array, 4);
	append(&array, 5);
	insert(&array, 6, -1);
	del(&array, 2);
	show(array);
	printf("%d\n", get(array, 5));
	inversion(&array);
	show(array);
	printf("%d\n", maxA(array));
	sort(&array);
	show(array);
	return 0;
}

void init(struct aL * arr, int length) {
	arr->pBase = (int *)malloc(sizeof(int) * length);
	if (arr->pBase == NULL) {
		printf("分配失败");
		exit(-1);
	}
	arr->len = length;
	arr->cnt = 0;
}

bool isEmpty(struct aL arr) {
	if (arr.cnt == 0)
		return true;
	else
		return false;
}

bool isFull(struct aL arr) {
	if (arr.cnt == arr.len)
		return true;
	else
		return false;
}

void show(struct aL arr) {
	if (isEmpty(arr))
		printf("NULL");
	else {
		for (int i = 0; i < arr.cnt; i++) {
			printf("%d ", arr.pBase[i]);
		}
		printf("\n");
	}
}

bool append(struct aL * arr, int value) {
	if (isFull(*arr))
		return false;
	arr->pBase[arr->cnt] = value;
	arr->cnt++;
	return true;
}

bool insert(struct aL * arr, int pos, int value) {
	if (isFull(*arr))
		return false;
	if (pos < 1 || pos > arr->cnt + 1)
		return false;

	arr->cnt++;

	for (int k = arr->cnt - 1; k > pos - 1; k--) {
		arr->pBase[k] = arr->pBase[k-1];
	}
	arr->pBase[pos - 1] = value;
	return true;
}

bool del(struct aL * arr, int pos) {
	if (isEmpty(*arr))
		return false;
	if (pos < 1 || pos > arr->cnt)
		return false;

	for (int k = pos - 1; k < arr->cnt - 1; k++) {
		arr->pBase[k] = arr->pBase[k + 1];
	}

	arr->cnt--;
	return true;
}

int get(struct aL arr, int pos) {
	if (pos < 1 || pos > arr.cnt)
		exit(-1);
	else
		return arr.pBase[pos - 1];
}

void inversion(struct aL * arr) {
	int pre = 0, post = arr->cnt - 1, temp;

	for (int i = 0; i < (arr->cnt / 2); i++) {
		temp = arr->pBase[pre];
		arr->pBase[pre] = arr->pBase[post];
		arr->pBase[post] = temp;
		post--;
		pre++;
	}
	return true;
}

void sort(struct aL * arr) {
	int t, pos;

	for (int i = 0; i < arr->cnt; i++) {
		t = arr->pBase[i];
		pos = i;
		for (int k = i; k < arr->cnt; k++){
			if (arr->pBase[k] < t) {
				pos = k;
				t = arr->pBase[k];
			}
		}
		arr->pBase[pos] = arr->pBase[i];
		arr->pBase[i] = t;
	}

	return true;
}

int maxA(struct aL arr) {
	int r = arr.pBase[0];

	for (int i = 1; i < arr.cnt; i++) {
		if (arr.pBase[i] > r)
			r = arr.pBase[i];
	}

	return r;
}

int minA(struct aL arr) {
	int r = arr.pBase[0];

	for (int i = 1; i < arr.cnt; i++) {
		if (arr.pBase[i] < r)
			r = arr.pBase[i];
	}

	return r;
}