示例#1
0
// Create a graph from an input file. The format for the input file is specified
// in the graph.h header file
Graph* createGraph(FILE *inputFile)
{
  Graph *g = myMalloc(sizeof(Graph));
  g->numVertices = countLines(inputFile);
  g->numEdges = (countNumWords(inputFile)-(g->numVertices))/2;
  // init vertices
  g->vertices = myMalloc(sizeof(Vertex) * g->numVertices);
  for(int i = 0; i < g->numVertices; i++){
    Vertex *vp = g->vertices+i;
    vp->label = i+1;
    vp->head = NULL;
    vp->tail = NULL;
  }
  // init edges
  g->edges = myMalloc(sizeof(Edge) * g->numEdges);
  for(int i = 0; i < g->numEdges; i++){
    Edge *ep = g->edges+i;
    ep->endpoint1 = NULL;
    ep->endpoint2 = NULL;
  }
  //init connector elements
  g->connectors = myMalloc(sizeof(ConnectorElement) * g->numEdges * 2);
  for(int i = 0; i < g->numEdges*2; i++){
    ConnectorElement *connector = g->connectors+i;
    connector->adjacentEdge=NULL;
    connector->sourceVertex=NULL;
    connector->prev=NULL;
    connector->next=NULL;
  }
  hookupGraph(g, inputFile);
  rewind(inputFile);

  return g;
}
示例#2
0
int main(int argc, char* argv[]){
  char str[100];
  printf("Enter String : ");
  scanf(" %[^\n]", str);
  printf("Given String : %s \n", str);
  printf("Numer of words = %d \n", countNumWords(str));
  return 0;
}
示例#3
0
文件: test.c 项目: volunteers/test
 void testcountNumWords()
 {
   printf("word#=%d\n", countNumWords(" a b "));
 }