Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#ifndef ARRAYLIST_H_ #define ARRAYLIST_H_ #define DEFAULT_CAPACITY 5 #define TRU

ID: 3549157 • Letter: #

Question

                    
                

                                     #ifndef ARRAYLIST_H_
                    #define ARRAYLIST_H_
                    
                    
                    
                    #define DEFAULT_CAPACITY 5
                    #define TRUE 1
                    #define FALSE 0
                    
                    
                    
                    typedef int Boolean;
                    typedef int ArrayListItemT;
                    
                    
                    
                    /*
                    * Pay very close attention to this definition.
                    * Notice that it does not define the struct,
                    * rather it defines a pointer to a struct.
                    * You must define the actual struct in your ArrayList.c file.
                    * It MUST have the name "struct ArrayList" to match this typedef.
                    * The definition of the struct is now "private".
                    */
                    
                    typedef struct ArrayList *ArrayListP;
                    
                    
                    
                    /*
                    * Returns a pointer to a new empty ArrayList
                    * Memory is allocated dynamically
                    * If malloc fails returns a NULL pointer
                    * Uses DEFAULT_CAPACITY
                    * if malloc if full double the size of the arrylist so we will start with 5 and if it got full make it 10 and put the item from the index 0-4 in the new                     one.
                    */
                    
                    ArrayListP newArrayList();
                    
                    
                    
                    /*
                    * Returns True if the requested item is in the ArrayList, else False
                    */
                    
                    Boolean containsArrayList(ArrayListP, ArrayListItemT);
                    
                    
                    /*
                    * Returns the index of the first occurrence of the item in the ArrayList
                    * Returns -1 if not found
                    */
                    
                    int indexOfArrayList(ArrayListP, ArrayListItemT);
                    
                    
                    /*
                    * Frees all memory allocated for the ArrayList
                    */
                    
                    ArrayListP freeArrayList(ArrayListP);
                    
                    
                    
                    #endif /* ARRAYLIST_H_ */


Explanation / Answer

#ifndef ALGORITHM_ARRAYLIST_H #define ALGORITHM_ARRAYLIST_H  #ifdef __cplusplus extern "C" { #endif  /**  * A value to be stored in an @ref ArrayList.  */  typedef void *ArrayListValue;  /**  * An ArrayList structure.  New ArrayLists can be created using the   * arraylist_new function.  *  * @see arraylist_new   */  typedef struct _ArrayList ArrayList;  /**  * Definition of an @ref ArrayList.  */   struct _ArrayList {          /** Entries in the array */                  ArrayListValue *data;          /** Length of the array */                  int length;          /** Private data and should not be accessed */          int _alloced; };  /**  * Compare two values in an arraylist to determine if they are equal.  *  * @return Non-zero if the values are not equal, zero if they are equal.  */  typedef int (*ArrayListEqualFunc)(ArrayListValue value1, ArrayListValue value2);  /**  * Compare two values in an arraylist.  Used by @ref arraylist_sort  * when sorting values.  *  * @param value1              The first value.  * @param value2              The second value.  * @return                    A negative number if value1 should be sorted  *                            before value2, a positive number if value2 should  *                            be sorted before value1, zero if the two values  *                            are equal.  */  typedef int (*ArrayListCompareFunc)(ArrayListValue value1,                                     ArrayListValue value2);  /**  * Allocate a new ArrayList for use.  *  * @param length         Hint to the initialise function as to the amount  *                       of memory to allocate initially to the ArrayList.  * @return               A new arraylist, or NULL if it was not possible  *                       to allocate the memory.  * @see arraylist_free  */  ArrayList *arraylist_new(int length);  /**  * Destroy an ArrayList and free back the memory it uses.  *  * @param arraylist      The ArrayList to free.  */  void arraylist_free(ArrayList *arraylist);  /**  * Append a value to the end of an ArrayList.  *  * @param arraylist      The ArrayList.  * @param data           The value to append.  * @return               Non-zero if the request was successful, zero  *                       if it was not possible to allocate more memory  *                       for the new entry.  */  int arraylist_append(ArrayList *arraylist, ArrayListValue data);  /**   * Prepend a value to the beginning of an ArrayList.  *  * @param arraylist      The ArrayList.  * @param data           The value to prepend.  * @return               Non-zero if the request was successful, zero  *                       if it was not possible to allocate more memory  *                       for the new entry.  */  int arraylist_prepend(ArrayList *arraylist, ArrayListValue data);  /**  * Remove the entry at the specified location in an ArrayList.  *  * @param arraylist      The ArrayList.  * @param index          The index of the entry to remove.  */  void arraylist_remove(ArrayList *arraylist, int index);  /**  * Remove a range of entries at the specified location in an ArrayList.  *  * @param arraylist      The ArrayList.  * @param index          The index of the start of the range to remove.  * @param length         The length of the range to remove.  */  void arraylist_remove_range(ArrayList *arraylist, int index, int length);  /**  * Insert a value at the specified index in an ArrayList.  * The index where the new value can be inserted is limited by the   * size of the ArrayList.  *  * @param arraylist      The ArrayList.  * @param index          The index at which to insert the value.  * @param data           The value.  * @return               Returns zero if unsuccessful, else non-zero   *                       if successful (due to an invalid index or   *                       if it was impossible to allocate more memory).  */  int arraylist_insert(ArrayList *arraylist, int index, ArrayListValue data);  /**  * Find the index of a particular value in an ArrayList.  *  * @param arraylist      The ArrayList to search.  * @param callback       Callback function to be invoked to compare  *                       values in the list with the value to be  *                       searched for.  * @param data           The value to search for.  * @return               The index of the value if found, or -1 if not found.  */  int arraylist_index_of(ArrayList *arraylist,                         ArrayListEqualFunc callback,                         ArrayListValue data);  /**   * Remove all entries from an ArrayList.  *  * @param arraylist      The ArrayList.  */  void arraylist_clear(ArrayList *arraylist);  /**   * Sort the values in an ArrayList.  *  * @param arraylist      The ArrayList.  * @param compare_func   Function used to compare values in sorting.  */  void arraylist_sort(ArrayList *arraylist, ArrayListCompareFunc compare_func);  #ifdef __cplusplus } #endif  #endif /* #ifndef ALGORITHM_ARRAYLIST_H */