#include #include #include #include // Data for the string cache #define SCAP 500 #define SLEN 15 char strings[SCAP][SLEN]; size_t beginning = 0, end = 0; char full = 0; /* * If the string holder structure is full, this will delete the oldest */ void add_string(const char* source){ strncpy(strings[end], source, SLEN); if(full){ end++; if(end >= SCAP) end = 0; beginning = end; } else { end++; if(end >= SCAP) end = 0; if(end == beginning) full = 1; } } int stringcount(){ if(full) return SCAP; if(beginning == end) return 0; if(end > beginning) return end - beginning; return SCAP - (1 + beginning - end); } // Returns true if we had a string to remove // false otherwise char remove_string(char* destination){ if(!full && beginning == end) return 0; if(full) full = 0; strncpy(destination, strings[beginning], SLEN); beginning++; if(beginning >= SCAP) beginning = 0; return 1; } int main(){ char stringplace[15]; add_string("gorilla"); add_string("moose"); add_string("gerbil"); add_string("antelope"); add_string("fish"); for(int i = 0; i < 5; i++){ remove_string(stringplace); printf("Removed: %s\n", stringplace); } printf("Finished intermediate removal\n"); add_string("grapefruit"); add_string("sturgeon"); add_string("lobster"); add_string("dugong"); add_string("kitchen"); add_string("snake"); add_string("warroom"); for(; stringcount();){ remove_string(stringplace); printf("Removed: %s\n", stringplace); } return 0; }