void testInsertAtN(){ Node *list = makeRandListOfSizeN(100); int vals[] = {16, 1024, 99, -10, -54098}; int poss[] = {0, 40, 22, 99, 32}; // Test insertion into null list Node *test = NULL; test = insertAtN(test, vals[0], poss[0]); assert(test->val == vals[0]); // Insert at various places for(int i = 0 ; i < (int)getArrLen(vals) ; i++){ list = insertAtN(list, vals[i], poss[i]); } // Ensure length is correct assert(getNumElements(list) == 100 + (int)getArrLen(vals)); // Insert random value at end int newRandVal = arc4random(); list = insertAtN(list, newRandVal, getNumElements(list)); DArray arr = arrayify(list); // Ensure that value (plus a couple other inserted ones) are correct assert(arr.contents[arr.size-1] == newRandVal); assert(arr.contents[poss[0]] == vals[0]); assert(arr.contents[poss[2]] == vals[2]); free(arr.contents); delList(list); delList(test); printf(">> Test insert at n completed! <<\n"); }
void stressTests(){ for(int i = 0 ; i <= 1000 ; i++){ Node *list = makeRandListOfSizeN(i * 1000); delList(list); if(!(i%50)){ printf("Made and deleted list of size %d...\n", i*1000); } } int hellListSize = 10000000; Node *list = makeRandListOfSizeN_bounded(hellListSize, 5000000); printList(list); for(int i = 0 ; i < 5000000 ; i++){ Node *t = findElement(list, i); if(t){ int pos = getNumElements(t->next); printf("Found %d in list at pos %d!\n", i, pos); break; } } for(int i = 0 ; i < 3000 ; i++){ int op = arc4random() % 4; switch(op){ case 0: list = insertAtN(list, arc4random(), arc4random_uniform(hellListSize)); break; case 1: list = deleteAtN(list, arc4random_uniform(hellListSize)); break; case 2: list = changeValueAtN(list, arc4random(), arc4random_uniform(hellListSize)); break; case 3: list = addToTail(list, arc4random()); break; } if(!(i%50)) printf("%d random operations completed...\n", i); } delList(list); }
int main(void) { setvbuf(stdout, NULL, _IONBF, 0); Header head; head.head = NULL; head.tail = NULL; head.nodeCount = 0; int menu = 0, list = 0; do { do { system("cls"); printf("\n Manipular qual lista.\n"); printf("1 - Encadeada.\n"); printf("2 - Sequencial.\n"); scanf("%d", &list); system("cls"); } while (list != 1 && list != 2); system("cls"); printf("\n Digite o numero da opcao desejada.\n"); printf("1 - Inserir elemento no final da lista.\n"); printf("2 - Inserir elemento no inicio da lista.\n"); printf("3 - Remover elemento a partir de um valor.\n"); printf("4 - Imprimir a lista.\n"); printf("5 - Inserir na posicao N.\n"); printf("6 - Remover ultimo da lista.\n"); printf("7 - Remover primeiro da lista.\n"); printf("8 - Remover elemento na posicao N.\n"); printf("9 - Carregar lista do arquivo.\n"); printf("10 - Salvar lista atual em arquivo.\n"); printf("11 - Inserction.\n"); printf("12 - Selection.\n"); printf("13 - Bubble.\n"); printf("14 - Shell.\n"); printf("15 - Quick.\n"); printf("16 - Merge.\n"); printf("17 - Busca Binaria.\n"); printf("18 - Busca Seq.\n"); printf("20 - Sair.\n"); scanf("%d", &menu); system("cls"); switch (menu) { case 1: if (list == 1) { insertNodeAtTheEnd(&head, getValue(), 1); reference(&head); } else { insertSeqAtTheEnd(getValue(), 1); } break; case 2: if (list == 1) { insertNodeAtStart(&head, getValue(), 1); reference(&head); } else { insertSeqAtStart(getValue(), 1); } break; case 3: if (list == 1) { deleteNodeByValue(&head); reference(&head); } else { deleteSeqByValue(); } break; case 4: if (list == 1) { printList(&head); } else { printSeq(); } break; case 5: if (list == 1) { insertAtN(&head, getValue(), getPosIns(&head)); reference(&head); } else { insertSeqAtN(getValue(), getPosSeqIns()); } break; case 6: if (list == 1) { deleteLastNode(&head, 1); reference(&head); } else { deleteLastSeq(1); } break; case 7: if (list == 1) { deleteFirstNode(&head, 1); reference(&head); } else { deleteFirstSeq(1); } break; case 8: if (list == 1) { deleteNodeAtN(&head, getPosDel(&head)); reference(&head); } else { deleteSeqAtN(getPosSeqDel(), 1); } break; case 9: listOptions(&head, list); break; case 10: if (list == 1) { writeLinkedList(&head); } else { writeSequentialList(); } break; case 11: if (list == 1) { linkedInserctionSort(&head); } else { inserctionSort(); } break; case 12: if (list == 1) { linkedSelectionSort(&head); } else { selectionSort(); } ; break; case 13: if (list == 1) { linkedBubbleSort(&head); } else { bubbleSort(); } break; case 14: if (list == 1) { linkedShellSort(&head); } else { shellSort(); } break; case 15: if (list == 1) { linkedCallQuickSort(&head); } else { callQuickSort(); } break; case 16: if (list == 1) { linkedCallMergeSort(head.nodeCount); } else { callMergeSort(); } break; case 17: if (list == 1) { linkedCallBynarySearch(&head); } else { callBynarySearch(); } break; case 18: if (list == 1) { searchNodeByValue(&head); } else { searchByValue(); } break; } } while (menu != 20); wipeList(&head); return EXIT_SUCCESS; }