Esempio n. 1
0
int main(int argc, char* argv[]){
   
   List A = newList();
   List B = newList();
   List C = NULL;
   int i;

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

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

   for(moveFront(A); index(A)>=0; moveNext(A)){
      printf("%d ", get(A));
   }
   printf("\n");
   for(moveBack(B); index(B)>=0; movePrev(B)){
      printf("%d ", get(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");


   moveFront(A);
   for(i=0; i<5; i++) moveNext(A); // at index 5
   insertBefore(A, -1);            // at index 6
   for(i=0; i<9; i++) moveNext(A); // at index 15
   insertAfter(A, -2);
   for(i=0; i<5; i++) movePrev(A); // at index 10
   delete(A);
   printList(stdout,A);
   printf("\n");
   printf("%d\n", length(A));
   clear(A);
   printf("%d\n", length(A));

   freeList(&A);
   freeList(&B);
   freeList(&C);

   return(0);
}
Esempio n. 2
0
void BFS(Graph G, int s){
    if( G==NULL ){
        printf("Graph Error: cannot run BFS() on NULL Graph pointer");
        exit(1);
    }
    if( 1>s || s>getOrder(G) ){
        printf("Graph Error: vertex s is not in |V(G)|");
        exit(1);
    }

    G->source = s;
    int i, x, y;
    for( i=1; i<=getOrder(G); i++){
        G->color[i] = WHITE;
        G->dist[i]  = INF;
        G->pred[i]  = NIL;
    }
    G->color[s] = GRAY;
    G->dist[s]  = 0;
    G->pred[s]  = NIL;
    List Q      = newList();


    append(Q, s);
    moveFront(Q);

    while( getindex(Q)!=-1 ){

        x = get(Q);
        moveFront(G->adj[x]);

        while( getindex(G->adj[x])!=-1 ){

            y = get(G->adj[x]);
            if( G->color[y]==WHITE ){
                G->color[y] = GRAY;
                G->dist[y]  = G->dist[x] + 1;
                G->pred[y]  = x;
                append(Q, y);
            }
            moveNext(G->adj[x]);
        }

        G->color[x] = BLACK;
        moveNext(Q);
    }
    freeList(&Q);
}
Esempio n. 3
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); 
}
Esempio n. 4
0
void Rover::MoveRover(int startX, int startY, DIRECTION currentDirection, string moveCommand)
{
	currentPosition.x = startX;
	currentPosition.y = startY;
	directionFacing = currentDirection;
	
	
	for(char command = 0; command < moveCommand.length(); command++)
	{
		switch(moveCommand[command])
		{
			case 'f': moveFront();
                break;
                
			case 'b': moveBack();
                break;
                
			case 'l':turnLeft();
                break;
                
			case 'r':turnRight();
                break;
                
			default:
                break;
                
		}
	}
    
}
Esempio n. 5
0
void SphereCamera::mouseMoved(QMouseEvent *event)
{
	const double x = event->x();
	const double y = event->y();

	// Rotation handler
	if (event->buttons() & Qt::RightButton) {
		if (y - _yOld < 0) {
			moveDown(_mouseSensitivity*std::abs(y - _yOld));
		}
		else if (y - _yOld > 0) {
			moveUp(_mouseSensitivity*std::abs(y - _yOld));
		}
		if (x - _xOld < 0) {
			moveRight(_mouseSensitivity*std::abs(x - _xOld));
		}
		else if (x - _xOld > 0) {
			moveLeft(_mouseSensitivity*std::abs(x - _xOld));
		}
	}
	// Zoom handler
	if (event->buttons() & Qt::MiddleButton) {
		if (y - _yOld < 0) {
			moveFront(_mouseSensitivity*std::abs(y - _yOld));			// Zoom +
		}
		else if (y - _yOld > 0) {
			moveBack(_mouseSensitivity*std::abs(y - _yOld));				// Zoom -
		}
	}
	_xOld = x;
	_yOld = y;
}
Esempio n. 6
0
void bimWorld::MovingCamera::moveVertical(double distance)
{

	auto manip = static_cast<PersonManipulator*>(m_host->_CameraManipulator()->getCameraManipulator());
	if (!manip)
		return;

	manip->moveFront(distance);
	m_host->_RenderingThreads()->updateSeveralTimes();

}
Esempio n. 7
0
// Adds a directed edge to the Graph G from u to v
// Pre: 1 <= u <= getOrder(G), 1 <= v <= getOrder(G)
void addArc(Graph G, int u, int v) {
   if(u < 1 || u > getOrder(G) || v < 1 || v > getOrder(G)) {
     printf("Graph Error: calling addArc() with verticies out of bounds\n");
     exit(1); 
   }
   List S = G->adj[u];
   moveFront(S);
   while(index(S) > -1 && v > get(S)) {
      moveNext(S);
   }
   if(index(S) == -1)
      append(S, v);
   else 
      insertBefore(S, v);
   G->size++;
}
Esempio n. 8
0
// private helper function for addEdge()
// inserts vertex by vertex label sorted order
static void insertVertex(List L, int x){
    if( L==NULL ){
        printf("Graph Error: cannot insert vertex into NULL referenced List");
        exit(1);
    }

    moveFront(L);
    while( getindex(L)!=-1 ){
        int temp = get(L);
        if( temp<x ) moveNext(L);
        else {
            insertBefore(L, x);
            break;
        }
    }
    if( getindex(L)==-1 ) append(L, x);
}
Esempio n. 9
0
int main (int argc, char** argv) {
   if (argc != 3) {
      printf ("usage: FindComponents infile outfile\n");
      exit (1);
   }
   int lines = 0;
   char buf[MAXLINE];
   FILE* count = fopen (argv[1], "r");
   FILE* read  = fopen (argv[1], "r");
   FILE* out   = fopen (argv[2], "w");
   if (count == NULL || read == NULL) {
      printf ("Unable to open %s for reading\n", argv[1]);
      exit (1);
   }
   if (out == NULL) {
      printf ("Unable to open %s for writing", argv[2]);
      exit (1);
   }
   while (fgets (buf, MAXLINE, count) != NULL) ++lines;
   char** linebuf = malloc (sizeof (char*) * lines);
   for (int i = 0; i < lines; ++i) {
      char line[MAXLINE];
      fgets (line, MAXLINE, read);
      linebuf[i] = malloc (sizeof (line) + 1);
      strcpy (linebuf[i], line);
      strtok (linebuf[i], "\n");
   }
   Graph dfsg = newGraph (atoi (linebuf[0]));
   process (dfsg, linebuf);
   Graph tran = transpose (dfsg);
   List vertlist = newList();
   for (int i = 1; i <= getOrder (dfsg); ++i) append (vertlist, i);
   DFS (dfsg, vertlist);
   DFS (tran, vertlist);

   int strong = 0, strong_;
   for (int i = 1; i <= getOrder (tran); ++i)
      if (getParent (tran, i) == NIL) ++strong;
   strong_ = strong;
   List comps[strong];
   fprintf (out, "Adjacency list representation of G:\n");
   printGraph (out, dfsg);
   fprintf (out, "\n");
   fprintf (out, "G contains %d strongly connected components:\n", strong);
   for (int i = 0; i < strong; ++i) comps[i] = newList();
   for (moveFront (vertlist); index (vertlist) >= 0; moveNext (vertlist)) {
      if (getParent (tran, get (vertlist)) == NIL) --strong;
      append (comps[strong], get (vertlist));
   }
   for (int i = 1; i <= strong_; i++) {
      fprintf (out, "Component %d: ", i);
      printList (out, comps[i - 1]);
      fprintf (out, "\n");
   }
   for (int i = 0; i < strong_; i++) freeList (&comps[i]);
   for (int i = 0; i < lines; i++) free (linebuf[i]);
   free (linebuf);
   freeGraph (&dfsg);
   freeGraph (&tran);
   freeList (&vertlist);
   fclose (out);
   fclose (read);
   fclose (count);
   return EXIT_SUCCESS;
}
Esempio n. 10
0
int main(int argc, char* argv[]){
   int i, s, max, min, d, n=35;
   List  C = newList(); // central vertices 
   List  P = newList(); // peripheral vertices 
   List  E = newList(); // eccentricities 
   Graph G = NULL;

   // Build graph G 
   G = newGraph(n);
   for(i=1; i<n; i++){
      if( i%7!=0 ) addEdge(G, i, i+1);
      if( i<=28  ) addEdge(G, i, i+7);
   }
   addEdge(G, 9, 31);
   addEdge(G, 17, 13);
   addEdge(G, 14, 33);

   // Print adjacency list representation of G
   printGraph(stdout, G);

   // Calculate the eccentricity of each vertex 
   for(s=1; s<=n; s++){
      BFS(G, s);
      max = getDist(G, 1);
      for(i=2; i<=n; i++){
         d = getDist(G, i);
         max = ( max<d ? d : max );
      }
      append(E, max);
   }

   // Determine the Radius and Diameter of G, as well as the Central and 
   // Peripheral vertices.
   append(C, 1);
   append(P, 1);
   min = max = front(E);
   moveFront(E);
   moveNext(E);
   for(i=2; i<=n; i++){
      d = get(E);
      if( d==min ){
         append(C, i);
      }else if( d<min ){
         min = d;
         clear(C);
         append(C, i);
      }
      if( d==max ){
         append(P, i);
      }else if( d>max ){
         max = d;
         clear(P);
         append(P, i);
      }
      moveNext(E);
   }

   // Print results 
   printf("\n");
   printf("Radius = %d\n", min);
   printf("Central vert%s: ", length(C)==1?"ex":"ices");
   printList(stdout, C);
   printf("\n");
   printf("Diameter = %d\n", max);
   printf("Peripheral vert%s: ", length(P)==1?"ex":"ices");
   printList(stdout, P);
   printf("\n");

   // Free objects 
   freeList(&C);
   freeList(&P);
   freeList(&E);
   freeGraph(&G);

   return(0);
}