int main(int argc, char* argv[]){ // varible declarations int i=0,j=0,length=0,*array=NULL, temp=0,key=0,found=0; double arrayTime, nodeTime; Node* head=NULL; clock_t start,end; key = atoi(argv[1]);// change string to integer printf("%-15s%-15s\n","Linear search","Node Search"); //loop for all the argumens for(i=2; i<argc; i++){ FILE* input = fopen(argv[i],"r"); if(input == NULL){ printf("Unable to open %s\n",argv[i]); } else{ fscanf(input,"%d",&length); array = (int *)malloc(length*sizeof(int)); for(j=0;j<length;j++){ fscanf(input,"%d",&temp); array[j] = temp; head = insert_end(head, temp); } //timing the algorith start = clock(); found = linearSearch(array,length,key); end = clock(); arrayTime = (double)(end-start)/CLOCKS_PER_SEC; //check if the search was found if(found == 0){ printf("number not found in linear search\n"); } start = clock(); found = nodeSearch(head, key); end = clock(); nodeTime = (double)(end-start)/CLOCKS_PER_SEC; if(found == 0){ printf("number not found in node search\n"); } printf("%-15lf%-15lf\n",arrayTime,nodeTime); //free the malloced memory free(array); free_node(head); head = NULL; } close(input);//close the input file } }
//recursivly searches the node int nodeSearch(Node* head, int key){ if(head == NULL){ return 0; } else if(head->value == key){ return 1; } else{ return nodeSearch(head->next, key); } }
//finds the GraphNode in the list, and if it doesn't exist, creates one with that name GraphNode* findNode(char* name, LinkedList* list) { LinkedListNode* currnode = list->head; GraphNode* graphnode = nodeSearch(name, list); //if the node can't be found, allocate a node for it if (graphnode == NULL) { graphnode = makeGraphNode(name); insertLast(list, graphnode); } return graphnode; }