Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * * dataStructure.c * * Provides a bound - checked data structure made of an array of integers * in which duplicated elements are

/*
* dataStructure.c
*
* Provides a bound-checked data structure made of an array of integers
* in which duplicated elements are allowed.
*
* The interface of this data structure includes several utility
* functions that operate on this array of integers as well as
* file i/o functions: saving into and loading from json files.
*
* Do not change the dataStructure.h file.
*
* Author: RV + AL
* Modified Date: July 2024
*/
#include // for malloc() and for rand()
#include // for file i/o calls
#include // for time()
#include // for strlen()
#include "dataStructure.h"
/*
* Description: Creates a new intArray_t data structure
* with initial array size "size".
* If successful (i.e. memory allocation succeeds),
* returns a pointer to a newly-allocated intArray_t.
* If unsuccessful, returns a NULL pointer.
* Time Efficiency: O(1)
*/
intArray_t * intArray_create( unsigned int size ){
// Allocate memory on the heap for intArray_t struct
intArray_t * ia = malloc( sizeof( intArray_t ));
if ( ia != NULL ){
// Set its field "size"
ia->size = size;
// Allocate memory on the heap for array
ia->data = malloc( size * sizeof(int));
if ( ia->data == NULL ){
free( ia );
ia = NULL;
}
else
// Set its field "elementCount" to zero -> empty array
ia->elementCount =0;
}
return ia;
}
/*
* Description: Frees all memory allocated for "ia".
* If the pointer "ia" is NULL, it does nothing.
* If the pointer "ia->data" is NULL, it does not
* attempt to free it. Returns INTARR_OK.
* Time Efficiency: O(1)
*/
intArrayResult_t intArray_destroy( intArray_t * ia ){
// If heap memory has been allocated for intArray_t struct ...
if ( ia != NULL ){
// If heap memory has been allocated for array ...
if ( ia->data != NULL ){
//... then free it
free( ia->data );
ia->data = NULL;
}
//... then free it.
free( ia );
ia = NULL;
}
return INTARR_OK;
}
/* Description: Appends "newElement" and returns INTARR_OK.
* If "newElement" cannot be appended, leaves the
* data structure unmodified and returns INTARR_ERROR.
* If "ia" is NULL, returns INTARR_BADPARAM.
* Time efficiency: O(1)
*/
intArrayResult_t intArray_append( intArray_t * ia, int newElement ){
// Function Stub
// This stub is to be removed when you have successfully implemented this function.
printf( "Calling intArray_append(...) with the parameter newElement ->%d.
", newElement );
return INTARR_OK; // You are free to modify this return statement.
}
/* Description: Removes the element at "indexToBeRemoved" in the data structure
* and returns INTARR_OK. If the data structure was initially sorted,
* it does not remain sorted after removing the element at "indexToBeRemoved".
* If "ia" is NULL or "indexToBeRemoved" is out of bounds, returns INTARR_BADPARAM.
* Time efficiency: O(1)
*/
intArrayResult_t intArray_remove( intArray_t * ia, unsigned int indexToBeRemoved ){
// Function Stub
// This stub is to be removed when you have successfully implemented this function.
printf( "Calling intArray_remove(...) with the parameter indexToBeRemoved ->%u.
", indexToBeRemoved );
return INTARR_OK; // You are free to modify this return statement.
}
/* Description: Modifies

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions