Node* composition(Node *pFirst, Node *pSecond) { Node *pResult = NULL; Node **ppResult = &pResult; Node *pSortFirst = bubbleSortList(pFirst); Node *pSortSecond = bubbleSortList(pSecond); Node **ppSortFirst = &pSortFirst; Node **ppSortSecond = &pSortSecond; if (pSortFirst == NULL) { pResult = pSortSecond; return pResult; } if (pSortSecond == NULL) { pResult = pSortFirst; return pResult; } while (pSortFirst && pSortSecond) { if (pSortFirst->value < pSortSecond->value) { push(ppResult, pSortFirst->value); pSortFirst = pSortFirst->next; continue; } if (pSortFirst->value == pSortSecond->value) { push(ppResult, pSortFirst->value); pSortFirst = pSortFirst->next; pSortSecond = pSortSecond->next; continue; } if (pSortFirst->value > pSortSecond->value) { push(ppResult, pSortSecond->value); pSortSecond = pSortSecond->next; continue; } } while (pSortFirst) { push(ppResult, pSortFirst->value); pSortFirst = pSortFirst->next; } while (pSortSecond) { push(ppResult, pSortSecond->value); pSortSecond = pSortSecond->next; } freeList(ppSortFirst); freeList(ppSortSecond); return pResult; }
int main(int argc, char* argv[]) { /* Declarations */ List list; List list_1; List list_2; List list_3; List list_4; List list_5; List list_6; Position pos; Position pos_1; Position pos_2; Position pos_3; Position pos_4; Position pos_a, pos_b; int len; int idx; ElementType elem; /* Initialize list */ list=createList(); /* Test functions 'insertNode' and 'appendNode' */ printf("Test functions 'insertNode' and 'appendNode'\n\n"); printf("Before 'insertNode':\n"); printList(list); pos_1=getHeaderNode(list); insertNode(11, list, pos_1); pos_2=advanceNode(pos_1); insertNode(2, list, pos_2); pos_3=advanceNode(pos_2); insertNode(3, list, pos_3); pos_4=advanceNode(pos_3); insertNode(10, list, pos_4); insertNode(9, list, pos_2); printf("After 'insertNode':\n"); printList(list); printf("Before 'appendNode':\n"); printList(list); appendNode(7, list); appendNode(2, list); printf("After 'appendNode'\n"); printList(list); printf("\n"); /* Test functions 'cloneList', 'deleteNode' and 'deleteList' */ printf("Test functions 'cloneList', 'deleteNode' and 'deleteList'\n\n"); list_1=cloneList(list); printf("Before 'deleteNode':\n"); printList(list_1); deleteNode(2, list_1); printf("After 'deleteNode':\n"); printList(list_1); printf("Before 'deleteList':\n"); printList(list_1); deleteList(list_1); printf("After 'deleteList':\n"); printList(list_1); printf("\n"); /* Test function 'getListLength' */ printf("Test function 'getListLength'\n\n"); len=getListLength(list); printf("Length: %d\n", len); printf("\n"); /* Test functions 'findNode', 'findNodePrevious' and 'getNodeIndex' */ printf("Test functions 'findNode', 'findNodePrevious' and 'getNodeIndex'\n\n"); elem=2; pos=findNode(elem, list); if(pos!=NULL) { idx=getNodeIndex(pos, list); printList(list); printf("finding %d, Element at index %d found\n", elem, idx); } else { printf("finding %d, not found\n", elem); } elem=3; pos=findNodePrevious(elem, list); /* Check whether elem is found in list */ if(pos->m_next!=NULL) { idx=getNodeIndex(pos, list); printf("finding previous element of %d, Element at index %d found\n", elem, idx); } else { /* elem is not in list */ printf("finding previous element of %d, not found\n", elem); } printf("\n"); /* Test functions 'makeListEmpty' and 'isListEmpty' */ printf("Test functions 'makeListEmpty' and 'isListEmpty'\n\n"); list_2=cloneList(list); printf("Before 'makeListEmpty':\n"); printList(list_2); list_2=makeListEmpty(list_2); if(isListEmpty(list_2)) { printf("List emptied successfully\n"); printf("After 'makeListEmpty'\n"); printList(list_2); } printf("\n"); /* Test functions 'getHeaderNode', 'getFirstNode', 'getLastNode', 'advanceNode' and 'getNodeElem' */ printf("Test functions 'getHeaderNode', 'getFirstNode', 'getLastNode', 'advanceNode' and 'getNodeElem'\n\n"); printList(list); pos=getHeaderNode(list); printf("Header at index %d\n", getNodeIndex(pos, list)); pos=getFirstNode(list); printf("First element at index %d have value %d\n", getNodeIndex(pos, list), getNodeElem(pos)); pos=getLastNode(list); printf("Last element at index %d have value %d\n", getNodeIndex(pos, list), getNodeElem(pos)); pos=getFirstNode(list); pos=advanceNode(pos); printf("Second element at index %d have value %d\n", getNodeIndex(pos, list), getNodeElem(pos)); printf("\n"); /* Test function 'reverseList' */ printf("Test function 'reverseList'\n\n"); list_3=cloneList(list); printf("Before 'reverseList':\n"); printList(list_3); list_3=reverseList(list_3); printf("After 'reverseList':\n"); printList(list_3); printf("\n"); /* Test function 'swapNode' */ printf("Test function 'swapNode'\n\n"); list_4=cloneList(list); printf("Before 'swapNode':\n"); printList(list_4); pos_a=getHeaderNode(list_4); pos_a=advanceNode(pos_a); pos_a=advanceNode(pos_a); pos_b=advanceNode(pos_a); swapNode(pos_a, pos_b, list_4); printf("After 'swapNode':\n"); printList(list_4); printf("\n"); /* Test function 'bubbleSortList' */ printf("Test function 'bubbleSortList'\n\n"); list_5=cloneList(list); printf("Before 'bubbleSortList':\n"); printList(list_5); bubbleSortList(list_5); printf("After 'bubbleSortList':\n"); printList(list_5); printf("\n"); /* Test function 'connectList' */ printf("Test function 'connectList'\n\n"); printf("List 1:\n"); printList(list); printf("Length: %d\n", getListLength(list)); printf("List 2:\n"); printList(list_5); printf("Length: %d\n", getListLength(list_5)); list_6=connectList(list, list_5); printf("Connected list:\n"); printList(list_6); printf("Length: %d\n", getListLength(list_6)); printf("\n"); /* Cleanup memory */ destroyList(list); destroyList(list_1); destroyList(list_2); destroyList(list_3); destroyList(list_4); destroyList(list_5); destroyList(list_6); return 0; }
int main() { FILE *file; List listDynamic; info reg; char option; printf("/t/t/t/tPrueba Lista Dinamica\n"); if(!openFile(&file,FILE_NAME,"r+b",!MESSAGE_OR_NOT)) { createFile(); if(!openFile(&file,FILE_NAME,"r+b",MESSAGE_OR_NOT)) return 0; } showFile(&file); createList(&listDynamic); fread(®,1,sizeof(info),file); while(!feof(file)) { putTheTopOfTheList(&listDynamic,®); fread(®,1,sizeof(info),file); } fseek(file,0L,SEEK_SET); option = menu(MESS,OPTIONS); while(option != 'i') { switch(option) { case 'b': { bubbleSortList(&listDynamic); puts("Lista ordenada por DNI"); break; } case 'c': { puts("Ingrese el DNI del nodo a eliminar:"); fflush(stdin); scanf("%08ld",®.dni); if(removeFromList(&listDynamic,®,compear) == NOT_FOUND) puts("Nodo no encontrado"); break; } case 'd': { /**DNI**/ printf("DNI:"); fflush(stdin); scanf("%08ld",®.dni); puts(""); if(searchNode(&listDynamic,®,compear) == O_K) showData(®); else puts("Nodo no encontrado"); break; } case 'f': { /**DNI**/ printf("DNI:"); fflush(stdin); scanf("%08ld",®.dni); puts(""); /**APELLIDO**/ printf("APELLIDO:"); fflush(stdin); scanf("%s",reg.surName); puts(""); /**NOMBRE**/ printf("NOMBRE:"); fflush(stdin); scanf("%s",reg.name); puts(""); /**PROMEDIO**/ printf("PROMEDIO:"); fflush(stdin); scanf("%f",®.average); puts(""); if(putTheTopOfTheList(&listDynamic,®) == WITHOUT_MEMORY) puts("Sin memoria"); break; } case 'g': { /**DNI**/ printf("DNI:"); fflush(stdin); scanf("%08ld",®.dni); puts(""); /**APELLIDO**/ printf("APELLIDO:"); fflush(stdin); scanf("%s",reg.surName); puts(""); /**NOMBRE**/ printf("NOMBRE:"); fflush(stdin); scanf("%s",reg.name); puts(""); /**PROMEDIO**/ printf("PROMEDIO:"); fflush(stdin); scanf("%f",®.average); puts(""); if(putAnEndTheList(&listDynamic,®) == WITHOUT_MEMORY) puts("Sin memoria"); break; } case 'h': { /**DNI**/ printf("DNI:"); fflush(stdin); scanf("%08ld",®.dni); puts(""); /**APELLIDO**/ printf("APELLIDO:"); fflush(stdin); scanf("%s",reg.surName); puts(""); /**NOMBRE**/ printf("NOMBRE:"); fflush(stdin); scanf("%s",reg.name); puts(""); /**PROMEDIO**/ printf("PROMEDIO:"); fflush(stdin); scanf("%f",®.average); puts(""); if(putInOrder(&listDynamic,®,compear,accumulate) == WITHOUT_MEMORY) puts("Sin memoria"); break; } } option = menu(MESS,OPTIONS); } putListInFile(&listDynamic,&file); showFile(&file); fclose(file); clearList(&listDynamic); return 0; }
/* Seek highest color. * Create list of clusters whose color is greater than or equal. * Order them by X position order, then by Y order. * Save average X and Z position. * Detect distance between them. * If the distance varies on both X and Y directions, the cluster might not belong to thrust. * * */ void seekDiamonds(LPCLUSIMG clusImg, Cluster* head){ Cluster* thisClus = head; ColorValue maxColor, thisColor; PCLUSLIST clusList, thisItem, lastItem; int count = 0; float minDist, thisDist; minDist = 500.0f; while(thisClus != NULL){ thisDist = getColorDistance(COLOR_WHITE,thisClus->getTone()); if(thisDist < minDist){ count = 1; minDist = thisDist; maxColor.color = thisClus->getTone(); } thisClus = thisClus->getNext(); } //create CLUSLIST items. writeConsoleFmt("Highest color: %3d,%3d,%3d\n",maxColor.color.red,maxColor.color.green,maxColor.color.blue); int xmin, xmax, ymin, ymax; clusList = lastItem = (PCLUSLIST)NULL; thisClus = head; while(thisClus != NULL){ thisColor.color = thisClus->getTone(); if(thisColor.value == maxColor.value){ thisItem = (PCLUSLIST)malloc(sizeof(CLUSLIST)); thisItem->cluster = thisClus; thisItem->next = NULL; thisItem->left = thisClus->getLeft(); thisItem->top = thisClus->getTop(); thisItem->width = thisClus->getRight()-thisClus->getLeft(); thisItem->height = thisClus->getBottom()-thisClus->getTop(); thisItem->count = thisClus->getCount(); if(clusList == NULL){ clusList = thisItem; xmin = thisItem->left; xmax = thisItem->left; ymin = thisItem->top; ymax = thisItem->top; }else{ lastItem->next = thisItem; if(thisItem->left < xmin) xmin = thisItem->left; if(thisItem->left > xmax) xmax = thisItem->left; if(thisItem->top < ymin) ymin = thisItem->top; if(thisItem->top > ymax) ymax = thisItem->top; } lastItem = thisItem; } thisClus = thisClus->getNext(); } lastItem = clusList; while(lastItem != NULL){ writeConsoleFmt("Cluster: %08x. Left: %d\n",lastItem->cluster,lastItem->left); lastItem = lastItem->next; } writeConsoleFmt("Variation: x:%d(%d-%d); y:%d(%d-%d);\n",xmax-xmin,xmax,xmin,ymax-ymin,ymax,ymin); clusList = lastItem = bubbleSortList(clusList,lastItem); while(lastItem != NULL){ writeConsoleFmt("Cluster: %08x. Left: %d. DistNxt: %d\n",lastItem->cluster,lastItem->left, (lastItem->next!=NULL?lastItem->next->left-(lastItem->left+lastItem->width):0)); clusList = lastItem; lastItem = lastItem->next; free(clusList); } }