#include #include #include "List.h" #include #define MAX_LEN 250 void shuffle(ListRef L, ListRef N){ int currindex = 1; int loopcount = 0; moveFirst(N); moveFirst(L); while(currindex <= getLength(N)){ printf("loop count = %d\n", loopcount++); int pos = getCurrent(N); int curr = getCurrent(L); deleteCurrent(L); int i = 1; moveFirst(L); while( i < pos){ moveNext(L); i++; } insertBeforeCurrent(L, curr); currindex++; moveNext(L); i = 1; moveFirst(L); while(i < currindex){ moveNext(L); i++; } } } int main(int argc, char * argv[]){ FILE* in = fopen(argv[1], "r"); FILE* out = fopen(argv[2], "w"); char line[MAX_LEN]; char* token; /* check command line for correct number of arguments */ if( argc != 3 ){ printf("Usage: %s infile outfile\n", argv[0]); exit(1); } if( in==NULL ){ printf("Unable to open file %s for reading\n", argv[1]); exit(1); } if( out==NULL ){ printf("Unable to open file %s for writing\n", argv[2]); exit(1); } /* read each line of input file, then count and print tokens */ while( fgets(line, MAX_LEN, in) != NULL) { ListRef N = newList(); token = strtok(line, " \n"); printf("starting strtok\n"); while( token!=NULL ){ insertAfterLast(N, atol(token)); token = strtok(NULL, " \n"); } printf("after reading\n"); ListRef L = newList(); printf("starting a new list\n"); int i; for(i = 1; i <= getLength(N); i++){ insertAfterLast(L, i); } printf("before copylist\n"); ListRef O = copyList(L); printf("after copylist\n"); int order = 0; printf("starting to shuffle\n"); for(;;){ shuffle(L,N); order++; if(equals(O,L)){ break; } } shuffle(L,N); printList(out, L); } /* close files */ fclose(in); fclose(out); return(0); }