Question: x#include #include #include #define uint unsigned int // pointer to array for storage int *a = NULL; // number of items i the array int

x#include #include #include

#define uint unsigned int

// pointer to array for storage int *a = NULL;

// number of items i the array int n = 0;

// maximum number of items we can store right now int max_items = 0;

// malloc memory, initialize number of items to 0 int initialize() { n = 0; max_items = 10; a = (int *) malloc(sizeof(int) * max_items); if (a == NULL) { max_items = 0; return 0; } return 1; } /* h10 should return the current number of items stored in the array */ uint size() { } /* h10 should return the item at index i */ int get(uint i) { } /* h10 should set the item at index i to x. return 1 if successfull, 0 if failed. */ int set(uint i, int x) { } /* doubles the amount of memory available in the array, returns 1 on success and 0 on failure */ int resize() { max_items *= 2; int * new_a = realloc(a, sizeof(int) * max_items); if (new_a == NULL) { printf("Error on realloc in resize. "); max_items /= 2; return 0; } else { a = new_a; return 1; } } /* add a new item at index i, shifting the ones after that all +1. return 1 on success, 0 on failure. */ int add(uint i, int x) { if (i > n) return 0; // should not be trying to add that far out.

if (max_items < n + 1) { if ( !resize()) return 0; }

for(int k=n; k > i; k--) a[k]=a[k-1]; a[i] = x; n++; return 1; }

/* h10 remove and return the item at index i, shifting the ones after that all -1. if invalid operation, return 0. */ int delete(uint i) {

}

/* free the memory in the array */ void free_mem() { if (a != NULL) free(a); a = NULL; max_items = 0; }

/* print the items in the array */ void print() { printf("------------------------ "); for(int i=0; i < n; i++) printf("%d, ", a[i]); printf(" "); } /* h10 should put x at the end of the array, by calling add with the appropriate parameters. return 1 on success, 0 on failure. */ int push(int x) { } /* h10 should remove from the end of the array and return that value, by calling delete with the appropriate parameters if invalid operation, return 0. */ int pop() {

} int main(int argc, char *argv[]) { // if the program is run in "debug" mode, then print // the array after each operation. // h10 - for you to implement this, and it may help you // with debugging your code. int debug = 0; if (argc > 1 && strcmp(argv[1], "debug") == 0) debug = 1;

if (!initialize()) { printf("Error initializing arraystack. "); exit(0); } // h10 - // the main loop of the program will read in // "commands" to add/delete/etc. to the array

// read lines from input until EOF

// a line of input will be as follows // command [argument1] [argument2] // // command will be either set, add, delete, push, or pop, with // the arguments being the ones the set/add/delete // functions expect // // set i x - call set(i, x) // add i x - call add(i, x) // delete i - call delete(i) // push x - call push(x) // pop - call pop() // // on any kind of input that does not follow this formatting, // your code should just ignore that line and go on to the next one. // // your should code should be case sensitive (so Set is not a valid operation). // after done reading through input (until getline returned 0) // last of all, print the contents of the list - call the print function

printf();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!