第五周作业——有向图邻接表表示及反向图构造

本文介绍了一个使用C++实现的有向图及其逆向图的数据结构。通过定义节点结构体和图类,实现了有向图的创建与逆向图的构建,并输出了有向图与逆向图的具体连接关系。

#include <iostream>
using namespace std;

struct Node
{
 int vertex;
 Node *next;
};
const int Maxsize=13;
class GraphReverse
{
public:
 GraphReverse(int n,int e);
 ~GraphReverse(){}
private:
 Node adjlist[Maxsize];
 Node opposite[Maxsize];
 int verNum,arcNum;
};

GraphReverse::GraphReverse(int n,int e)
{
 verNum=n;arcNum=e;
 int i,j,k;
 for(i=0;i<verNum;i++)
 {
  adjlist[i].vertex=i;
  adjlist[i].next=NULL;
  opposite[i].vertex=i;
  opposite[i].next=NULL;
 }
 for(k=0;k<arcNum;k++)
 {
  Node *s,*t;
  cout<<"input:"<<endl;
  cin>>i>>j;
  s=new Node;s->vertex=j;
  s->next=adjlist[i].next;
  adjlist[i].next=s;
  t=new Node;t->vertex=i;
  t->next=opposite[j].next;
  opposite[j].next=t;
 }
 cout<<endl<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
 cout<<"有向图输出:"<<endl;
 for(i=0;i<verNum;i++)
 {
  cout<<adjlist[i].vertex<<":";
  Node *p=adjlist[i].next;
  while(p!=NULL)
  {
   cout<<p->vertex<<"^";
   p=p->next;
  }
  cout<<endl;
 }
 cout<<"逆向图输出:"<<endl;
 for(i=0;i<verNum;i++)
 {
  cout<<opposite[i].vertex<<":";
  Node *p=opposite[i].next;
  while(p!=NULL)
  {
   cout<<p->vertex<<"^";
   p=p->next;
  }
  cout<<endl;
 }
}
int main()
{
 GraphReverse Graph(13,22);
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值