数据结构_图_建立十字链表求有向图中每个顶点的入度出度并输出和它相关的弧_C++实现
2011-09-24 16:32
756 查看
示例输入:
a b c d //每个顶点的名字 char型
(CTRL+Z) //标示输入结束
7 //有向图中弧的个数
0 1 //该数对代表从编号为0的顶点到编号为1的顶点之间有一条弧 0->1 下同
0 2 //a、b、c、d编号分别为0、1、2、3
2 0
2 3
3 0
3 1
3 2
示例输出:
有向图示例
"head.h"
"main.cpp"
a b c d //每个顶点的名字 char型
(CTRL+Z) //标示输入结束
7 //有向图中弧的个数
0 1 //该数对代表从编号为0的顶点到编号为1的顶点之间有一条弧 0->1 下同
0 2 //a、b、c、d编号分别为0、1、2、3
2 0
2 3
3 0
3 1
3 2
示例输出:
有向图示例
"head.h"
#include<iostream> #define MAX_VERTEX_NUM 20 using namespace std; class ArcBox//弧 { public: ArcBox(); int tailvex,headvex; ArcBox * hlink,* tlink; char info; }; ArcBox::ArcBox() { tailvex=headvex=0; hlink=tlink=NULL; } class VexNode//顶点 { public: VexNode(); char data; ArcBox *firstin,*firstout; }; VexNode::VexNode() { firstin=firstout=NULL; } class OlGraphMessage//十字链表相关 { public: OlGraphMessage(); VexNode xlist[MAX_VERTEX_NUM]; int vexnum,arcnum; }; OlGraphMessage::OlGraphMessage() { vexnum=arcnum=0; } class OLGRAPH { public: void OlGraphConstructor();//建立十字链表 private: void GetVertex();//得到顶点信息 void GetArcNum();//得到弧度数 void CreatOl();//根据顶点信息建立十字链表 void OlPrinter();//输出顶点信息 OlGraphMessage ol; }; void OLGRAPH::OlGraphConstructor() { cout<<"OlGraphConstructor Called !"<<endl<<endl; GetVertex();//得到顶点信息 GetArcNum();//得到弧度数 CreatOl();//根据顶点信息建立十字链表 OlPrinter();//输出顶点信息 } void OLGRAPH::GetVertex()//得到顶点信息 { cout<<"GetVertex Called !"<<endl<<endl; cout<<"Please Enter The Vertex"<<endl<<endl; while(cin>>ol.xlist[ol.vexnum].data) ol.vexnum++; cin.clear(); } void OLGRAPH::GetArcNum()//得到弧度数 { cout<<"GetArcNum Called !"<<endl<<endl; cout<<"Please Enter The Number Of The Arc"<<endl<<endl; cin>>ol.arcnum; } void OLGRAPH::CreatOl()//根据顶点信息建立十字链表 { cout<<"CreatOl Called !"<<endl<<endl; cout<<"Please Input The ArcMessage :"<<endl<<endl; int a,b; ArcBox *insert,*p; while(ol.arcnum--) { cin>>a>>b; insert=new ArcBox; insert->headvex=a; insert->tailvex=b; p=ol.xlist[a].firstout; if(p==NULL) { ol.xlist[a].firstout=insert; } else { while(p->tlink!=NULL) p=p->tlink; p->tlink=insert; } p=ol.xlist[b].firstin; if(p==NULL) { ol.xlist[b].firstin=insert; } else { while(p->hlink!=NULL) p=p->hlink; p->hlink=insert; } } cout<<"CreatOl Succeed !"<<endl<<endl; } void OLGRAPH::OlPrinter()//输出顶点信息 { cout<<"OlPrinter Called !"<<endl<<endl; ArcBox * p; int od,id; for(int i=0;i<ol.vexnum;i++) { cout<<ol.xlist[i].data<<" : "<<endl;//输出顶点 od=id=0;//初始化出度和入度 //求以该顶点为弧尾的弧 p=ol.xlist[i].firstout; while(p!=NULL) { cout<<ol.xlist[p->headvex].data<<"->"<<ol.xlist[p->tailvex].data<<endl; p=p->tlink; od++; } cout<<"OutDegree = "<<od<<endl; //求以该顶点为弧头的弧 p=ol.xlist[i].firstin; while(p!=NULL) { cout<<ol.xlist[p->tailvex].data<<"<-"<<ol.xlist[p->headvex].data<<endl; p=p->hlink; id++; } cout<<"InDegree = "<<id<<endl; } }
"main.cpp"
#include"head.h" int main() { OLGRAPH ol; ol.OlGraphConstructor(); system("pause"); }
相关文章推荐
- C++实现图的十字链表表示及相关各算法
- 第十二周--数据结构--输出出图G中每个顶点的出度
- 输入m个学生,每个学生有4门课,在主调函数中输入学生的相关信息,编写三个函数: (1)求第一门课的平均分; (2)找出有两门课以上不及格的学生,并输出他们的学号和全部成绩,要求用指针函数实现:fl
- C++实现树的建立,查找,遍历输出
- 数据结构之---C/C++实现稀疏矩阵的十字链表
- 数据结构复习:几种排序算法的C++实现和二叉树的相关算法实现
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- (二叉树)谈一谈各类算法和数据结构的c++实现以及相关操作的复杂度(二)
- 二叉树的建立与输出以及其他一些相关操作(递归算法实现) C语言
- 数据结构_队列_用链表动态建立释放节点实现队列各种操作_C++实现
- C++实现:双输入单输出系统:建立一个 感知器网络,实现上述样本的分类。计算出相应的网络权值矩阵w。
- 数据结构 P42-43 算法实现 多项式的建立及加法
- 【数据结构】用C++实现单链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
- C++实现建立和一二进制树的三个递归遍历
- 哈夫曼树数据机构的建立及哈夫曼编码与解码的C++实现
- c++上机作业测试体重是否标准并输出相关提示
- 《C++实现数据结构》:堆栈
- c++数据结构:线性表实现之双链表
- 【数据结构】实现大小堆也叫二叉堆(类似c++中的优先队列)
- 图的实现--邻接表(求出各顶点的出度和入度)