Beispiel #1
0
int main(){
	struct cirListDeque* q = createCirListDeque(); 
	addBackCirListDeque(q, (TYPE)1);
	addBackCirListDeque(q, (TYPE)2);
	addBackCirListDeque(q, (TYPE)3);
	addFrontCirListDeque(q, (TYPE)4);
	addFrontCirListDeque(q, (TYPE)5);
	addFrontCirListDeque(q, (TYPE)6);
	printCirListDeque(q);
	printf("\n");
	//6, 5, 4, 1, 2, 3
	printf("%g\n", frontCirListDeque(q));
	printf("\n");
	//6
	printf("%g\n", backCirListDeque(q));
	printf("\n");
	//3
	removeFrontCirListDeque(q);
	//5, 4, 1, 2, 3
	removeBackCirListDeque(q);
	//5, 4, 1, 2
	printCirListDeque(q);
	printf("\n");
	//5, 4, 1, 2
	reverseCirListDeque(q);
	//2, 1, 4, 5
	printCirListDeque(q);
	printf("\n");
	//2, 1, 4, 5
	deleteCirListDeque(q);
	return 0;
}
/* De-allocate all links of the deque

	param: 	q		pointer to the deque
	pre:	none
	post:	All links (including Sentinel) are de-allocated
*/
void freeCirListDeque(struct cirListDeque *q)
{
	/* DONE: you must write this */	 
	assert(q != 0);

	while (q->size > 0){
		removeBackCirListDeque(q);
	}
}
Beispiel #3
0
int main(){
    struct cirListDeque* q = createCirListDeque();
    addBackCirListDeque(q, (TYPE)1);
    addBackCirListDeque(q, (TYPE)2);
    addBackCirListDeque(q, (TYPE)3);
    addFrontCirListDeque(q, (TYPE)4);
    addFrontCirListDeque(q, (TYPE)5);
    addFrontCirListDeque(q, (TYPE)6);
    printCirListDeque(q);
    printf("%g\n", frontCirListDeque(q));
    printf("%g\n", backCirListDeque(q));
    removeFrontCirListDeque(q);
    removeBackCirListDeque(q);
    printCirListDeque(q);
    reverseCirListDeque(q);
    printCirListDeque(q);
    return 0;
}
Beispiel #4
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int BFS(Graph* g, Vertex* source, Vertex* destination){
	/* FIXME you will write this */

	/* create and initialize circular list deque pointer */
    struct cirListDeque *stack = malloc(sizeof(struct cirListDeque));
    initCirListDeque(stack);

    /*/ create vertex pointer and clear visited nodes */
    struct Vertex *temp = source;
    clearVisited(g);

    /* add to front */
    addFrontCirListDeque(stack, temp);

    /* if not empty, use BFS */
    while(!isEmptyCirListDeque(stack)){

        /*remove back(top) value */
        temp = backCirListDeque(stack);
        removeBackCirListDeque(stack);

        /* check if visited, mark, and return true  */
        if(!temp -> isVisited){
            temp -> isVisited = 1;
        }
        if(temp == destination){

            removeAllCirListDeque(stack);
            return 1;
        }

        /* check is the neighboring nodes have been visited. add to stack if not */
        for(int i = 0; i < temp->numNeighbors; i++){
            if(!temp->neighbors[i]->isVisited){
                addFrontCirListDeque(stack, temp->neighbors[i]);
            }
        }
    }

    return 0;

}
Beispiel #5
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int DFS(Graph* g, Vertex* source, Vertex* destination){
	/* FIXME you will write this */

	struct cirListDeque *stack = malloc(sizeof(struct cirListDeque));
	initCirListDeque(stack);

	struct Vertex *temp = source;
    clearVisited(g);

	/* add to the front of the circular list deque */
	addFrontCirListDeque(stack, temp);

	/*if it isn't empty, use DFS */
	while(!isEmptyCirListDeque(stack)){

        temp = backCirListDeque(stack);
        removeBackCirListDeque(stack);

        /* check if visited, mark if it hasn't been visited */
        if(!temp -> isVisited){
            temp ->isVisited = 1;
        }

        if(temp == destination){

            /* if you reach the destination, remove from the list to free memory, return 1 */
            removeAllCirListDeque(stack);
            return 1;
        }

        /* check is the neighboring nodes have been visited. add to stack if not */
        for(int i = 0; i < temp->numNeighbors; i++){
            if(!temp->neighbors[i] -> isVisited){
                addFrontCirListDeque(stack, temp->neighbors[i]);
            }
        }
	}

    return 0;
}
int main () {
	struct cirListDeque testDeque;

	printf("Testing Deque ADT based on Circularly-Doubly-Linked List \n");
	/*Initialize the deque */
	initCirListDeque(&testDeque);
	/*Add 3 to the back */
	addBackCirListDeque(&testDeque, 3);
	/*Add 4 to the back */
	addBackCirListDeque(&testDeque, 4);
	/*Add 5 to the back */
	addBackCirListDeque(&testDeque, 5);

	/*Should print 3.0  4.0  5.0 */
	printCirListDeque(&testDeque);
	
	/*Add 2 to the front */
	addFrontCirListDeque(&testDeque, 2);
	/*Add 1 to the front */
	addFrontCirListDeque(&testDeque, 1);

	/*Should print 1.0, 2.0, 3.0,  4.0,  5.0 */
	printCirListDeque(&testDeque);

	/*Should print front: 5.0; back: 1.0 */
	printf("front: %g; back: %g\n\n", frontCirListDeque(&testDeque), backCirListDeque(&testDeque));
	/*Remove the back of the deque */
	removeBackCirListDeque(&testDeque);
	/*Should print front: 5.0; back: 2.0 */
	printf("front: %g; back: %g\n\n", frontCirListDeque(&testDeque), backCirListDeque(&testDeque));
	/*Remove the front of the deque */
	removeFrontCirListDeque(&testDeque);
	/*Should print front: 4.0; back: 2.0 */
	printf("front: %g; back: %g\n\n", frontCirListDeque(&testDeque), backCirListDeque(&testDeque));
	freeCirListDeque(&testDeque);

	return EXIT_SUCCESS;
}
Beispiel #7
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int BFS(Graph* g, Vertex* source, Vertex* destination)
{
	/* DONE you will write this */
    /* use a queue */
    clearVisited(g);
    cirListDeque *queue = malloc(sizeof(cirListDeque));
    
    initCirListDeque(queue);
    
    Vertex *current = source;
    int i;
    for (i = 0; i < current->numNeighbors; i++) {
        addFrontCirListDeque(queue, current->neighbors[i]);
    }
    current->isVisited = 1;
    
    
    while (! isEmptyCirListDeque(queue)) {
        current = backCirListDeque(queue);
        removeBackCirListDeque(queue);
        if (current == destination) {
            return 1;
        }
        current->isVisited = 1;
        
        
        for (i = 0; i < current->numNeighbors; i++) {
            if (! current->neighbors[i]->isVisited) {
                addFrontCirListDeque(queue, current->neighbors[i]);
            }
            
        }
        
    }
    
	return 0;
}
int main()
{
	struct cirListDeque* q = createCirListDeque(); 
	addBackCirListDeque(q, (TYPE)1);				//adds to back of deque 3 times
	addBackCirListDeque(q, (TYPE)2);
	addBackCirListDeque(q, (TYPE)3);
	addFrontCirListDeque(q, (TYPE)4);				//adds to front of deque 3 times
	addFrontCirListDeque(q, (TYPE)5);
	addFrontCirListDeque(q, (TYPE)6);				// should end up being 6 5 4 1 2 3
	printf("Printing list.\n");
	printCirListDeque(q);
	printf("\nDisplaying front and back of list.");
	printf("\nFront: %g", frontCirListDeque(q));
	printf("\nBack: %g\n", backCirListDeque(q));
	removeFrontCirListDeque(q);						// removes 6
	removeBackCirListDeque(q);						// removes 3
	printf("\nList with front and back removed.\n");
	printCirListDeque(q);
	printf("\nList will get reversed.\n");
	reverseCirListDeque(q);							// reverses deque should be 2 1 4 5
	printCirListDeque(q);
	printf("\n");
	return 0;
}
Beispiel #9
0
/* This function should return a boolean (encoded as an int) indicating
 * whether or not a path exists between the argument vertices.
 * param: g			Graph to perform the search in
 * param: source	Vertex to originate the search from
 * param: destination Vertex to stop the search from (if it is found)
 * ret: boolean indicating whether or not a path exists
 */
