- /*
- 名称:图的数组存储实现
- */
- #include<iostream>
- #include<assert.h>
- #include<malloc.h>
- #define N 5 //图的节点个数
- #define E 4 //图的边的条数
- using namespace std;
- typedef int Type;
- typedef struct Craphnode
- {
- Type node_container;
- Type node_index;
- }GraphNode;
- class Graph
- {
- public:
- Graph();
- ~Graph();
- void CreateGraph();
- void DFS(int i);
- void Display();
- void setarc();
- private:
- Type arc[N][N]; //图的弧信息
- GraphNode Vnode; //图的节点信息
- int snode_index,enode_index; //图的弧的开始节点索引和结束节点索引
- };
- Graph::Graph() //图的弧初始化
- {
- for(int i=0;i<N;i++)
- {
- for(int j=0;j<N;j++)
- {
- arc[i][j]=0;
- }
- }
- }
- void Graph::CreateGraph()
- {
- std::cout<<"Input:"<<N<<"顶点信息"<<endl;
- for(int i=0;i<N;i++)
- {
- std::cout<<"输入Vnode.index:"<<endl;
- std::cin>>Vnode.node_index;
- std::cout<<"输入Vnode.container:"<<endl;
- std::cin>>Vnode.node_container;
- }
- std::cout<<"图的顶点信息录入完毕!"<<endl;
- for(int i=0;i<E;i++)
- {
- setarc();
- while(snode_index>E,enode_index>E)
- {
- std::cout<<"error!";
- setarc();
- }
- arc[snode_index][enode_index]=1;
- }
- }//end of CreateGraph function definition
- Graph::~Graph()
- {
- }
- void Graph::setarc()
- {
- std::cout<<"起点:";
- std::cin>>snode_index;
- std::cout<<endl<<"终点:";
- std::cin>>enode_index;
- }
- void Graph::Display()
- {
- std::cout<<endl;
- for(int i=0;i<N;i++)
- {
- for(int j=0;j<N;j++)
- {
- std::cout<<arc[i][j];
- }
- std::cout<<endl;
- }
- getchar();
- }
- Graph g;
- int main()
- {
- g.CreateGraph();
- g.Display();
- getchar();
- return 0;
- }