Skip to content

mpassarella/arraylist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

arraylist

ArrayList functions for C developers
This minimal projects is aimed to give a simple procedural interface to manage dynamically allocated arraylists in C

Internal structure

Type arrlist

Properties

  • int size: Number of items currently stored in the arraylist
  • int ubound: Current size of allocated memory spaces to accomodate future items
  • void items: Array of pointers to stored items

Defined constants

  • INIT_SIZE: Number of initially allocated items when the array is instatiated
  • GENERIC_ERROR: Generic error return code
  • GENERIC_OK: Generic ok return code

Functions

  • arrlist* arrnew()
    • Creates a new, blank, arraylist and returns its pointer
  • arrlist* arrnew_size(int initial_size)
    • Creates a new, blank, arraylist and returns its pointer with initial size of initial_size
  • int additem(arrlist *p, void *pitem)
    • Adds a new pitem item to the p arrlist and returns the size after the insertion. Returns -1 if something went wrong
  • int delitems(arrlist *p, int position)
    • Deletes the items from the p arrlist at the position position until the end. Returns the arraylist's size after the deletion otherwise -1 if something went wrong
  • int delitem(arrlist *p, int position)
    • Deletes the item from the p arrlist at the position position and compacts the arraylist. Returns the arraylist's size after the deletion otherwise -1 if something went wrong
  • int splice(arrlist *s, arrlist *p, int position)
    • Splices the arraylist in two arraylists at the position position. s contains the pointer to the arraylist from item 0 to position position -1, p contains the arraylist from item at position position to the end. Returns -1 if something went wrong
  • void clear(arrlist *p)
    • Destroys the p instance and recreates it returning its new pointer.

How to use it

Instantiation

    arrlist *a = arrnew();
    printf("Size %d\r\n", a->size);

Instantiation with a specified size

    arrlist *a = arrnew_size(10);
    printf("Size %d\r\n", a->size);

Add and item to the arraylist

    arrlist *a = arrnew();
    additem(a, "Item number 1");

Remove all items from a position

    arrlist *a = arrnew();
    additem(a, "Item number 1"); 
    additem(a, "Item number 2"); 
    additem(a, "Item number 3"); 
    additem(a, "Item number 4"); 
    [...]
    delitems(a, 9);

Remove an item from the arraylist

    arrlist *a = arrnew();
    additem(a, "Item number 1"); 
    additem(a, "Item number 2"); 
    additem(a, "Item number 3"); 
    additem(a, "Item number 4"); 
    [...]
    delitem(a, 9);

Splice an arraylist into two at a specified position

    arrlist *a = arrnew();
    arrlist *b = arrnew();
    additem(a, "Item number 1"); 
    additem(a, "Item number 2"); 
    additem(a, "Item number 3"); 
    additem(a, "Item number 4"); 
    [...]
    splice(a, b, 5);

Clear the arraylist

    arrlist *a = arrnew();
    additem(a, "Item number 1"); 
    additem(a, "Item number 2"); 
    additem(a, "Item number 3"); 
    additem(a, "Item number 4"); 
    [...]
    clear(a);
    printf("Size %d\r\n", a->size);

About

ArrayList functions for C developers

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published