// CList.c - By Michael G. Workman // // This C console application builds a list and outputs the results of the list // then times the results // // This application is freely distributable under terms of MIT open source license #include #include #include // struct type for linked list struct Integers { unsigned int nIntNumber; struct Integers* next; struct Integers* previous; }; // head and last nodes struct Integers* head = NULL; struct Integers* last = NULL; // to keep track of the current node pointed to struct Integers* current = NULL; // Create Linked List void insert(int data) { // Allocate memory for new node struct Integers* node = malloc(sizeof(struct Integers)); node->nIntNumber = data; node->previous = NULL; node->next = NULL; // If head is empty, create new list if (head == NULL) { head = node; last = node; return; } // start at the end of the list if (last != NULL) { current = last; } else { current = head; } // Insert link at the end of the list current->next = node; last = node; node->previous = current; } // output the list void outputList() { // start at first node in list struct Integers* ptr = head; // output the contents of the list while (ptr != NULL) { printf("%d \n", ptr->nIntNumber); ptr = ptr->next; } } int main() { // start timing clock_t start = clock(); // loop through total number of elements adding each integer with each iteration int i; for (i = 0; i < 100000; i++) { insert(i); } // output the contents of the list outputList(); // stop timing clock_t stop = clock(); // calculate time in seconds double timeSeconds = ((double) stop - start) / (double) CLOCKS_PER_SEC; // output results printf("C time to build and display list: %f seconds\n", timeSeconds); printf("Number of elements: %d \n\n", i); return 0; }