$30
EECS 2031 Page 1 of 2
LAB 5 ─ Pointers to Structures and Dynamic Memory Allocation
1. Specification
The extendable array data structure allows the array capacity to be increased or reduced according to
the current array utilization. We implement a simple extendable array data structure in which elements
are inserted at and removed from only one end of the array (the rear in this exercise). Write a C
program to implement the insertion and deletion operations, extending or shrinking the array as
needed.
2. Implementation
• The program to be submitted is named lab5.c. Use the given template lab5.c and fill in your
code. Submit only file lab5.c.
• You are also given a file named lab5main.c to test your code. Do not submit file lab5main.c.
• The first function to be implemented is insertLast(). See file lab5.c for its specification. The
new element is to be inserted at the rear of the extendable array. When a new element is inserted
into a full array, extend the array by doubling its current capacity C (e.g., if C == 4 then C is increased
to 8). Use function malloc or calloc to allocate memory for an array. Allocate a new array; copy
the content of the old (smaller) array to the new (bigger) array; free the old array.
• The second function to be implemented is removeLast(). See file lab5.c for its specification.
The function removes and returns the last element of the array (i.e., the element that was inserted
last). If the array is empty, the function calls function printErr() to display an error message
and returns -1. After a deletion, if the number of elements in the array falls below C/4 (size < C/4),
shrink the array by half of the current capacity, but the minimum capacity should always be 4 (e.g., if
C == 16 and size < 4 then C is reduced to 8). Again, use function malloc or calloc to allocate
memory for a new array as explained above.
• You may define your own variables inside functions insertLast()and removeLast().
• In file lab5.c you are given several utility functions such as initArr(),printErr()and
printArray(). DO NOT modify these functions.
• Do not modify the function and structure definitions in file lab5.c.
3. Sample Inputs/Outputs
See file lab5out.txt for the output from running programs lab5.c and lab5main.c.
EECS 2031 Page 2 of 2
Common Notes
• Complete the header in file lab5.c with your student and contact information.
• Assume that all inputs are valid. No error checking is required on inputs.
• To compile both files lab5.c and lab5main.c , use the following command:
cc lab5.c lab5main.c