Ejemplo n.º 1
0
void main()
{
  int k,n;
  AMLGraph g;
  VertexType v1,v2;
  CreateGraph(&g);
  Display(g);
  printf("修改顶点的值,请输入原值 新值: ");
  scanf("%s%s",v1,v2);
  PutVex(&g,v1,v2);
  printf("插入新顶点,请输入顶点的值: ");
  scanf("%s",v1);
  InsertVex(&g,v1);
  printf("插入与新顶点有关的边,请输入边数: ");
  scanf("%d",&n);
  for(k=0;k<n;k++)
  {
    printf("请输入另一顶点的值: ");
    scanf("%s",v2);
    InsertArc(&g,v1,v2);
  }
  Display(g);
  printf("深度优先搜索的结果:\n");
  DFSTraverse(g,visit);
  printf("广度优先搜索的结果:\n");
  BFSTraverse(g,visit);
  DestroyGraph(&g);
}
Ejemplo n.º 2
0
 void main()
 {
   int i,j,k,n;
   MGraph g;
   VertexType v1,v2;
   printf("请顺序选择有向图,有向网,无向图,无向网\n");
   for(i=0;i<4;i++) // 验证4种情况
   {
     CreateGraph(g); // 构造图g
     Display(g); // 输出图g
     printf("插入新顶点,请输入顶点的值: ");
     scanf("%s",v1);
     InsertVex(g,v1);
     printf("插入与新顶点有关的弧或边,请输入弧或边数: ");
     scanf("%d",&n);
     for(k=0;k<n;k++)
     {
       printf("请输入另一顶点的值: ");
       scanf("%s",v2);
       if(g.kind<=1) // 有向
       {
         printf("对于有向图或网,请输入另一顶点的方向(0:弧头 1:弧尾): ");
         scanf("%d",&j);
         if(j) // v2是弧尾
           InsertArc(g,v2,v1);
         else // v2是弧头
           InsertArc(g,v1,v2);
       }
       else // 无向
         InsertArc(g,v1,v2);
     }
     Display(g); // 输出图g
     printf("删除顶点及相关的弧或边,请输入顶点的值: ");
     scanf("%s",v1);
     DeleteVex(g,v1);
     Display(g); // 输出图g
   }
   DestroyGraph(g); // 销毁图g
 }
Ejemplo n.º 3
0
 void main()
 {
   int j,k,n;
   OLGraph g;
   VertexType v1,v2;
   CreateDG(g);
   Display(g);
   printf("修改顶点的值,请输入原值 新值: ");
   scanf("%s%s",v1,v2);
   PutVex(g,v1,v2);
   printf("插入新顶点,请输入顶点的值: ");
   scanf("%s",v1);
   InsertVex(g,v1);
   printf("插入与新顶点有关的弧,请输入弧数: ");
   scanf("%d",&n);
   for(k=0;k<n;k++)
   {
     printf("请输入另一顶点的值 另一顶点的方向(0:弧头 1:弧尾): ");
     scanf("%s%d",v2,&j);
     if(j)
       InsertArc(g,v2,v1);
     else
       InsertArc(g,v1,v2);
   }
   Display(g);
   printf("删除一条弧,请输入待删除弧的弧尾 弧头:");
   scanf("%s%s",v1,v2);
   DeleteArc(g,v1,v2);
   Display(g);
   printf("删除顶点及相关的弧,请输入顶点的值: ");
   scanf("%s",v1);
   DeleteVex(g,v1);
   Display(g);
   printf("深度优先搜索的结果:\n");
   DFSTraverse(g,visit);
   printf("广度优先搜索的结果:\n");
   BFSTraverse(g,visit);
   DestroyGraph(g);
 }
