static void SortedIntersection()//PROBLEM 16 { int boolean = 1; int content; struct node *Ahead = NULL; struct node *Bhead = NULL; struct node *Chead = NULL; while(boolean) { if(yesno("Would you like to enter a value for Ahead (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&Ahead, content); } else { boolean = 0; } Insert_Sort(&Ahead); } boolean = 1; while(boolean) { if(yesno("Would you like to enter a value for Bhead (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&Bhead, content); } else { boolean = 0; } Insert_Sort(&Bhead); } PrintListData(Ahead); PrintListData(Bhead); PrintListData(Chead); Sorted_Intersection(&Ahead, &Bhead, &Chead); PrintListData(Ahead); PrintListData(Bhead); PrintListData(Chead); freeElements(&Ahead);//free up the allocated memory if(Ahead == NULL) { printf("The head node was set to NULL.\n"); } freeElements(&Bhead);//free up the allocated memory if(Bhead == NULL) { printf("The head node was set to NULL.\n"); } freeElements(&Chead);//free up the allocated memory if(Chead == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 16
static void ISBST()//PROBLEM 13 and 14 { int boolean = 1; struct node *head = NULL; int content; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head, content); } else { boolean = 0; } } if(head == NULL) { printf("The tree is empty.\n"); return; } Is_Search_Tree(head); freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } //Now enter the same nodes and make a non searchable tree boolean = 1; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); NonSearchableTree(&head, content); } else { boolean = 0; } } if(head == NULL) { printf("The tree is empty.\n"); return; } Is_Search_Tree(head); freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 13 and 14
static void RecursionReverse()//PROBLEM 18 { int boolean = 1; int content; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&head, content); } else { boolean = 0; } } PrintListData(head); Recursion_Reverse(&head); PrintListData(head); freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 18
static void MergeSort()//PROBLEM 15 { int boolean = 1; int content; struct node *Ahead = NULL; while(boolean) { if(yesno("Would you like to enter a value for Ahead (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&Ahead, content); } else { boolean = 0; } } PrintListData(Ahead); Merge_Sort(&Ahead); PrintListData(Ahead); freeElements(&Ahead);//free up the allocated memory if(Ahead == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 14
static void InsertNth()//PROBLEM 5 { int boolean = 1; int content; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&head, content); } else { boolean = 0; } } boolean = 1; //reset boolean to default to prepare for next while loop while(boolean) { short int boolean2; short int boolean3 = 1; int value = 0; if(yesno("Would you like to insert a new node at an arbitrary position (Y/N)?")) { read_int("Enter the position for which you want to insert the node: ", &content); Insert_Nth(&head, content); printf("Now let's check the contents of the list.\n"); while(boolean3) { read_int("Please enter your value to search for: ", &content); boolean2 = GetValue(head, content, &value); if(boolean2 == 0) { printf("The list is either empty or the value you entered is out of range.\n"); } else { printf("The value of element %d is %d.\n", content, value); } if(!yesno("Do you want to search for another value of an element (Y/N)?")) { boolean3 = 0; } } } else { boolean = 0; } } freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 5
static void MinValue()//PROBLEM 4 { int boolean = 1; int content; int minvalue = 0; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head, content); } else { boolean = 0; } } minvalue = Min_Value(head); printf("The depth of the tree is %d.\n", minvalue); freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 4
static void DoubleTree()//PROBLEM 10 { int boolean = 1; int content; int depth; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head, content); } else { boolean = 0; } } if(head == NULL) { printf("The tree is empty.\n"); return; } depth = Max_Depth(head); Print_Paths(head, depth); Double_Tree(head); Print_Paths(head, depth); freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 10
static void PrintTree()//PROBLEM 5 { int boolean = 1; int content; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head, content); } else { boolean = 0; } } Print_Tree(head); freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 5
static void GetNth()//START PROBLEM 2 { int boolean = 1; int content; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); AppendNode(&head, content); } else { boolean = 0; } } boolean = 1; //reset boolean to default to prepare for next while loop while(boolean) { short int boolean2; int value = 0; if(yesno("Do you want to search for the value of an element (Y/N)?")) { read_int("Please enter your value to search for: ", &content); boolean2 = GetValue(head, content, &value); if(boolean2 == 0) { printf("The list is either empty or the value you entered is out of range.\n"); } else { printf("The value of element %d is %d.\n", content, value); } } else { boolean = 0; } } freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 2
static void AlternatingSplit()//PROBLEM 12 { int boolean = 1; int content; struct node *Ahead = NULL; struct node *Bhead = NULL; while(boolean) { if(yesno("Would you like to enter a value for Ahead (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&Ahead, content); } else { boolean = 0; } } PrintListData(Ahead); PrintListData(Bhead); Alternating_Split(&Bhead, &Ahead);//notice the reverse order here compared with Move_Node PrintListData(Ahead); PrintListData(Bhead); freeElements(&Ahead);//free up the allocated memory if(Ahead == NULL) { printf("The head node was set to NULL.\n"); } freeElements(&Bhead);//free up the allocated memory if(Bhead == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 12
static void BuildCount()//PROBLEM 1 and 2 { int boolean = 1; int content; int length = 0; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head, content); } else { boolean = 0; } } length = getLength(head); printf("The lenght of the tree is %d.\n", length); boolean = 1; //reset boolean to default to prepare for next while loop while(boolean) { int count = 0; if(yesno("Do you want to search for the total number of entries for a value (Y/N)?")) { read_int("Please enter your value to search for: ", &content); length = getLength(head); printf("The lenght of the tree is %d.\n", length); ElementSearch(head, content, &count); printf("The total number of entries for %d is %d.\n", content, count); } else { boolean = 0; } } freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 1 and 2
static void Append_Node_Problem()//PROBLEM 8, THE APPEND NODE { int boolean = 1; int content; struct node *Ahead = NULL; struct node *Bhead = NULL; while(boolean) { if(yesno("Would you like to enter a value for Ahead (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&Ahead, content); } else { boolean = 0; } } boolean = 1; while(boolean) { if(yesno("Would you like to enter a value for Bhead (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&Bhead, content); } else { boolean = 0; } } PrintListData(Ahead); PrintListData(Bhead); Append_Node(&Ahead, &Bhead); PrintListData(Ahead); PrintListData(Bhead); freeElements(&Ahead);//free up the allocated memory if(Ahead == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 8
static void Pop()//PROBLEM 4 { int boolean = 1; int content; struct node *head = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Push(&head, content); } else { boolean = 0; } } boolean = 1; //reset boolean to default to prepare for next while loop while(boolean) { int rv; if(yesno("Would you like to pop the head node and return its value (Y/N)?")) { rv = PopHead(&head); printf("The return value for the popped node is %d.\n", rv); } else { boolean = 0; } } freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } }//END PROBLEM 4
int getServiceFromYAML(maps* conf, char* file,service** service,char *name){ FILE *fh = fopen("test.yml", "r"); if(current_content!=NULL){ freeMap(¤t_content); free(current_content); current_content=NULL; } #ifdef DEBUG_SERVICE_CONF fprintf(stderr,"(STARTING)FREE current_element\n"); #endif if(current_element!=NULL){ freeElements(¤t_element); free(current_element); current_element=NULL; } my_service=NULL; my_service=*service; my_service->name=strdup(name); my_service->content=NULL; my_service->metadata=NULL; my_service->inputs=NULL; my_service->outputs=NULL; fh = fopen(file,"r"); if (fh==NULL){ fprintf(stderr,"error : file not found\n") ; return -1; } yaml_parser_t parser; yaml_token_t token; /* new variable */ /* Initialize parser */ if(!yaml_parser_initialize(&parser)) fputs("Failed to initialize parser!\n", stderr); if(fh == NULL) fputs("Failed to open file!\n", stderr); /* Set input file */ yaml_parser_set_input_file(&parser, fh); /* BEGIN new code */ int level=0; int plevel=level; int ilevel=-1; int blevel=-1; int ttype=0; int wait_metadata=-1; char *cur_key; do { yaml_parser_scan(&parser, &token); switch(token.type) { /* Stream start/end */ case YAML_STREAM_START_TOKEN: #ifdef DEBUG_YAML puts("STREAM START"); #endif break; case YAML_STREAM_END_TOKEN: #ifdef DEBUG_YAML puts("STREAM END"); #endif break; /* Token types (read before actual token) */ case YAML_KEY_TOKEN: #ifdef DEBUG_YAML printf("(Key token) "); #endif ttype=0; break; case YAML_VALUE_TOKEN: #ifdef DEBUG_YAML printf("(Value token) "); #endif ttype=1; break; /* Block delimeters */ case YAML_BLOCK_SEQUENCE_START_TOKEN: #ifdef DEBUG_YAML puts("<b>Start Block (Sequence)</b>"); #endif break; case YAML_BLOCK_ENTRY_TOKEN: #ifdef DEBUG_YAML puts("<b>Start Block (Entry)</b>"); #endif break; case YAML_BLOCK_END_TOKEN: blevel--; if(ilevel>=0) ilevel--; #ifdef DEBUG_YAML printf("<b>End block</b> (%d,%d,%d,%d)\n", blevel,level,ilevel,ttype); #endif break; /* Data */ case YAML_BLOCK_MAPPING_START_TOKEN: #ifdef DEBUG_YAML puts("[Block mapping]"); #endif blevel++; break; case YAML_SCALAR_TOKEN: if(ttype==0){ cur_key=zStrdup((char *)token.data.scalar.value); } if(ttype==1){ if(current_content==NULL){ current_content=createMap(cur_key,(char *)token.data.scalar.value); }else{ addToMap(current_content,cur_key,(char *)token.data.scalar.value); } free(cur_key); cur_key=NULL; } if(ttype==0 && blevel==0 && level==0 && strcasecmp((char *)token.data.scalar.value,"MetaData")==0 && blevel==0){ addMapToMap(&my_service->content,current_content); #ifdef DEBUG_YAML fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; wait_metadata=1; } if(ttype==0 && blevel>0 && level>0 && strcasecmp((char *)token.data.scalar.value,"MetaData")==0){ if(current_element->content==NULL && current_content!=NULL) addMapToMap(¤t_element->content,current_content); #ifdef DEBUG_YAML dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; wait_metadata=1; } if(ttype==0 && strcasecmp((char *)token.data.scalar.value,"inputs")==0 && blevel==0){ if(wait_metadata>0){ addMapToMap(&my_service->metadata,current_content); wait_metadata=-1; }else{ if(current_content!=NULL && my_service->content==NULL) addMapToMap(&my_service->content,current_content); } #ifdef DEBUG_YAML dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; wait_metadata=false; level++; } if(ttype==0 && strcasecmp((char *)token.data.scalar.value,"outputs")==0 && blevel==1){ level++; #ifdef DEBUG_YAML dumpMap(current_content); printf("\n***\n%d (%d,%d,%d,%d)\n+++\n", current_element->defaults==NULL,blevel,level,ilevel,ttype); #endif if(current_element->defaults==NULL && current_content!=NULL && ilevel<0){ current_element->defaults=(iotype*)malloc(IOTYPE_SIZE); current_element->defaults->content=NULL; current_element->defaults->next=NULL; addMapToMap(¤t_element->defaults->content,current_content); #ifdef DEBUG_YAML dumpElements(current_element); dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; }else{ if(current_content!=NULL && ilevel<=0){ addMapToIoType(¤t_element->supported,current_content); #ifdef DEBUG_YAML dumpElements(current_element); dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; } } } if(level==1 && strcasecmp((char *)token.data.scalar.value,"default")==0){ ilevel=0; } if(level==1 && strcasecmp((char *)token.data.scalar.value,"supported")==0){ #ifdef DEBUG_YAML dumpMap(current_content); printf("\n***\n%d (%d,%d,%d,%d)\n+++\n", current_element->defaults==NULL,blevel,level,ilevel,ttype); #endif if(current_element->defaults==NULL && current_content!=NULL && ilevel<0){ current_element->defaults=(iotype*)malloc(IOTYPE_SIZE); current_element->defaults->content=NULL; current_element->defaults->next=NULL; addMapToMap(¤t_element->defaults->content,current_content); #ifdef DEBUG_YAML dumpElements(current_element); dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; }else{ if(current_content!=NULL && ilevel<=0){ if(current_element->supported==NULL){ current_element->supported=(iotype*)malloc(IOTYPE_SIZE); current_element->supported->content=NULL; current_element->supported->next=NULL; } addMapToMap(¤t_element->supported->content,current_content); #ifdef DEBUG_YAML dumpElements(current_element); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; } } ilevel=1; } if(strncasecmp((char *)token.data.scalar.value,"ComplexData",11)==0 || strncasecmp((char *)token.data.scalar.value,"LiteralData",10)==0 || strncasecmp((char *)token.data.scalar.value,"ComplexOutput",13)==0 || strncasecmp((char *)token.data.scalar.value,"LiteralOutput",12)==0 || strncasecmp((char *)token.data.scalar.value,"BoundingBoxOutput",13)==0 || strncasecmp((char *)token.data.scalar.value,"BoundingBoxData",12)==0){ current_element->format=zStrdup((char *)token.data.scalar.value); free(cur_key); cur_key=NULL; if(wait_metadata>0 && current_content!=NULL){ addMapToMap(¤t_element->metadata,current_content); wait_metadata=-1; }else{ if(current_content!=NULL){ addMapToMap(¤t_element->content,current_content); } } #ifdef DEBUG_YAML dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; #ifdef DEBUG_YAML dumpElements(current_element); #endif } if(blevel==1 && level==1){ if(current_element!=NULL && current_content!=NULL){ if(current_element->defaults==NULL){ current_element->defaults=(iotype*)malloc(IOTYPE_SIZE); current_element->defaults->content=NULL; current_element->defaults->next=NULL; addMapToMap(¤t_element->defaults->content,current_content); }else{ if(current_element->supported==NULL){ current_element->supported=(iotype*)malloc(IOTYPE_SIZE); current_element->supported->content=NULL; current_element->supported->next=NULL; addMapToMap(¤t_element->supported->content,current_content); }else addMapToIoType(¤t_element->supported,current_content); } } if(current_element!=NULL){ if(my_service->inputs==NULL) my_service->inputs=dupElements(current_element); else addToElements(&my_service->inputs,current_element); freeElements(¤t_element); free(current_element); } plevel=level; current_element=(elements*)malloc(ELEMENTS_SIZE); current_element->name=zStrdup((char *)token.data.scalar.value); current_element->content=NULL; current_element->metadata=NULL; current_element->format=NULL; current_element->defaults=NULL; current_element->supported=NULL; current_element->next=NULL; } if(blevel==1 && level==2){ if(current_element!=NULL && current_content!=NULL){ if(current_element->defaults==NULL){ current_element->defaults=(iotype*)malloc(IOTYPE_SIZE); current_element->defaults->content=NULL; current_element->defaults->next=NULL; addMapToMap(¤t_element->defaults->content,current_content); }else{ if(current_element->supported==NULL){ current_element->supported=(iotype*)malloc(IOTYPE_SIZE); current_element->supported->content=NULL; current_element->supported->next=NULL; addMapToMap(¤t_element->supported->content,current_content); }else addMapToIoType(¤t_element->supported,current_content); } } if(current_element!=NULL){ if(plevel==level){ if(my_service->outputs==NULL) my_service->outputs=dupElements(current_element); else addToElements(&my_service->outputs,current_element); }else{ if(my_service->inputs==NULL) my_service->inputs=dupElements(current_element); else addToElements(&my_service->inputs,current_element); } freeElements(¤t_element); free(current_element); } plevel=level; current_element=(elements*)malloc(ELEMENTS_SIZE); current_element->name=zStrdup((char *)token.data.scalar.value); current_element->content=NULL; current_element->metadata=NULL; current_element->format=NULL; current_element->defaults=NULL; current_element->supported=NULL; current_element->next=NULL; } #ifdef DEBUG_YAML printf("scalar %s (%d,%d,%d,%d,%d)\n", token.data.scalar.value,blevel,level,plevel,ilevel,ttype); #endif break; /* Others */ default: if(token.type==0){ char tmp[1024]; sprintf(tmp,"Wrong charater found in %s: \\t",name); setMapInMaps(conf,"lenv","message",tmp); return -1; } #ifdef DEBUG_YAML printf("Got token of type %d\n", token.type); #endif break; } if(token.type != YAML_STREAM_END_TOKEN ) yaml_token_delete(&token); } while(token.type != YAML_STREAM_END_TOKEN); yaml_token_delete(&token); #ifdef DEBUG_YAML fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif if(current_element!=NULL && current_content!=NULL){ if(current_element->defaults==NULL){ current_element->defaults=(iotype*)malloc(IOTYPE_SIZE); current_element->defaults->content=NULL; current_element->defaults->next=NULL; addMapToMap(¤t_element->defaults->content,current_content); }else{ if(current_element->supported==NULL){ current_element->supported=(iotype*)malloc(IOTYPE_SIZE); current_element->supported->content=NULL; current_element->supported->next=NULL; addMapToMap(¤t_element->supported->content,current_content); }else addMapToIoType(¤t_element->supported,current_content); } #ifdef DEBUG_YAML dumpMap(current_content); fprintf(stderr,"MSG: %s %d \n",__FILE__,__LINE__); #endif freeMap(¤t_content); free(current_content); current_content=NULL; } if(current_element!=NULL){ if(my_service->outputs==NULL) my_service->outputs=dupElements(current_element); else addToElements(&my_service->outputs,current_element); freeElements(¤t_element); free(current_element); current_element=NULL; } /* END new code */ /* Cleanup */ yaml_parser_delete(&parser); fclose(fh); #ifdef DEBUG_YAML dumpService(my_service); #endif *service=my_service; return 1; }
static void SameTree()//PROBLEM 11 { int boolean = 1; int content; int length; int length2; int depth; int depth2; struct node *head = NULL; struct node *head2 = NULL; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head, content); } else { boolean = 0; } } boolean = 1; while(boolean) { if(yesno("Would you like to enter a value (Y/N)?")) { read_int("Please enter your value: ", &content); Append(&head2, content); } else { boolean = 0; } } if(head == NULL) { printf("The head tree is empty.\n"); if(head2 != NULL) { freeElements(&head2);//free up the allocated memory } return; } if(head2 == NULL) { printf("The head2 tree is empty.\n"); if(head != NULL) { freeElements(&head);//free up the allocated memory } return; } depth = Max_Depth(head); length = getLength(head); depth2 = Max_Depth(head2); length2 = getLength(head2); boolean = Same_Tree(head, head2, length, length2, depth, depth2); if(boolean == 1) { printf("The two trees are indentical.\n"); } else { printf("The two trees are different.\n"); } freeElements(&head);//free up the allocated memory if(head == NULL) { printf("The head node was set to NULL.\n"); } freeElements(&head2);//free up the allocated memory if(head == NULL) { printf("Head2 node was set to NULL.\n"); } }//END PROBLEM 11