int main(){ /*make list and output file*/ ListHndl TheList = newList(); FILE *out = fopen("out.out", "w"); /*test empty in empty case*/ if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); printf("testing insert one number\n"); insertAtFront(TheList,(unsigned long*)25728); printf("%lu\n",(unsigned long)getFirst(TheList)); printf("%lu\n",(unsigned long)getLast(TheList)); /*should have same value*/ printf("testing list with three numbers\n"); insertAtFront(TheList,(unsigned long*)1589458); insertAtBack(TheList,(unsigned long*)35762111234); printf("%lu\n",(unsigned long)getFirst(TheList)); /*test empty in full case*/ if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); /*test moving the current pointer around*/ moveFirst(TheList); moveNext(TheList); printf("%lu\n",(unsigned long)getCurrent(TheList)); moveLast(TheList); movePrev(TheList); printf("%lu\n",(unsigned long)getCurrent(TheList)); /*test printList*/ printList(out, TheList); /*test makeEmpty*/ makeEmpty(TheList); if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); /*test inserting functions*/ insertAtFront(TheList,(unsigned long*)2); insertAtFront(TheList,(unsigned long*)1); insertAtFront(TheList,(unsigned long*)4); insertAtBack(TheList,(unsigned long*)4); moveLast(TheList); insertBeforeCurrent(TheList,(unsigned long*)3); printList(out,TheList); deleteFirst(TheList); deleteCurrent(TheList); deleteLast(TheList); printList(out,TheList); makeEmpty(TheList); printList(out,TheList); /*free list and close output file*/ freeList(&TheList); fclose(out); return 0; }
int main() { ListHndl intList; intList = newList(); int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; /* * Integer list testing * */ printHeading(" INTEGER LIST ", '#', 80); printList(stdout, intList); accessorTest("isEmpty", 1, isEmpty(intList)); for(int i = 0; i < 10; i++) { insertAtFront(intList, data[i]); mutatorTest("%s : data = %d", "insertAtFront", data[i]); } moveFirst(intList); for(int i = 0; i < 5; i++) { printList(stdout, intList); insertBeforeCurrent(intList, data[i]); mutatorTest("insertBeforeCurrent : data = %d", data[i]); moveNext(intList); } accessorTest("isEmpty", 0, isEmpty(intList)); printList(stdout, intList); moveFirst(intList); while( !offEnd(intList) ) { printList(stdout, intList); moveNext(intList); } moveLast(intList); while( !offEnd(intList) ) { printList(stdout, intList); movePrev(intList); } makeEmpty(intList); mutatorTest("makeEmpty( intList)"); printList(stdout, intList); accessorTest("isEmpty", 1, isEmpty(intList)); freeList(intList); return 0; }
/* putEdgeArc */ void putEdgeArc(GraphRef G, int u, int v){ moveTo(G->adj[u], 0); while(1){ if(getCurrent(G->adj[u]) < v && getIndex(G->adj[u]) < getLength(G->adj[u])-1){ moveNext(G->adj[u]); } else if(getCurrent(G->adj[u]) > v ){ insertBeforeCurrent(G->adj[u], v); break; } else{ insertBack(G->adj[u], v); break; } } }
void addArc(GraphRef G, int u, int v){ ListRef row = G->adjacency[u]; /* Inserts vetex v into row u in the adjacency array */ if (isEmpty(row)){ insertFront(row, v); return; } else{ moveTo(row, 0); while (!offEnd(row)){ if (v == getCurrent(row)) return; if (v > getCurrent(row)){ moveNext(row); }else{ insertBeforeCurrent(row, v); return; } } insertBack(row, v); } }
int main(int argc, char* argv[]) { int i; ListRef A = newList(); ListRef B = newList(); ListRef ACopy = NULL; ListRef AB_Cat = NULL; insertBack(A, 10); insertBack(A, 20); insertBack(A, 30); insertBack(A, 40); insertBack(A, 50); insertBack(A, 60); printf("equals(A,B) : %d\n", equals(A, B)); insertBack(B, 10); insertBack(B, 20); insertBack(B, 30); insertBack(B, 40); insertBack(B, 50); insertBack(B, 60); AB_Cat = catList(A, B); printf("printLIST(AB_Cat) : "); printLIST(AB_Cat); ACopy = copyList(A); printf("printLIST(A) : "); printLIST(A); printf("printLIST(ACopy) : "); printLIST(ACopy); printf("equals(A,ACopy) : %d\n", equals(A, ACopy)); printf("equals(A,B) : %d\n", equals(A, B)); printf("printLIST(A) : "); printLIST(A); moveTo(A, getLength(A)); printf("offEnd(A) : %d\n", offEnd(A)); moveTo(A, 3); insertBeforeCurrent(A, 35); insertAfterCurrent(A, 45); printf("printLIST(A) : "); printLIST(A); printf("getCurrent(A) : %d\n", getCurrent(A)); movePrev(A); printf("getCurrent(A) : %d\n", getCurrent(A)); deleteCurrent(A); printf("printLIST(A) : "); printLIST(A); makeEmpty(B); deleteFront(A); printf("printLIST(A) : "); printLIST(A); printf("getLength(A) : %d\n", getLength(A)); printf("isEmpty(A) : %d\n", isEmpty(A)); makeEmpty(A); printf("isEmpty(A) : %d\n", isEmpty(A)); printf("getLength(A) : %d\n", getLength(A)); /* printf("printLIST(A) : "); printLIST(A); */ insertFront(B, 50); insertBack(B, 60); insertFront(B, 40); insertBack(B, 70); insertFront(B, 30); insertBack(B, 80); insertFront(B, 20); insertBack(B, 90); insertFront(B, 10); printf("printLIST(B) : "); printLIST(B); printf("offEnd(B) : %d\n", offEnd(B)); moveTo(B, 5); printf("offEnd(B) : %d\n", offEnd(B)); printf("getCurrent(B) : %d\n", getCurrent(B)); deleteCurrent(B); printf("printLIST(B) : "); printLIST(B); /* printf("getCurrent(B) : %d\n", getCurrent(B));*/ moveTo(B, 0); printf("getFront(B) : %d\n", getFront(B)); printf("getCurrent(B) : %d\n", getCurrent(B)); deleteFront(B); printf("printLIST(B) : "); printLIST(B); printf("getFront(B) : %d\n", getFront(B)); /* printf("getCurrent(B) : %d\n", getCurrent(B)); */ moveTo(B, (getLength(B)-1)); printf("getCurrent(B) : %d\n", getCurrent(B)); printf("getBack(B) : %d\n", getBack(B)); deleteBack(B); printf("getBack(B) : %d\n", getBack(B)); /* printf("getCurrent(B) : %d\n", getCurrent(B)); */ moveTo(B, (getLength(B)-1)); printf("getCurrent(B) : %d\n", getCurrent(B)); printf("getBack(B) : %d\n", getBack(B)); deleteBack(B); printf("getBack(B) : %d\n", getBack(B)); printf("getCurrent(B) : %d\n", getCurrent(B)); return(0); }
/* main program*/ int main(){ /* variables */ char inName[128]; char outName[128]; ListHndl array[128]; char tempLine[128]; int customers; int purchases; int tempCust; unsigned long tempBook; FILE *in; FILE *out; /* greeting & prompt for input and output file names */ printf("Welcome! Please enter your input file here: "); scanf("%s",inName); in = fopen(inName, "r"); if(in == NULL){ printf("Error: This input file does not exist.\n"); exit(1); } printf("Please enter your output file here: "); scanf("%s",outName); out = fopen(outName, "w"); /* check numbers of customer and purchased */ do{ if(fgets(tempLine, 128, in) == NULL) invalidInput(); }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &customers) != 1) invalidInput(); do{ if(fgets(tempLine, 128, in) == NULL) invalidInput(); }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &purchases) != 1) invalidInput(); /*makes empty Lists and fills them up*/ for(int i = 0; i<customers; i++) array[i] = newList(); for(int i = 0; i<purchases; i++){ do{ if(fgets(tempLine, 128, in) == NULL){ printf("Error: Number of purchases does not match (too little).\n"); exit(1); } }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &tempCust) != 1) invalidInput(); if(sscanf(tempLine,"%*d%lu", &tempBook) != 1) invalidInput(); if(tempCust > customers){ printf("Error: Invalid customer number.\n"); exit(1); } if(isEmpty(array[tempCust-1])){ insertAtBack(array[tempCust-1],tempBook); continue; } moveFirst(array[tempCust-1]); while(!offEnd(array[tempCust-1])){ if(tempBook < (unsigned)getCurrent(array[tempCust-1])){ insertBeforeCurrent(array[tempCust-1], tempBook); break; } moveNext(array[tempCust-1]); } if(offEnd(array[tempCust-1])) insertAtBack(array[tempCust-1],tempBook); } /* checks for any lingering purchases */ for(;;){ if(fgets(tempLine, 128, in) == NULL) break; if(sscanf(tempLine,"%d", &tempCust) != 0){ printf("Error: Number of purchases does not match (too many).\n"); exit(1); } } /*printing store*/ fprintf(out, "customer# books purchased\n"); for(int i = 0; i<customers; i++){ fprintf(out, "%d ",i+1); moveFirst(array[i]); while(!offEnd(array[i])){ fprintf(out, "%lu ", getCurrent(array[i])); moveNext(array[i]); } fprintf(out, "\n"); } /*free everythingggggggggggg*/ for(int i = 0; i<customers; i++) freeList(&array[i]); fclose(in); fclose(out); /* end program */ printf("Done.\n"); return 0; }
int main (){ /***Exercise List constructor***/ ListHndl List; List = NULL; List = newList (); if(List){ printf("List Created\n"); }else{ printf("List Not Created\n"); } printf("isEmpty %d\n",isEmpty(List)); /*should print 1*/ /***Populate with test data***/ int i; for(i=0; i<=4; i++){ insertAtFront(List,i); } printList(stdout, List); printf("isEmpty %d\n",isEmpty(List)); /*should print 0*/ int j; for(j=5; j<=9; j++){ insertAtBack(List,j); } printList(stdout, List); /***Exercise all access functions***/ printf("offEnd %d\n",offEnd(List));/*should print 0*/ printf("atFirst %d\n",atFirst(List));/*should print 0*/ printf("atLast %d\n",atLast(List));/*should print 0*/ printf("getFirst %d\n", getFirst(List));/*should print 4*/ printf("getLast %d\n", getLast(List));/*should print 9*/ printf("getCurrent %d\n", getCurrent(List));/*should print 0*/ /***Exercise all removal manipulation functions***/ deleteLast(List); printList(stdout, List); printf("getLast %d\n", getLast(List));/*should print 8*/ deleteFirst(List); printList(stdout, List); printf("getFirst \n", getFirst(List));/*should print 3*/ deleteCurrent(List); printList(stdout, List); moveLast(List); printList(stdout, List); movePrev(List); printList(stdout, List); moveNext(List); printList(stdout, List); /***Exercise various edge cases***/ makeEmpty(List); insertAtFront(List, 40); moveFirst(List); deleteCurrent(List); insertAtFront(List, 41); insertAtBack(List, 42); moveFirst(List); insertBeforeCurrent(List, 43); printList(stdout, List); /***Exercise List destructors***/ deleteCurrent(List); printList(stdout, List); makeEmpty(List); printf("offEnd %d\n",offEnd(List));/*should print 1*/ freeList(&List); return(0); }