15 #define NUM_INC_STORE 100000
32 if (0 < nsp->
size && NULL == nsp->
num)
54 assert(NULL != filename);
55 assert(0 < strlen(filename));
64 if (NULL == (nsp = calloc(1,
sizeof(*nsp))))
72 if (strcmp(filename,
"-"))
74 if (NULL == (fp = fopen(filename,
"r")))
86 while(fread(&n,
sizeof(n), 1, fp))
98 if (NULL == (nsp->
num = realloc(nsp->
num, new_size *
sizeof(nsp->
num[0]))))
103 nsp->
size = new_size;
116 if (strcmp(filename,
"-"))
131 if (nsp->
num != NULL)
145 if (NULL != nsp->
num)
153 static int cmp_num(
const void *a,
const void* b)
174 for(
int i = 0; i < nsp->
used; i++)
176 if (nsp->
num[i] != prev)
180 printf(
"%d\n", prev);
Data structure to store numbers.
void nums_free(NUMS *nsp)
Free NUMS data structure.
static int is_valid(const NUMS *nsp)
Checks if data structure is ok.
static int cmp_num(const void *a, const void *b)
Compare two ints.
Appfs Exercise 1: Number storage and sorting subroutines.
NUMS * nums_read(const char *filename)
Read numbers into data structure.
int * num
storage for numbers
size_t used
next free num[]
void nums_print(const NUMS *nsp)
Sort and print all numbers in the NUMS structure.
size_t nums_used(const NUMS *nsp)
Return count of numbers in NUMS structure.
size_t size
max number of entries off num