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

Write a C function: size t* addSize tElement(size t* list, size t* size, size t*

ID: 3752231 • Letter: W

Question

Write a C function: size t* addSize tElement(size t* list, size t* size, size t* capacity, size t toAdd) => Adds toAdd to the given list at the end of the list and if necessary: creates a new list of a larger size, copying the old list into the new one, freeing the old list, placing the new element and then returning the new list. The original list is destroyed (or should be considered destroyed), the caller must use the returned list instead.

This function will change *size and may change *capacity. When necessary, you should expand the list by (2*capacity)+1.

Explanation / Answer

size_t* addSize tElement(size_t* list, size_t* size, size_t* capacity, size_t toAdd)

{

    // if the list is full

    if( *size == *capacity )

    {

        // create a new list of size 2 * capacity + 1

        size_t *new_list = (size_t *)malloc( ( 2 * ( *capacity ) + 1 ) * sizeof( size_t ) );

       

        int i;

       

        // copy the contents of list to new_list

        for( i = 0 ; i < *size ; i++ )

            new_list[i] = list[i];

       

        // make new_list as the list

        list = new_list;

    }

   

    // add toAdd to list

    list[ (*size)++ ] = toAdd;

  return list;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote