1 #include<iostream>
  2 #include<string.h>
  3 #define MAXNODE 100    //最多100个顶点
  4 using namespace std;
  5 
  6 typedef char VertexInfo[9];//定义顶点的名字
  7 typedef struct Node    //定义邻接点
  8 {
  9     int pos;//表示邻接点是那个一个节点
 10     struct Node* next;
 11     int value;
 12 }NODE;
 13 typedef struct  hEad//定义头结点
 14 {
 15     VertexInfo Name;//定义头结点的名字
 16     NODE* next;//定义第一个邻接点的指针
 17 }HEAD;
 18 typedef struct Vertex    //定义图的结构体
 19 {
 20     HEAD head[MAXNODE] = { 0 };//用数组来存储头节点
 21     int SumEdge;//边的总数
 22     int SumNode;//一共有多少个节点
 23 
 24 }Vertex;
 25 void LinkNode(VertexInfo TempString,int pos,int TempValue,HEAD &TempHead)
 26 {
 27     if (TempHead.next == NULL)//如果是没有邻接点
 28     {
 29         NODE* temp = (NODE*)malloc(sizeof(NODE));
 30         temp->pos = pos;
 31         temp->next = NULL;
 32         temp->value = TempValue;
 33         TempHead.next = temp;
 34     }
 35     else
 36     {
 37         NODE* TempMove = TempHead.next;
 38         while (TempMove->next != NULL)//移动到头结点的最后一个邻接点
 39         {
 40             TempMove = TempMove->next;
 41         }
 42         NODE* temp = (NODE*)malloc(sizeof(NODE));
 43         temp->pos = pos;
 44         temp->next = NULL;
 45         temp->value = TempValue;
 46         TempHead.next = temp;
 47 
 48     }
 49 
 50 }
 51 void CreateVertex(Vertex &test)
 52 {
 53     cout << "构成的是一个邻接表有向图" << endl;
 54     cout << "请输入边的总数" << endl;
 55     cin >> test.SumEdge;
 56     cout << "请输入节点的总数" << endl;
 57     cin >> test.SumNode;
 58     cout << "请输入节点的名字,用空格分开" << endl;
 59     for (int i = 0; i < test.SumNode;i++)//初始化头结点的名字
 60     {
 61         cin >> test.head[i].Name;
 62     }
 63     cout << "请输入那两个节点构成的边和权值 如:v0 v1 100" << endl;
 64     VertexInfo TempA, TempB;
 65     int TempValue;
 66     for (int i = 0; i < test.SumEdge; i++)//建立边的连接
 67     {
 68         cin >> TempA >> TempB >> TempValue;
 69         for (int j = 0; j < test.SumNode; j++)//遍历查找头结点
 70         {
 71             if (!strcmp(TempA, test.head[j].Name))//找到属于某个节点
 72             {
 73                 for (int k = 0; k < test.SumNode; k++)//找到邻接点的所属的位置
 74                 {
 75                     if (!strcmp(TempB, test.head[k].Name))//找到邻接点的位置,k表示邻接点的位置
 76                     {
 77                         LinkNode(TempB, k, TempValue,test.head[j]);//构建邻接表
 78                     }
 79                 }
 80             }
 81         }
 82     }
 83 }
 84 void ShowEdge(HEAD &head,Vertex &tempV)
 85 {
 86     NODE *temp;
 87     if (head.next == NULL)
 88     {
 89         cout << head.Name << "没有边" << endl;
 90     }
 91     else 
 92     {
 93         do {
 94             temp = head.next;
 95             cout << head.Name << "" << tempV.head[temp->pos].Name << "权值为" << temp->value << endl;
 96         } while (temp = temp->next);
 97     }
 98 }
 99 void ShowVertex(Vertex& TestVertex)
100 {
101     for (int i = 0; i < TestVertex.SumNode; i++)
102     {
103         ShowEdge(TestVertex.head[i],TestVertex);
104     }
105 }
106 
107 int main()
108 {
109     Vertex TestVertex;
110     CreateVertex(TestVertex);
111     ShowVertex(TestVertex);
112     return 0;
113 }