/* This program uses code from "Algorithms in C, Third Edition," * by Robert Sedgewick, Addison-Wesley, 1998. */ #include #include #include "list_interface.h" main(int argc, char ** argv) { list the_list = newList(); if (argc != 3) { printf("You need to provide two command line arguments: N and M."); return -1; } int N = atoi(argv[1]); int M = atoi(argv[2]); int i; link new_entry = newLink(1); setFirst(the_list, new_entry); link previous = new_entry; for (i = 2; i <= N; i++) { new_entry = newLink(i); insertLink(the_list, previous, new_entry); previous = new_entry; } /* make the list circular */ link first_entry = listFirst(the_list); setNext(previous, first_entry); link current_link = previous; while(current_link != linkNext(current_link)) { for (i = 1; i < M; i++) { current_link = linkNext(current_link); } link deleted = deleteNext(the_list, current_link); free(deleted); } first_entry = listFirst(the_list); printf("The chosen value is %d.\n", linkItem(first_entry)); /* Before we destroy the list, we must make the first (and only remaining) * link point to NULL, otherwise destroyList will not work. */ setNext(first_entry, NULL); destroyList(the_list); return 0; }