Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.50 KB | None | 0 0
  1. int read_graph(FILE ** graphIn,FILE ** heurIn,Node ** newNode,int ** nodeList)
  2. {
  3.     int newCur,newNext,newCost,tempCur,tempNext,tempCost;
  4.     int arlength=1; //array length for Node List or total number of different nodes
  5.     int tempIndex=-1; // index for findNode()
  6.        
  7.     *newNode=(Node *)malloc(arlength*sizeof(Node)); //create the first Node (array size 1)
  8.     fscanf(*graphIn,"%d %d %d",&tempCur,&tempNext,&tempCost); //scan details for the first node
  9.    
  10.     arlength++; //since the first node has a destination / next node, then the number of nodes will be increased by 1
  11.    
  12.     (*newNode)[0].current=tempCur; //store the node name to the node structure array
  13.     (*nodeList)[0]=tempCur; //store the node name to the node List array
  14.     (*newNode)[0].branch=1; //add 1 branch option
  15. printf("%d %d %d\n",(*newNode)[0].current,(*nodeList)[0],(*newNode)[0].branch);
  16.     (*newNode)[0].nextPos=(int *)malloc((*newNode)[0].branch*sizeof(int)); //allocate memory for the branches
  17.     (*newNode)[0].cost=(int *)malloc((*newNode)[0].branch*sizeof(int)); //allocate memory for the branches cost
  18.     *newNode=realloc(*newNode,arlength*sizeof(Node)); //expand the node structure array by 1 element
  19.     *nodeList=realloc((*nodeList),arlength*sizeof(int)); //expand the node List array by 1 element
  20.  
  21.     (*newNode)[1].current=tempNext; //put the first "next" or branch option as a new element in the structure array.
  22.     (*nodeList)[1]=tempNext; //put the first "next" or branch option as a new element in the node list array.
  23. printf("%d %d\n",(*newNode)[1].current,(*nodeList)[1]);
  24.     (*newNode)[0].nextPos[((*newNode)[0].branch)-1]=tempNext;; // store the branch
  25.     (*newNode)[0].cost[((*newNode)[0].branch)-1]=tempCost; //store the cost of the branch
  26. printf("%d %d\n",(*newNode)[0].nextPos[((*newNode)[0].branch)-1],(*newNode)[0].cost[((*newNode)[0].branch)-1]);
  27.        
  28.     while(!(feof(*graphIn))) // while the end of file is not reached
  29.     {
  30.     fscanf(*graphIn,"%d %d %d",&newCur,&newNext,&newCost);
  31.    
  32.     if((newCur!=tempCur)&&(!(isExist(newCur,*nodeList,arlength)))) //when the new Node doesn't exist in the list
  33.         {
  34.         arlength++; //increase the number of Nodes
  35.         *newNode=realloc(*newNode,arlength*sizeof(Node)); //expand the structure array
  36.         *nodeList=realloc((*nodeList),arlength*sizeof(int)); //expand the List array
  37.        
  38.         (*nodeList)[arlength-1]=newCur; //store the new node to the newly expanded element of the List array
  39.         (*newNode)[arlength-1].current=newCur; //store the new node to the newly expanded element of the structure array
  40.         (*newNode)[arlength-1].branch=1; // add 1 branch option
  41. printf("%d %d %d\n",(*nodeList)[arlength-1],(*newNode)[arlength-1].current,(*newNode)[arlength-1].branch);
  42.         (*newNode)[arlength-1].nextPos=(int *)malloc(((*newNode)[arlength-1].branch)*sizeof(int));//allocate memory for the branch
  43.         (*newNode)[arlength-1].cost=(int *)malloc(((*newNode)[arlength-1].branch)*sizeof(int));//allocate memory for the branch cost
  44.        
  45.         (*newNode)[arlength-1].nextPos[((*newNode)[arlength-1].branch)-1]=newNext;
  46.         (*newNode)[arlength-1].cost[((*newNode)[arlength-1].branch)-1]=newCost;
  47. printf("%d %d\n",(*newNode)[arlength-1].nextPos[((*newNode)[arlength-1].branch)-1],(*newNode)[arlength-1].cost[((*newNode)[arlength-1].branch)-1]);
  48.         if(!(isExist(newNext,*nodeList,arlength)))
  49.             {  
  50.             arlength++;
  51.             *newNode=realloc(*newNode,arlength*sizeof(Node));
  52.             *nodeList=realloc((*nodeList),arlength*sizeof(int));
  53.            
  54.             (*nodeList)[arlength-1]=newNext;
  55.             (*newNode)[arlength-1].current=newNext;
  56. printf("%d %d\n",(*nodeList)[arlength-1],(*newNode)[arlength-1].current);
  57.             }
  58.         tempCur=newCur;
  59.         tempNext=newNext;
  60.         tempCost=newCost;
  61.         }
  62.     else if((newCur==tempCur)&&(isExist(newCur,*nodeList,arlength))) // checking if the node exists in the list
  63.         {
  64.         tempIndex=findNode(newCur,*nodeList,arlength); //find where the node is
  65.        
  66.         (*newNode)[tempIndex].branch++;
  67.        
  68.         (*newNode)[tempIndex].nextPos=realloc((*newNode)[tempIndex].nextPos,((*newNode)[tempIndex].branch)*sizeof(Node));
  69.         (*newNode)[tempIndex].cost=realloc((*newNode)[tempIndex].cost,((*newNode)[tempIndex].branch)*sizeof(Node));
  70.         (*newNode)[tempIndex].nextPos[((*newNode)[tempIndex].branch)-1]=newNext;
  71.         (*newNode)[tempIndex].cost[((*newNode)[tempIndex].branch)-1]=newCost;
  72. printf("%d %d\n",(*newNode)[tempIndex].nextPos[((*newNode)[tempIndex].branch)-1],(*newNode)[tempIndex].cost[((*newNode)[tempIndex].branch)-1]);    
  73.         tempCur=newCur;
  74.         tempNext=newNext;
  75.         tempCost=newCost;
  76.         }
  77.     }
  78. printf("final check %d %d %d\n",(*newNode)[5].current,(*newNode)[5].nextPos[0],(*newNode)[5].cost[0]);
  79. return arlength;   
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement