Skip to content

This is a simple implementation of an ArrayList in C using a struct, a pointer to the struct and a pointer to the array. The array is static and the size of the array is defined when the ArrayList is created.

License

Notifications You must be signed in to change notification settings

theonlyswissarmy/ArrayListC-but-better

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArrayList C

This is a simple implementation of an ArrayList in C using a struct, a pointer to the struct and a pointer to the array. The array is static and the size of the array is defined when the ArrayList is created.

This is cool. This is fine. This isn't really what an arraylist is though. Just as a little exercise I'm going to try and make this template into a proper arraylist

Functions

  • ArrayList* ArrayCreate(int n): Creates an ArrayList with a size of n.

  • void ArrayAppendFirst(ArrayList* list, int value): Add an element to the beginning of the list.

  • void ArrayAppendLast(ArrayList* list, int value): Appends a value to the end of the array.

  • void ArrayAppend(ArrayList* list, int value, int index): Appends a value to the array at the specified index.

  • void ArrayDelete(ArrayList* list, int index): Deletes the element at the given index.

  • void ArrayDeleteFirst(ArrayList* list): Deletes the first element of the array.

  • void ArrayDeleteLast(ArrayList* list): Deletes the last element of the array.

  • void ArrayPrint(ArrayList* list): Prints the array

  • void ArrayFree(ArrayList* list): Frees the memory

  • char* ArrayToString(ArrayList* list): Returns a string representation of the array

Usage

// Create an array list with maximum 10 value capacity.
ArrayList* list = ArrayCreate(10);

// Append a value to the last of the array list.
ArrayAppendLast(list, 10);
ArrayAppendLast(list, 20);
ArrayAppendLast(list, 30);
ArrayAppendLast(list, 40);

// Append a value to the first of the array list.
ArrayAppendFirst(list, 5);

// Append a value to the array list at the specified index.
ArrayAppend(list, 25, 2);

// Print the array list.
ArrayPrint(list);

// Delete a value from the array list at the specified index.
ArrayDelete(list, 3);

// Delete a value from the first of the array list.
ArrayDeleteFirst(list);

// Delete a value from the last of the array list.
ArrayDeleteLast(list);

// Print the array list.
ArrayPrint(list);

And the output is:

5 10 25 20 30 40
10 25 30

© Copyright Max Base, 2022

About

This is a simple implementation of an ArrayList in C using a struct, a pointer to the struct and a pointer to the array. The array is static and the size of the array is defined when the ArrayList is created.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 100.0%