I made Some Data Structures in C, With Some Function Pointers.
#include <stdio.h>
#include <stdlib.h>
// Defining Sort Type of An Array To Efficient Uses.
#define __Sort_Type_Unsorted__ 0
#define __Sort_Type_Ascending__ 1
#define __Sort_Type_Descending__ 2
// Defining Some Error Massages
#define __D_I_A_S__ERROR_01__ "\nDynamic Integer Array:-| ERROR 01 |-<%s> Operation Can't Be Performed, Because Array is Deleted!!!", __FUNCTION__
#define __D_I_A_S__ERROR_02__ "\nDynamic Integer Array:-| ERROR 02 |-<%s> Operation Can't Be Performed, Because Array Index Out of Range!!!", __FUNCTION__
typedef struct Dynamic_Integer_Array_Structure
{
int __Size__; //#### Size of Array
int *__Base_Address__; //#### Address of 0th Index of The Array
unsigned short int __Sort_Type__; //#### Type of Array. Possible Values: 0. Unsorted, 1. Sorted in Ascending Order, 2. Sorted In Descending Order.
void (*repr)(struct Dynamic_Integer_Array_Structure *structure);
void (*delete)(struct Dynamic_Integer_Array_Structure *structure);
int (*pop)(struct Dynamic_Integer_Array_Structure *structure, int index);
int (*search)(struct Dynamic_Integer_Array_Structure *structure, int element);
void (*append)(struct Dynamic_Integer_Array_Structure *structure, int element);
void (*remove)(struct Dynamic_Integer_Array_Structure *structure, int element);
void (*insert)(struct Dynamic_Integer_Array_Structure *structure, int index, int element);
} int_Array;
void __D_I_A_S_Insert__(struct Dynamic_Integer_Array_Structure *arr, int index, int element)
{
if ((arr->__Sort_Type__) == __Sort_Type_Ascending__)
{
if (((arr->__Base_Address__)[index - 1] > element) || (element < (arr->__Base_Address__)[index]))
{
arr->__Sort_Type__ = __Sort_Type_Unsorted__;
}
}
else if ((arr->__Sort_Type__) == __Sort_Type_Descending__)
{
if (((arr->__Base_Address__)[index - 1] < element) || (element > (arr->__Base_Address__)[index]))
{
arr->__Sort_Type__ = __Sort_Type_Unsorted__;
}
}
if ((arr->__Size__) == -1)
{
printf(__D_I_A_S__ERROR_01__);
}
else if ((arr->__Size__) <= index)
{
printf(__D_I_A_S__ERROR_02__);
}
else
{
int temp_Array[(arr->__Size__)];
for (int i = 0; i < (arr->__Size__); i++)
{
temp_Array[i] = (arr->__Base_Address__)[i];
}
(arr->__Size__)++;
(arr->__Base_Address__) = (int *)realloc((arr->__Base_Address__), ((arr->__Size__) * sizeof(int)));
for (int i = 0; i < index; i++)
{
(arr->__Base_Address__)[i] = temp_Array[i];
}
(arr->__Base_Address__)[index] = element;
for (int i = (index + 1); i < (arr->__Size__); i++)
{
(arr->__Base_Address__)[i] = temp_Array[i - 1];
}
}
}
void __D_I_A_S_Remove__(struct Dynamic_Integer_Array_Structure *arr, int element)
{
if ((arr->__Size__) == -1)
{
printf(__D_I_A_S__ERROR_01__);
}
else
{
int first_Occurrence_Index = arr->search(arr, element);
int temp_Array[(arr->__Size__) - 1];
for (int i = 0; i < first_Occurrence_Index; i++)
{
temp_Array[i] = (arr->__Base_Address__)[i];
}
for (int i = (first_Occurrence_Index) + 1; i < (arr->__Size__); i++)
{
temp_Array[i - 1] = (arr->__Base_Address__)[i];
}
(arr->__Base_Address__) = (int *)realloc((arr->__Base_Address__), ((arr->__Size__) - 1) * sizeof(int));
(arr->__Size__)--;
for (int i = 0; i < (arr->__Size__); i++)
{
(arr->__Base_Address__)[i] = temp_Array[i];
}
}
}
void __D_I_A_S_Append__(struct Dynamic_Integer_Array_Structure *arr, int element)
{
if ((arr->__Sort_Type__) == __Sort_Type_Ascending__)
{
if ((arr->__Base_Address__)[(arr->__Size__)] > element)
{
arr->__Sort_Type__ = __Sort_Type_Unsorted__;
}
}
else if ((arr->__Sort_Type__) == __Sort_Type_Descending__)
{
if ((arr->__Base_Address__)[(arr->__Size__)] < element)
{
arr->__Sort_Type__ = __Sort_Type_Unsorted__;
}
}
if ((arr->__Size__) == -1)
{
printf(__D_I_A_S__ERROR_01__);
}
else if ((arr->__Size__) == 0)
{
(arr->__Size__) = 1;
(arr->__Base_Address__)[0] = element;
}
else
{
int temp_Array[(arr->__Size__)];
for (int i = 0; i < (arr->__Size__); i++)
{
temp_Array[i] = (arr->__Base_Address__)[i];
}
(arr->__Size__)++;
(arr->__Base_Address__) = (int *)realloc((arr->__Base_Address__), ((arr->__Size__) * sizeof(int)));
for (int i = 0; i < (arr->__Size__) - 1; i++)
{
(arr->__Base_Address__)[i] = temp_Array[i];
}
(arr->__Base_Address__)[(arr->__Size__) - 1] = element;
}
}
int __D_I_A_S_Search__(struct Dynamic_Integer_Array_Structure *arr, int element)
{
if (arr->__Size__ == -1)
{
printf(__D_I_A_S__ERROR_01__);
}
else
{
if ((arr->__Sort_Type__) == __Sort_Type_Unsorted__)
{
for (int i = 0; i < (arr->__Size__); i++)
{
if ((arr->__Base_Address__)[i] == element)
{
return i;
}
}
}
else if ((arr->__Sort_Type__) == __Sort_Type_Ascending__)
{
int middle_Index, Left_Most_Searching_Index, Right_Most_Searching_Index;
Left_Most_Searching_Index = 0;
Right_Most_Searching_Index = (arr->__Size__) - 1;
while (Left_Most_Searching_Index <= Right_Most_Searching_Index)
{
middle_Index = (Left_Most_Searching_Index + Right_Most_Searching_Index) / 2;
if ((arr->__Base_Address__)[middle_Index] == element)
{
return middle_Index;
}
else if ((arr->__Base_Address__)[middle_Index] < element)
{
Left_Most_Searching_Index = middle_Index + 1;
}
else
{
Right_Most_Searching_Index = middle_Index - 1;
}
}
}
else if ((arr->__Sort_Type__) == __Sort_Type_Descending__)
{
int middle_Index, Left_Most_Searching_Index, Right_Most_Searching_Index;
Left_Most_Searching_Index = 0;
Right_Most_Searching_Index = (arr->__Size__) - 1;
while (Left_Most_Searching_Index <= Right_Most_Searching_Index)
{
middle_Index = (Left_Most_Searching_Index + Right_Most_Searching_Index) / 2;
if ((arr->__Base_Address__)[middle_Index] == element)
{
return middle_Index;
}
else if ((arr->__Base_Address__)[middle_Index] > element)
{
Left_Most_Searching_Index = middle_Index + 1;
}
else
{
Right_Most_Searching_Index = middle_Index - 1;
}
}
}
}
return -1;
}
int __D_I_A_S_Pop__(struct Dynamic_Integer_Array_Structure *arr, int index)
{
if ((arr->__Size__) == -1)
{
printf(__D_I_A_S__ERROR_01__);
return -1;
}
else if ((arr->__Size__) <= index)
{
printf(__D_I_A_S__ERROR_02__);
}
else
{
int popped_Element = (arr->__Base_Address__)[index];
int temp_Array[(arr->__Size__) - 1];
for (int i = 0; i < index; i++)
{
temp_Array[i] = (arr->__Base_Address__)[i];
}
for (int i = index + 1; i < (arr->__Size__); i++)
{
temp_Array[i - 1] = (arr->__Base_Address__)[i];
}
(arr->__Base_Address__) = (int *)realloc((arr->__Base_Address__), ((arr->__Size__) - 1) * sizeof(int));
(arr->__Size__)--;
for (int i = 0; i < (arr->__Size__); i++)
{
(arr->__Base_Address__)[i] = temp_Array[i];
}
return popped_Element;
}
}
void __D_I_A_S_Represent__(struct Dynamic_Integer_Array_Structure *arr)
{
if ((arr->__Size__) == -1)
{
printf(__D_I_A_S__ERROR_01__);
}
else if (arr->__Size__ == 0)
{
printf("\nDynamic Integer Array: {}.");
}
else
{
printf("\n{%d", arr->__Base_Address__[0]);
for (int i = 1; i < (arr->__Size__); i++)
{
printf(", %d", (arr->__Base_Address__)[i]);
}
printf("}.");
}
}
void __D_I_A_S_Delete__(struct Dynamic_Integer_Array_Structure *arr)
{
if (arr->__Size__ == -1)
{
printf(__D_I_A_S__ERROR_01__);
printf(" Already!!!");
}
else
{
(arr->__Size__) = -1;
free(arr->__Base_Address__);
}
}
void __D_I_A_S_Initialize__(int_Array *new_Array)
{
new_Array->__Size__ = 0;
new_Array->__Sort_Type__ = __Sort_Type_Unsorted__;
(new_Array->__Base_Address__) = (int *)malloc(sizeof(int));
// Functions Declaration...
(new_Array->pop) = __D_I_A_S_Pop__;
(new_Array->repr) = __D_I_A_S_Represent__;
(new_Array->delete) = __D_I_A_S_Delete__;
(new_Array->search) = __D_I_A_S_Search__;
(new_Array->append) = __D_I_A_S_Append__;
(new_Array->remove) = __D_I_A_S_Remove__;
(new_Array->insert) = __D_I_A_S_Insert__;
}
#define int_Array_init __D_I_A_S_Initialize__
So I can Make an Instance of The Structure and Reference To Its Methods.
int_Array a;
int_Array_init(&a);
a.append(&a, 6);
a.append(&a, 5);
a.append(&a, 4);
a.append(&a, 3);
a.append(&a, 2);
a.append(&a, 1);
a.repr(&a);
printf("\nAfter Removal");
a.remove(&a, 3);
a.repr(&a);
printf("\nAfter Popping");
a.pop(&a, 40);
a.repr(&a);
printf("\nElement Found At Index %d In The Array.", a.search(&a, 3));
printf("\nSort Type Of The Array: %d", a.__Sort_Type__);
a.repr(&a);
a.delete(&a);
a.repr(&a);
a.append(&a, 1);
a.insert(&a, 1, 1);
a.pop(&a, 1);
a.remove(&a, 1);
a.search(&a, 1);
a.delete(&a);
printf("\nDone!!!");
Output :::
{6, 5, 4, 3, 2, 1}.
After Removal
{6, 5, 4, 2, 1}.
After Popping
Dynamic Integer Array:-| ERROR 02 |-<__D_I_A_S_Pop__> Operation Can't Be Performed, Because Array Index Out of Range!!!
{6, 5, 4, 2, 1}.
Element Found At Index -1 In The Array.
Sort Type Of The Array: 0
{6, 5, 4, 2, 1}.
Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Represent__> Operation Can't Be Performed, Because Array is Deleted!!!Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Append__> Operation Can't Be Performed, Because Array is Deleted!!!
Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Insert__> Operation Can't Be Performed, Because Array is Deleted!!!
Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Pop__> Operation Can't Be Performed, Because Array is Deleted!!!
Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Remove__> Operation Can't Be Performed, Because Array is Deleted!!!
Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Search__> Operation Can't Be Performed, Because Array is Deleted!!!
Dynamic Integer Array:-| ERROR 01 |-<__D_I_A_S_Delete__> Operation Can't Be Performed, Because Array is Deleted!!! Already!!!
Done!!!
Is There Any Way To Avoid Passing Address of Instance every time. Is That Possible That The Below Code Will Produce the Same Output As the Above. something like :::
int_Array a;
int_Array_init(&a);
a.append(6);
a.append(5);
a.append(4);
a.append(3);
a.append(2);
a.append(1);
a.repr();
printf("\nAfter Removal");
a.remove(3);
a.repr();
printf("\nAfter Popping");
a.pop(40);
a.repr();
printf("\nElement Found At Index %d In The Array.", a.search(3));
printf("\nSort Type Of The Array: %d", a.__Sort_Type__);
a.repr();
a.delete();
a.repr();
a.append(1);
a.insert(1, 1);
a.pop(1);
a.remove(1);
a.search(1);
a.delete();
printf("\nDone!!!");
Please Tell The Solution That You Know About To Solve The Problem.