int main() { system("color 0a"); int ch,data; do { printf("Enter choice : \n1. Add at rear\n2.Add at front\n3.Remove from rear\n4.Remove from Front\n5.Display\n6.search for an item\n0. to exit\n"); scanf("%d",&ch); switch(ch) { case 1:printf("Enter data :"); scanf("%d",&data); addRear(data); break; case 2: printf("Enter data :"); scanf("%d",&data); addFront(data); break; case 3:removeRear(); break; case 4:removeFront(); break; case 5:display(); break; case 6: printf("Enter data to search :"); scanf("%d",&data); search(data); break; } }while(ch!=0); return 0; }
/** * This method removes the node whose pointer is passed in */ NodePtr removeNode(ListPtr list, NodePtr node) { if (list == NULL ) return NULL ; if (list->head == NULL ) return NULL ; if (list->tail == NULL ) return NULL ; if (node == NULL ) return NULL ; if (list -> size <= 0 ) return NULL; if (list ->size == 0 && list->head == NULL && list->tail ==NULL) return NULL; if (node == list->head || node->prev == NULL ) return removeFront(list); if (node == list->tail || node->next == NULL ) return removeRear(list); node->prev->next = node->next; node->next->prev = node->prev; node->next = NULL; node->prev = NULL; list->size--; return node; }
int main() { int ch,data; do { printf("1.Add at rear\n2.Add at front\n3.Remove from rear\n4.remove from front\n5.Display\n0.exit\n"); scanf("%d",&ch); switch(ch) { case 1:printf("Enter data : "); scanf("%d",&data); addRear(data); break; case 2:printf("Enter data : "); scanf("%d",&data); addFront(data); break; case 3:removeRear(); break; case 4:removeFront(); break; case 5:display(); break; case 0:printf("\nExitting\n"); break; } }while(ch!=0); return 0; }
/** * Runs tests on the removeRear function by removing all the nodes in the list **/ void removeRearAll(ListPtr list, int count) { int i; int testSize = count; for(i = 0; i < count; i++) { NodePtr node = removeRear(list); testSize--; if(node == NULL) {printf("Node is NULL"); continue;} //printf("i: %d jobid: %d size: %d", i, node->data->jobid, list->size); if(node->data->jobid != testSize || list->size != testSize || (list->head != NULL && list->head->data->jobid != 0) || (list->tail != NULL && list->tail->data->jobid != testSize - 1) ) { printf("\n%s\n%s\n", sep, "Error: removeRearAll invalid"); break; } freeNode(node); } if(DEBUG > 0) verifyRearAll(list, count); }
void runRandomTests(struct parameters *state, ListPtr list) { int i; int start; int test; NodePtr node; JobPtr job; long int num; srand(state->seed); start=0; if (state->restart) { start = state->done+1; srand(state->randomNum); } for (i=start; i<state->count; i++) { num = rand(); test = num % NUM_TESTS; if ((i > 0) && ((i % CHECKPOINT_COUNT) == 0)) { fprintf(stderr, "checkpointing list, count = %d\n", i); state->done = i-1; state->randomNum = num; checkpointList(list, state->saveFile); checkpointTestSuite(state, stateFile); } switch (test) { case 0: if (DEBUG > 1) fprintf(stderr,"addAtFront\n"); state->n++; job = createJob(state->n, "some info"); node = createNode(job); addAtFront(list, node); break; case 1: if (DEBUG > 1) fprintf(stderr,"addAtRear\n"); state->n++; job = createJob(state->n, "some info"); node = createNode(job); addAtRear(list, node); break; case 2: if (DEBUG > 1) fprintf(stderr,"removeFront\n"); node = removeFront(list); break; case 3: if (DEBUG > 1) fprintf(stderr,"removeRear\n"); node = removeRear(list); break; case 4: if (DEBUG > 1) fprintf(stderr,"removeNode\n"); node = removeNode(list, search(list, i)); break; case 5: if (DEBUG > 1) fprintf(stderr,"reverseList\n"); reverseList(list); default: break; } } }
void runRandomTests(int count, unsigned int seed, int n, ListPtr list) { int i; int test; NodePtr node; ObjectPtr job; ObjectPtr searchJob; int *tests; tests = (int *) malloc(sizeof(int)*NUM_TESTS); for (i=0; i<NUM_TESTS; i++) tests[i]=0; srandom(seed); for (i=0; i<count; i++) { test = (int) (NUM_TESTS * (double) rand()/RAND_MAX); tests[test]++; switch (test) { case 0: if (DEBUG_LEVEL > 1) fprintf(stderr,"addAtFront\n"); n++; job = createObject(n, "some info"); node = createNode(job); addAtFront(list, node); break; case 1: if (DEBUG_LEVEL > 1) fprintf(stderr,"addAtRear\n"); n++; job = createObject(n, "some info"); node = createNode(job); addAtRear(list, node); break; case 2: if (DEBUG_LEVEL > 1) fprintf(stderr,"removeFront\n"); node = removeFront(list); freeNode(node, freeObject); break; case 3: if (DEBUG_LEVEL > 1) fprintf(stderr,"removeRear\n"); node = removeRear(list); freeNode(node, freeObject); break; case 4: if (DEBUG_LEVEL > 1) fprintf(stderr,"removeNode\n"); searchJob = createObject(i,"foo"); node = removeNode(list, search(list, searchJob)); freeNode(node, freeObject); freeObject(searchJob); break; case 5: if (DEBUG_LEVEL > 1) fprintf(stderr,"reverseList\n"); reverseList(list); break; case 6: if (DEBUG_LEVEL > 1) fprintf(stderr,"searchList\n"); searchJob = createObject(i,"foo"); node = search(list, searchJob); freeObject(searchJob); break; default: break; } } print_stats(tests); free(tests); }
/** * Runs edge tests on the current Doubly Linked List implementation **/ void runEdgeCases() { ListPtr l = createList(); NodePtr n = NULL; printf("\n%s\n%s", sep, "Running edge cases with NULL values"); printf("\n%s", "addAtFront"); addAtFront(NULL, NULL); addAtFront(l, NULL); addAtFront(NULL, n); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "addAtRear"); addAtRear(NULL, NULL); addAtRear(l, NULL); addAtRear(NULL, n); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeFront"); n = removeFront(NULL); n = removeFront(l); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeRear"); n = removeRear(NULL); n = removeRear(l); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode"); n = removeNode(NULL, NULL); n = removeNode(l, NULL); n = removeNode(NULL, n); if(l->size == 0 && n == NULL && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "reverseList"); reverseList(NULL); reverseList(l); if(l->size == 0 && l->head == NULL && l->tail == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "search"); int i = 0; n = search(NULL, 0); n = search(l, 0); n = search(NULL, i); if(n == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s\n%s", sep, "Running edge cases with Single values"); printf("\n%s", "addAtFront"); JobPtr job = createJob(1, "Single", 1); n = createNode(job); l = createList(); addAtFront(l, n); if(l->size == 1 && l->head == n && l->tail == n && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeFront"); NodePtr removeFrontNode = removeFront(l); if(l->size == 0 && l->head == NULL && l->tail == NULL && removeFrontNode == n) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "addAtRear"); addAtRear(l, n); if(l->size == 1 && l->head == n && l->tail == n && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s s", "removeRear"); NodePtr removeRearNode = removeRear(l); if(l->size == 0 && l->head == NULL && l->tail == NULL && removeRearNode == n) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode, empty list"); NodePtr testnode = removeNode(l, n); if(l->size == 0 && l->head == NULL && l->tail == NULL && testnode == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode, one node, matches"); addAtFront(l, n); testnode = removeNode(l, n); if(l->size == 0 && l->head == NULL && l->tail == NULL && testnode == n) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "removeNode, one node, no match"); JobPtr noMatchJob = createJob(1, "no match", 1); NodePtr noMatch = createNode(noMatchJob); addAtFront(l, noMatch); testnode = removeNode(l, n); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && testnode == NULL && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "reverseList"); reverseList(l); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "search, matches"); testnode = search(l, 1); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && testnode == noMatch && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess"); else printf("\nFail"); printf("\n%s", "search, no match"); testnode = search(l, 0); if(l->size == 1 && l->head == noMatch && l->tail == noMatch && testnode == NULL && l->head->next == NULL && l->head->prev == NULL && l->tail->next == NULL && l->tail->prev == NULL) printf("\nSuccess\n"); else printf("\nFail\n"); freeList(l); }