Пример #1
0
// Performs Breadth-first search on the Graph G with the
// given source vertex s.
void BFS(Graph G, int s) {
   for(int i = 0; i < (G->order + 1); ++i) {
      G->parent[i] = NIL;
      G->distance[i] = INF;
      G->color[i] = WHITE;
   }
   G->source = s;
   G->distance[s] = 0;
   G->color[s] = GRAY;
   List Q = newList();
   prepend(Q, s);
   while(length(Q) > 0) {
      int ele = back(Q);
      deleteBack(Q);
      List adj = G->adj[ele];
      moveFront(adj);
      while(index(adj) > -1) {
         int v = get(adj);
         if(G->color[v] == WHITE) {
            G->color[v] = GRAY;
            G->parent[v] = ele;
            G->distance[v] = G->distance[ele] + 1;
            prepend(Q, v);
         }
         moveNext(adj);
      }
   }
   freeList(&Q); 
}
Пример #2
0
// clear()
// Re-sets this List to the empty state.
void clear(List L) {
   if (L != NULL) {
      while( length(L) > 0 ) {
         deleteBack(L);   
      }
   }
   L->front = L->back = L->cursor = NULL;
   L->cursor_inx = -1;
}
Пример #3
0
// freeList()
// Frees all heap memory associated with List *pL, and sets *pL to NULL.
void freeList(List* pL) {
   if (pL != NULL && *pL != NULL) {
      while( length(*pL) > 0 ) {
         deleteBack(*pL);   
      }
      free(*pL);
      *pL = NULL;
   }
}
Пример #4
0
/*freeList Function: this function frees the list by walking through the list
 *until the list is empty freeing each back end of the list and setting the
 *pointer to NULL. Finally the list pointer is freed and set to NULL as well
 */
void freeList(List* pL) {
    if (pL == NULL || *pL == NULL)
        return;
    while (!isEmpty(*pL)) {
        deleteBack(*pL);
    }
    free(*pL);
    *pL = NULL;
}
Пример #5
0
/*clear Function: this function walks through deleting the last element until
 *the list is empty
 */