void result_insert_arc(
		GRAPHIC_TYPE* graphic,
		VERTEX_TYPE* start_vertex,
		VERTEX_TYPE* end_vertex,
		int arc_weight) {
	VERTEX_TYPE* graphic_start_vertex = GetVertex(*graphic, start_vertex -> value);
	if (graphic_start_vertex == NULL) {
		InsertVertex(graphic, start_vertex -> value, &graphic_start_vertex);
	}
	VERTEX_TYPE* graphic_end_vertex = GetVertex(*graphic, end_vertex -> value);
	if (graphic_end_vertex == NULL) {
		InsertVertex(graphic, end_vertex -> value, &graphic_end_vertex);
	}
	InsertArc(graphic, graphic_start_vertex, graphic_end_vertex, arc_weight);
}
Ejemplo n.º 5
0
void ExtremalQuery3BSP<Real>::CreateBSPTree (
    std::multiset<SphericalArc>& arcs, std::vector<SphericalArc>& nodes)
{
    // The tree has at least a root.
    mTreeDepth = 1;

    typename std::multiset<SphericalArc>::reverse_iterator iter;
    for (iter = arcs.rbegin(); iter != arcs.rend(); ++iter)
    {
        InsertArc(*iter, nodes);
    }

    // The leaf nodes are not counted in the traversal of InsertArc.  The
    // depth must be incremented to account for leaves.
    ++mTreeDepth;
}
Ejemplo n.º 6
0
Archivo: realmap.c Proyecto: taxusyew/c
// 添加一个弧,逻辑部分
void AddArc(RealMap * rm, int src, int des, int weight)
{
	RMNode * tmp;
	RMNode * arc;
	RMNode * tmparc;
	RMNode * node = (RMNode *)malloc(sizeof(RMNode));
	memset(node, 0, sizeof(RMNode));
	
	tmp = rm->first;
	// 判断要插入的两个节点是否存在
	if(!IfNodeExist(rm, des) || !IfNodeExist(rm, src))
	{
		printf("Error: des node does not exist!\n");
		return;
	}

	// 判断要插入的两个节点是否存在
	if(IfArcExist(rm, src, des))
	{
		printf("Error: Arc exist!\n");
		return;
	}

	//if(!tmp)
	//{
	//	printf("The map is empty. Can not insert!\n");
	//}
	//else
	//{
		while (tmp != 0 && tmp->id != src)
		{
			tmp = tmp->next;
		}
		// 判断是否到达最后
		if (tmp == 0)
		{
			printf("Error: Src node does not exist!\n");
		}
		else{
			// 从头插入弧链表
			InsertArc(tmp, des, weight);
			printf("InsertArc: No. %d -> No.%d\n", src, des);
		}
	//}
	
	rm->numArc++;
}
void result_insert_arc(
		AdjacentMultipleListGraphic graphic,
		int start_vertex_index,
		int end_vertex_index,
		int weight,
		AdjacentMultipleListGraphic* result) {
	ElementType start_vertex_value = (graphic -> vertex_list + start_vertex_index) -> value;
	ElementType end_vertex_value = (graphic -> vertex_list + end_vertex_index) -> value;

	AdjacentMultipleListGraphicVertex* start_vertex = GetVertex(*result, start_vertex_value);
	AdjacentMultipleListGraphicVertex* end_vertex = GetVertex(*result, end_vertex_value);
	if (start_vertex == NULL)
		InsertVertex(result, start_vertex_value, &start_vertex);
	if (end_vertex == NULL)
		InsertVertex(result, end_vertex_value, &end_vertex);

	InsertArc(result, start_vertex, end_vertex, weight);
}
Ejemplo n.º 8
0
 void main()
 {
   int i,j,k,n;
   ALGraph g;
   VertexType v1,v2;
   printf("请选择有向图\n");
   CreateGraph(&g);
   Display(g);
   printf("删除一条边或弧,请输入待删除边或弧的弧尾 弧头:");
   scanf("%s%s",v1,v2);
   DeleteArc(&g,v1,v2);
   printf("修改顶点的值,请输入原值 新值: ");
   scanf("%s%s",v1,v2);
   PutVex(&g,v1,v2);
   printf("插入新顶点,请输入顶点的值: ");
   scanf("%s",v1);
   InsertVex(&g,v1);
   printf("插入与新顶点有关的弧或边,请输入弧或边数: ");
   scanf("%d",&n);
   for(k=0;k<n;k++)
   {
     printf("请输入另一顶点的值: ");
     scanf("%s",v2);
     printf("对于有向图,请输入另一顶点的方向(0:弧头 1:弧尾): ");
     scanf("%d",&j);
     if(j)
       InsertArc(&g,v2,v1);
     else
       InsertArc(&g,v1,v2);
   }
   Display(g);
   printf("删除顶点及相关的弧或边,请输入顶点的值: ");
   scanf("%s",v1);
   DeleteVex(&g,v1);
   Display(g);
   printf("深度优先搜索的结果:\n");
   DFSTraverse(g,print);
   printf("广度优先搜索的结果:\n");
   BFSTraverse(g,print);
   DestroyGraph(&g);
   printf("请顺序选择有向网,无向图,无向网\n");
   for(i=0;i<3;i++) /* 验证另外3种情况 */
   {
     CreateGraph(&g);
     Display(g);
     printf("插入新顶点,请输入顶点的值: ");
     scanf("%s",v1);
     InsertVex(&g,v1);
     printf("插入与新顶点有关的弧或边,请输入弧或边数: ");
     scanf("%d",&n);
     for(k=0;k<n;k++)
     {
       printf("请输入另一顶点的值: ");
       scanf("%s",v2);
       if(g.kind<=1) /* 有向 */
       {
         printf("对于有向图或网,请输入另一顶点的方向(0:弧头 1:弧尾): ");
         scanf("%d",&j);
         if(j)
           InsertArc(&g,v2,v1);
         else
           InsertArc(&g,v1,v2);
       }
       else /* 无向 */
         InsertArc(&g,v1,v2);
     }
     Display(g);
     printf("删除顶点及相关的弧或边,请输入顶点的值: ");
     scanf("%s",v1);
     DeleteVex(&g,v1);
     Display(g);
     DestroyGraph(&g);
   }
 }