int DFS(Graph* g, Vertex* source, Vertex* destination)
{
  /* FIXME you will write this */
  /* DFSRecursive (g, source, destination) */   

  assert( g != NULL );
  assert( source != NULL );
  assert( destination != NULL );

  clearVisited(g);
  
  /* printf("Searching for %c from %c\n",destination->label,source->label); */

  cirListDeque stack;  /* a stack can be a special instance of deque */
  /* can't use a pointer for 'stack' because there's nothing for it to point at */

  Vertex* current;

  int i;  /* C99 isn't included on this assignment for some reason  */
 
  initCirListDeque(&stack); 
  
  addBackCirListDeque(&stack,source);   /* push vertex source to top of stack */
  /* printf("pushed %c\n",source->label); */

  while( !isEmptyCirListDeque(&stack) )  /* loop until &stack is empty */
    {
      /* printf("(SOL) current stack: "); */
      /* printCirListDeque(&stack);  */
      /* printf("\n"); */

      current = backCirListDeque(&stack);  /* pop vertex from &stack */
      removeBackCirListDeque(&stack);
      
      /* printf("removed %c\n", current->label); */
      /* printf("current->label = %c\n",current->label); */

      if ( current->label == destination->label )
	{
	  /* printf("found %c\n",destination->label); */
	  return 1;
	}
      
      else
	{
	  if ( current->isVisited == 0 )  /* mark as visited if not already */
	    {
	      current->isVisited = 1;
	      /* printf("marked %c as visited\n",current->label); */
	    }
	  else
	    {
	      /* printf("Skipping %c because it visited already.\n",current->label); */
	    }
	  /* printf("checking %d neighbors\n",current->numNeighbors);	   */
	  for  (  i = 0; i < current->numNeighbors; i++ )  /* push neighbors if necessary */
	    {
	      if ( current->label == destination->label )
		{
		  /* printf("found %c\n",destination->label); */
		  return 1;
		}
	      else
		{
		  if ( current->neighbors[i]->isVisited == 0 )
		    {
		      addBackCirListDeque( &stack,current->neighbors[i] );
		      /* printf("pushed %c\n",current->neighbors[i]->label); */
		    }
		  /* else  */
		    /* printf("Skipping %c because it visited already.\n",current->neighbors[i]->label); */
		}
	    }
	}

    }

  return 0;
}
Beispiel #10
0
/* Remove all links of the deque

	param: 	q		pointer to the deque
	pre:	q is not null
	post:	q is empty
*/
void removeAllCirListDeque(struct cirListDeque *q)
{
	while(!isEmptyCirListDeque(q))
		removeBackCirListDeque(q);
}