void clear(List L) {
    
    //input validation
    if (L == NULL) {
        printf("Error: Cannot call clear() on NULL list L\n");
        exit(EXIT_FAILURE);
    } else if (isEmpty(L)) {
        printf("Error: Cannot call clear() on empty list L\n");
        exit(EXIT_FAILURE);
    }
    while (!isEmpty(L)) {
        deleteBack(L);
    }
}
Пример #6
0
/*
*  makeEmpty
*  Sets List to the empty state.
*  Post: isEmpty();
*/
void makeEmpty(ListRef L){
   if(!isEmpty(L)){
      while( !isEmpty(L)){ deleteBack(L); }
   }
} 
Пример #7
0
int main(int argc, char* argv[])
{
    int i;
    ListRef A = newList();
    ListRef B = newList();
	ListRef ACopy = NULL;
	ListRef AB_Cat = NULL;
	insertBack(A, 10);
	insertBack(A, 20);
	insertBack(A, 30);
	insertBack(A, 40);
	insertBack(A, 50);
	insertBack(A, 60);
	printf("equals(A,B)		: %d\n", equals(A, B));
	insertBack(B, 10);
	insertBack(B, 20);
	insertBack(B, 30);
	insertBack(B, 40);
	insertBack(B, 50);
	insertBack(B, 60);
	AB_Cat = catList(A, B);
	printf("printLIST(AB_Cat)	: ");
	printLIST(AB_Cat);
	ACopy = copyList(A);
	printf("printLIST(A)		: ");
	printLIST(A);
	printf("printLIST(ACopy)	: ");
	printLIST(ACopy);
	printf("equals(A,ACopy)		: %d\n", equals(A, ACopy));
	printf("equals(A,B)		: %d\n", equals(A, B));
	printf("printLIST(A)		: ");
	printLIST(A);
	moveTo(A, getLength(A));
	printf("offEnd(A)		: %d\n", offEnd(A));
	moveTo(A, 3);
	insertBeforeCurrent(A, 35);
	insertAfterCurrent(A, 45);
	printf("printLIST(A)		: ");
	printLIST(A);
	printf("getCurrent(A)		: %d\n", getCurrent(A));
	movePrev(A);
	printf("getCurrent(A)		: %d\n", getCurrent(A));
	deleteCurrent(A);
	printf("printLIST(A)		: ");
	printLIST(A);
	makeEmpty(B);
	deleteFront(A);
	printf("printLIST(A)		: ");
	printLIST(A);
	printf("getLength(A)		: %d\n", getLength(A));
	printf("isEmpty(A)		: %d\n", isEmpty(A));
	makeEmpty(A);
	printf("isEmpty(A)		: %d\n", isEmpty(A));
	printf("getLength(A)		: %d\n", getLength(A));
	/* printf("printLIST(A)		: ");
	printLIST(A); */
	insertFront(B, 50);
	insertBack(B, 60);
	insertFront(B, 40);
	insertBack(B, 70);
	insertFront(B, 30);
	insertBack(B, 80);
	insertFront(B, 20);
	insertBack(B, 90);
	insertFront(B, 10);
	printf("printLIST(B)		: ");
	printLIST(B);
	printf("offEnd(B)		: %d\n", offEnd(B));
	moveTo(B, 5);
	printf("offEnd(B)		: %d\n", offEnd(B));
	printf("getCurrent(B)		: %d\n", getCurrent(B));
	deleteCurrent(B);
	printf("printLIST(B)		: ");
	printLIST(B);
	/* printf("getCurrent(B) 	: %d\n", getCurrent(B));*/
	moveTo(B, 0);
	printf("getFront(B)		: %d\n", getFront(B));
	printf("getCurrent(B)		: %d\n", getCurrent(B));
	deleteFront(B);
	printf("printLIST(B)		: ");
	printLIST(B);
	printf("getFront(B)		: %d\n", getFront(B));
	/* printf("getCurrent(B)		: %d\n", getCurrent(B)); */
	moveTo(B, (getLength(B)-1));
	printf("getCurrent(B)		: %d\n", getCurrent(B));
	printf("getBack(B)		: %d\n", getBack(B));
	deleteBack(B);
	printf("getBack(B)		: %d\n", getBack(B));
	/* printf("getCurrent(B)		: %d\n", getCurrent(B)); */
	moveTo(B, (getLength(B)-1));
	printf("getCurrent(B)		: %d\n", getCurrent(B));
	printf("getBack(B)		: %d\n", getBack(B));
	deleteBack(B);
	printf("getBack(B)		: %d\n", getBack(B));
	printf("getCurrent(B)		: %d\n", getCurrent(B)); 
	
	
	
    return(0);
}
Пример #8
0
int main(int argc, char* argv[]){

// VARIABLE DECLARATION ///////////////////////////////////////////

    FILE *in, *out;
    char* token;
    char line[MAX_LEN];
    int x, y;
    Graph G, Gt;
    List stack = newList();

// ERROR CHECKING AND I/O PREP ////////////////////////////////////

    //verifies the correct number of arguments
     if(argc != 3){
        printf("Usage: %s <input file> <output file>\n", argv[0]);
        exit(1);
    }
    //opens the in file and out file
    in = fopen(argv[1], "r");
    out = fopen(argv[2], "w");
    //checks that they exist
    if(in == NULL)
    {
        printf("Unable to open file %s for reading\n", argv[1]);
        exit(1);
    }
    if(out == NULL)
    {
        printf("Unable to open file %s for writing\n", argv[2]);
        exit(1);
    }

// INPUT AND GRAPH CREATION ///////////////////////////////////////

     // get first line and store in x
    fgets(line, MAX_LEN, in);
    token = strtok(line, "\n");
    x = y = atoi(token);
    // create new graph of size x
    G = newGraph(x);

    // verify the 0 0 line hasn't been reached then get next line
    while(x != 0 && y != 0 && fgets(line, MAX_LEN, in) != NULL){
        // store first and second
        // numbers in x and y
        // respectively
        token = strtok(line, " ");
        x = atoi(token);
        token = strtok(NULL, " ");
        y = atoi(token);
        // add a new arc with
        // origin x and terminus y
        if( x != 0 && y != 0){
            addArc(G, x, y);
        }
    }
    // print out G's adjacency list representation
    fprintf(out, "Adjacency list representation of G:\n");
    printGraph(out, G);

// STRONGLY-CONNECTED-COMPONENTS ALGORITHM IMPLEMENTATION ///////////

    // Initialize the stack to contain the vertices
    // of G in ascending order
    for(int i = 1; i < getOrder(G); i++){
        append(stack, i);
    }
    // Run a depth first search on G using stack
    // as the processing order of the vertices,
    // after stack will hold the vertices sorted
    // in order of decreasing finish time
    DFS(G, stack);
    // Create a transpose graph of G
    Gt = transpose(G);
    // Run the depth first search again this time
    // on the transpose of G using the output
    // stack from the first call
    DFS(Gt, stack);
    // the resulting stack contains the strongly
    // connected components of G
    fprintf(out, "G contains %d strongly connected components:", getSCC(Gt));
    // for however many SCC's there are
    for(int i = 1; i <= getSCC(Gt); i++){
        // start at the bottom of the stack
        moveTo(stack, length(stack)-1);
        // move up until you reach a vertex with a
        // NIL parent, that is the root of a SCC
        while(Gt->parent[getElement(stack)] != NIL){
            movePrev(stack);
        }
        // print it as the first in its component list
        fprintf(out, "\nComponent %d: ", i);
        fprintf(out, "%d ", getElement(stack));
        moveNext(stack);
        // and move down the stack printing out vertices
        // until you reach the bottom
        while(getIndex(stack) >= 0){
            fprintf(out, "%d ", getElement(stack));
            moveNext(stack);
        }
        // now move back up the stack deleting as you
        // go until you reach the root of this SCC
        while(Gt->parent[back(stack)] != NIL){
            deleteBack(stack);
        }
        // and delete it
        deleteBack(stack);
    }

// CLEAN UP /////////////////////////////////////////////////////////

    fclose(out);
    fclose(in);

    freeList(&stack);
    stack = NULL;

    freeGraph(&G);
    freeGraph(&Gt);
    G = NULL;
    Gt = NULL;

    return (0);
}
Пример #9
0
int main(int argc, char* argv[]){
   
   
   FILE *out;

   out = fopen(argv[2], "w");



   List A = newList();
   List B = newList();
   List C = NULL;
   int i;

   for(i=1; i<=5; i++){
      append(A,i);
      prepend(B,i);
   }

   printList(out,A); 
   printf("\n");
   printList(stdout,B); 
   printf("\n");

   for(moveTo(A,0); getIndex(A)>=0; moveNext(A)){
      printf("%d ", getElement(A));
   }
   printf("\n");
   for(moveTo(B,length(B)-1); getIndex(B)>=0; movePrev(B)){
      printf("%d ", getElement(B));
   }
   printf("\n");

   C = copyList(A);
   printf("%s\n", equals(A,B)?"true":"false");
   printf("%s\n", equals(B,C)?"true":"false");
   printf("%s\n", equals(C,A)?"true":"false");

   printf("A's front = %d\n",    front(A));
   printf("A's back = %d\n",    back(A));
   deleteFront(A);
   deleteBack(A);
   printf("A's front = %d\n",    front(A));
   printf("A's back = %d\n",    back(A));

   moveTo(A,4);
   insertBefore(A,-1);
   moveTo(A,3);
   insertAfter(A,-2);
   moveTo(A,2);
   delete(A);
   printList(stdout,A);
   printf("\n");
   // printf("%d\n", length(A));

   clear(A);
   printf("%d\n", length(A));

   printf("entering A \n");
   freeList(&A);

   //clear(B);

   printf("A free, entering B \n");
   freeList(&B);

   //clear(C);

   printf("A/B free, entering C \n");
   freeList(&C);

   fclose(out);

   return(0);
}