#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arraylist.h"
struct Array_List_Type* Create_Array_List(int Maximum_Element_Count) {
if (Maximum_Element_Count < 0) {
printf("Error! The Maximum number of elements must be greater then zero. Create_Array_List()\n");
return NULL;
}
struct Array_List_Type* Pointer_Return = NULL;
(struct Array_List_Type*)Pointer_Return = (struct Array_List_Type*)malloc(1 * sizeof(struct Array_List_Type));
if ((struct Array_List_Type*)Pointer_Return == NULL) {
printf("Error! Dynamic memory allocation failed(1). :( Create_Array_List()\n");
return NULL;
}
Pointer_Return->Maximum_Element_Count = Maximum_Element_Count;
Pointer_Return->Current_Element_Count = 0;
(struct Array_List_Node_Type*)Pointer_Return->Pointer_Element = NULL;
(struct Array_List_Node_Type*)Pointer_Return->Pointer_Element = (struct Array_List_Node_Type*)malloc(Maximum_Element_Count * sizeof(struct Array_List_Node_Type));
if ((struct Array_List_Node_Type*)Pointer_Return->Pointer_Element == NULL) {
printf("Error! Dynamic memory allocation failed(2). :( Create_Array_List()\n");
free((struct Array_List_Type*)Pointer_Return);
return NULL;
}
(void*)memset((struct Array_List_Node_Type*)Pointer_Return->Pointer_Element, 0, Maximum_Element_Count * sizeof(struct Array_List_Node_Type));
return (struct Array_List_Type*)Pointer_Return;
}
int Add_Array_List_Element(struct Array_List_Type* Pointer_List, int position, struct Array_List_Node_Type element) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Add_Array_List_Element()\n");
return FALSE;
}
else if (Is_Array_List_Full((struct Array_List_Type*)Pointer_List)) {
printf("Error! Exceeded capacity of a array list. -[%d]/[%d]\n", position, Pointer_List->Maximum_Element_Count);
return FALSE;
}
else if (!(position >= 0 && position <= Pointer_List->Current_Element_Count)) {
printf("Error! Position index-[%d] out of range. Add_Array_List_Element()\n", position);
return FALSE;
}
int index = 0;
for (index = Pointer_List->Current_Element_Count - 1; index >= position; index--) {
*(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index + 1) = *(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index);
}
*(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + position) = *(struct Array_List_Node_Type*)&element;
Pointer_List->Current_Element_Count++;
return TRUE;
}
int Remove_Array_List_Element(struct Array_List_Type* Pointer_List, int position) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Remove_Array_List()\n");
return FALSE;
}
else if (!(position >= 0 && position < Pointer_List->Current_Element_Count)) {
printf("Error! Position index-[%d] out of range. Remove_Array_List()\n");
return FALSE;
}
int index = 0;
for (index = position; index < Pointer_List->Current_Element_Count - 1; index++) {
*(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index) = *(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index + 1);
}
Pointer_List->Current_Element_Count--;
return TRUE;
}
struct Array_List_Node_Type* Get_Array_List_Element(struct Array_List_Type* Pointer_List, int position) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Get_Array_List_Element()\n");
return FALSE;
}
else if (!(position >= 0 && position < Get_Array_List_Length((struct Array_List_Type*)Pointer_List))) {
printf("Error! Position index-[%d] out of range. Get_Array_List_Element()\n");
return FALSE;
}
struct Array_List_Node_Type* Pointer_Return = NULL;
(struct Array_List_Node_Type*)Pointer_Return = (struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + position);
return (struct Array_List_Node_Type*)Pointer_Return;
}
void Display_Array_List(struct Array_List_Type* Pointer_List) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Display_Array_List()\n");
return;
}
printf("Number of Maximum element : %d\n", Pointer_List->Maximum_Element_Count);
printf("Number of Current element : %d\n", Pointer_List->Current_Element_Count);
int index = 0;
for (index = 0; index < Pointer_List->Current_Element_Count; index++) {
printf("[%d],%d\n", index, Get_Array_List_Element((struct Array_List_Type*)Pointer_List, index)->data);
}
for (index = Pointer_List->Current_Element_Count; index < Pointer_List->Maximum_Element_Count; index++) {
printf("[%d], Empty\n", index);
}
return;
}
int Is_Array_List_Full(struct Array_List_Type* Pointer_List) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Is_Array_List_Full()\n");
return FALSE;
}
else if (Pointer_List->Current_Element_Count != Pointer_List->Maximum_Element_Count) {
return FALSE;
}
else return TRUE;
}
int Get_Array_List_Length(struct Array_List_Type* Pointer_List) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Get_Array_List_Length()\n");
return 0;
}
else return Pointer_List->Current_Element_Count;
}
void Delete_Array_List(struct Array_List_Type* Pointer_List) {
if ((struct Array_List_Type*)Pointer_List == NULL) {
printf("Error! The array list is null. Delete_Array_List()\n");
return;
}
free((struct Array_List_Node_Type*)Pointer_List->Pointer_Element);
free((struct Array_List_Type*)Pointer_List);
return;
}