• 十字链表的方式实现在头部插入图中节点


    #include<stdio.h>
    #include<malloc.h>
    #define MAX_VERTEX_NUM 20
     
    typedef struct ArcBox{
     int tailvex,headvex;//该弧的头和尾定点的位置
     struct ArcBox *hlink,*tlink;//分别为弧头和弧尾相同的弧的链域
     int *info; 
    }ArcBox; 
    typedef struct VexNode   //顶点结点
    {
     char data;    //顶点信息(标识)
     ArcBox *firstin;  //该顶点的第一条入弧
     ArcBox *firstout;  //该顶点的第一条出弧
    }VexNode;
    typedef struct      //图的顶点列表
    {
     VexNode xlist[MAX_VERTEX_NUM];  //顶点列表
     int vexnum,arcnum;    //定点数,弧数
    }OLGraph;
    int Locate(OLGraph *G, char vex)
    {
     int i=0;
     for(;i<G->vexnum;i++)
       if(vex==G->xlist[i].data)
       break;
     return i;
    }
    OLGraph* CreateDG(OLGraph *G)
    {
      G=(OLGraph *)malloc(sizeof(OLGraph));
      char vex1,vex2;
      int in,out;//分别表示头和尾巴 
      ArcBox *p; 
      
      printf("输入有向图的顶点数:\n");
      scanf("%d",&G->vexnum); 
      printf("输入有向图的边数:\n");
      scanf("%d",&G->arcnum);     
      printf("输入顶点值:");
      int i=0;
      printf("%d\n",G->vexnum);
      for(i=0;i<G->vexnum;++i)
      {
        printf("第%d次输入\n",i);
        printf("好奇葩\n");
        fflush(stdin);
        scanf("%c",&G->xlist[i].data); 
        G->xlist[i].firstin=G->xlist[i].firstout=NULL;
      } 
      int k=0;
      for(;k<G->arcnum;k++)
      {
       printf("输入弧%d(顶点1,顶点2)",k);
       fflush(stdin);//清空缓冲区,避免对后面数据的影响 
       scanf("%c,%c",&vex1,&vex2);//输入一条弧的始点和终点
       in =Locate(G,vex1);
       out =Locate(G,vex2);
       p=(ArcBox *)malloc(sizeof(ArcBox));
       p->headvex=in; p->tailvex=out;
       p->hlink=G->xlist[in].firstin;
       p->tlink=G->xlist[out].firstout; 
       G->xlist[in].firstin=G->xlist[out].firstout=p;
       scanf("%d",p->info);
      }
      
      return G;
    } 
     
    int main()
    {
      OLGraph *G;
      G=CreateDG(G);
      system("pause");
    }

    注:环境为:dev-c++,保存为.c文件

  • 相关阅读:
    Java的常量接口思考,项目中的常量是放在接口里还是放在类里呢?
    有些时候会看到url参数上出现%BF之类
    mysql 取整
    MySQL使用判断
    java 获得当前时间前指定几个小时的时间?
    SVN集成compare4比较软件
    JAVA去重
    连接SQL常见问题
    springMVC项目部署 服务器启动spring容器报错bean未从类加载器中找到
    Java 中Timer和TimerTask 定时器和定时任务使用的例子
  • 原文地址:https://www.cnblogs.com/moshang/p/3772101.html
Copyright © 2020-2023  润新知