Beispiel #1
0
void doBFS(GraphRef g, int source){
	ListRef list = newList();
	initGraph(g);
	insertAfterLast(list, source);
	g->color[source]= 1;
	g->distance[source]=0;
	while(!isEmpty(list)){
		int current = getFirst(list);
		if(current > g->numVertices){
			deleteFirst(list);
			 break;
		}
		ListRef edgeList = g->vertices[current];
		moveFirst(edgeList);
		while(!offEnd(edgeList)){
			int edge = getCurrent(edgeList);
			if(g->color[edge]==0){
				g->distance[edge] = g->distance[current]+1;
				g->color[edge] = 1;
				g->parent[edge] = current;
				insertAfterLast(list, edge);
			}
			moveNext(edgeList);
		}
		deleteFirst(list);
		g->color[current] = 2;
	}
	makeEmpty(list);
	freeList(list);
}
Beispiel #2
0
ListRef copyList(ListRef L) {
    ListRef nlist = newList();
    NodeRef n = L->first;
    
    while (n != NULL) {
        insertAfterLast(nlist, n->data);
        n = n->next;
    }
    return (nlist);
}
Beispiel #3
0
// returns a list that has all the elements of one list given
// that are copied onto this new list
ListRef copyList(ListRef L) {
    ListRef copyList=newList();
    int count =0;
    NodeType *node = malloc(sizeof(struct Node));
    if(!isEmpty(L)&&count==0) {
        node = L->first;
        while(node != NULL) {
            insertAfterLast(copyList,node->data);
            node= node->next;
        }
        return copyList;

    }
}
Beispiel #4
0
// inserts a node in a list after the current node
// the new node must have a pointer prev to current
// and point the previous to the next of the current of the node
void insertAfterCurrent(ListRef L, long data) {
    NodeType *node = malloc(sizeof(struct Node));
    node ->data = data;
    if(!offEnd(L)) {
        if(L->current!=L->last) {
            node->prev = L->current;
            node->next= L->current->next;
            L->current->next->prev =node;
            L->current->next = node;
            L->current = node;
        }
        else insertAfterLast(L, data);

    }
}
Beispiel #5
0
/*
 * insert after the current node, if defined
 */
void insertAfterCurrent(ListRef L, long data)
{
    NodeRef n;
    if (L == NULL) {
        printf( "Error: Called insertAfterCurrent() on NULL ListRef\n");
        //exit(EXIT_FAILURE);
    } else if (isEmpty(L)) {
        printf( "Error: Called insertAfterCurrent() on empty ListRef\n");
        //exit(EXIT_FAILURE);
    }
    
    if (atLast(L)) {
        insertAfterLast(L, data);
    } else {
        n = newNode(data);
        n->next = L->current->next;
        n->prev = L->current;
        L->current->next = n;
        n->next->prev = n;
        L->length++;
    }
}
Beispiel #6
0
int main (int argc, char **argv){
   int EXIT_STATUS = 0;
   
   FILE *info = fopen (argv[1], "r");  //open file for scanning
   if(info == NULL){  //check for valid filename, if not exit with status 1
      fflush(NULL);
      fprintf(stderr,"Invalid file name!\n");
      fflush(NULL);
      EXIT_STATUS = 1;
      exit(EXIT_STATUS);
   }

   long numcustomers;  //variable for # of customers
   long numpurchases;  //variable for # of purchases
   long hashsize;
   fscanf(info, "%ld", &numcustomers);  //scan in first number as # customers
   fscanf(info, "%ld", &numpurchases);  //scan in next number as # customers
   fscanf(info, "%ld", &hashsize);

   hashRef bookhash = newHash(hashsize);

   ListRef customers[numcustomers+1];  //create array that holds list for each customer

   for(int i = 1; i<numcustomers+1; i++){  //create a list for each customer
      customers[i] = newList();
   }

   long bookid = 0;  //variables for custid and bookid
   long custid = 0;
   int printrecbool = 0;
   int check = 0;    //variable to check for EOF

   fscanf(info, "%ld %ld %d", &custid, &bookid, &printrecbool);  //initial scan of custID and bookID
   while(check != EOF){  //loop until we reach end of file
      insertAfterLast(customers[custid], bookid);  //add bookID to appropriate customer list
      insertBookList(bookhash, bookid);            //inserts book into the hash

      for(NodeRef current = customers[custid]->first; current != NULL; current = current->next){
         if(current->data != bookid){
            int index = hashKey(bookhash, bookid);
            bookNodeRef temp = findBook(bookhash->purchasedBooks[index], bookid);
            insertAfterLast(temp->copurchases, current->data);

            index = hashKey(bookhash, current->data);
            temp = findBook(bookhash->purchasedBooks[index], current->data);
            insertAfterLast(temp->copurchases, bookid);
         }
      }


      if(printrecbool){
         int index = hashKey(bookhash, bookid);
         bookNodeRef temp = findBook(bookhash->purchasedBooks[index], bookid);
         NodeRef mostfreq = highestCount(temp->copurchases, customers[custid]);
         printf("Customers buying book %ld also purchased book %ld\n", bookid, mostfreq->data);
      }
      check = fscanf(info, "%ld %ld %d", &custid, &bookid, &printrecbool);  //scan in next custID and bookID
   }



   fclose(info);

   return EXIT_STATUS;
}
Beispiel #7
0
void addEdge(GraphRef g, int from, int to){
	insertAfterLast(g->vertices[from],  to);
	++g->numEdges;
}