数据结构C++版 王红梅 OJ习题

本文介绍使用邻接表存储有向图的方法,并实现计算各个顶点的入度和出度的算法。通过具体示例展示了如何构建邻接表以及如何遍历邻接表来计算每个顶点的入度和出度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1025: 邻接表(1)

Description

已知有向图采用邻接表存储,部分代码如下,请补充完成计算各个顶点的入度,出度算法。

#include <iostream>
#include <string>
using namespace std;

const int MaxSize=20;     // 顶点个数的最大值
int visited[MaxSize];     //访问标志数组(0表示未访问,1表示已访问)

//定义边表结点
struct ArcNode                
{
 int adjvex;                    //邻接点的序号
 ArcNode *next;                 //指向下一个边结点的指针
};

 //定义顶点表结点
template <class T>
struct VertexNode             
{
 T vertex;                 //顶点的名称
 ArcNode *firstedge;        //指向第一个边表结点的头指针
};

//邻接表类
template <class T>
class ALGraph
{
public:
   ALGraph(T a[ ], int n, int e);   //构造函数,初始化一个有n个顶点e条边的图
   ~ALGraph();                      //析构函数,释放邻接表中各边表结点的存储空间
   void DispALGraph();              //输出邻接表
   void CountInD(int ind[]);     //计算各个顶点的入度,存储在ind中
   void CountOutD(int outd[]);   //计算各个顶点的出度,存储在outd中
private:
   VertexNode<T> adjlist[MaxSize];  //存放顶点表的数组
   int vertexNum, arcNum;           //图的顶点数和边数
};

/*
 *前置条件:图不存在
 *输    入:无
 *功    能:图的初始化
 *输    出:无
 *后置条件:得到一个无向图
 */
template <class T>
ALGraph<T>::ALGraph(T a[ ], int n, int e)
{
    arcNum = e;                             //边数
 vertexNum=n;                            //顶点数
    int i,j;
 for (i=0; i<vertexNum; i++)
 {       
    adjlist[i].vertex = a[i];
    adjlist[i].firstedge = NULL;    
 }

 for (int k=0; k<arcNum; k++)    //依次输入每一条边,并在相应边表中插入结点
    {
  cin>>i>>j;                         //输入边所依附的两个顶点的序号
        ArcNode *s=new ArcNode;
  s->adjvex=j;  //生成一个边表结点s
     s->next=adjlist[i].firstedge;      //将结点s插入到i号表的头结点之后 
        adjlist[i].firstedge=s;
 }
}
/*   前置条件:图已存在
 *   输    入:无
 *   功    能:销毁图
 *   输    出:无
 *   后置条件:释放图所占用的存储空间
 */
template <class T>
ALGraph<T>::~ALGraph( )
{
  for (int i=0; i<vertexNum; i++)
  {
    ArcNode * p=adjlist[i].firstedge;
 while (p!=NULL)                                              //循环删除
 {
  adjlist[i].firstedge=p->next;
  delete p;                                                 //释放结点空间
  p=adjlist[i].firstedge;
 }
  }
}

/*  
 *前置条件:图已存在
 *输    入:无
 *功    能:输出图中所有顶点及边的数据信息
 *输    出:图中所有顶点及边的数据信息
 *后置条件:图保持不变
 */
template <class T>
void ALGraph<T>::DispALGraph( )                    
{                                 
 int i;
 ArcNode *p;
 cout<<"图的邻接表:\n";
    for(i=0;i<vertexNum;i++){     
  cout<<i<<" "<<adjlist[i].vertex<<" "; //输出图中顶点的序号i及值
  for(p=adjlist[i].firstedge;p;p=p->next)
   cout<<p->adjvex<<" ";  //输出i号顶点的邻接点的序号
  cout<<endl;
 }
}

//计算各个顶点的入度
template <class T>
void ALGraph<T>::CountInD(int ind[])
{
}
//计算各个顶点的出度
template <class T>
void ALGraph<T>::CountOutD(int outd[])
{
 }
int main()
{
 string a[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N"}; //顶点信息
 int i,n,e;
 cin>>n>>e;  //输入顶点个数和边个数
    ALGraph<string> G(a,n,e);
 G.DispALGraph();
 int ind[MaxSize];
 cout<<"Indegree:";
 G.CountInD(ind); 
    for(i=0;i<n;i++)
  cout<<ind[i]<<" ";// 输出各个顶点的入度
 cout<<endl;
    int outd[MaxSize];
 cout<<"OutDegree:";
 G.CountOutD(outd);
    for(i=0;i<n;i++)
  cout<<outd[i]<<" "; // 输出各个顶点的出度
 return 0;
}

Input

Output

Sample Input

6 7
0 1 0 3 1 2 1 5 2 4 3 5 4 5

Sample Output

Graph adjlist:
0 A 3 1 
1 B 5 2 
2 C 4 
3 D 5 
4 E 5 
5 F 
Indegree:0 1 1 1 1 3 
OutDegree:2 2 1 1 1 0 
//
// Created by Legends丶Hu on 2020/2/5.
//

#include <iostream>
#include <string>

using namespace std;
const int MaxSize = 20;     // 顶点个数的最大值
int visited[MaxSize];     //访问标志数组(0表示未访问,1表示已访问)
//定义边表结点
struct ArcNode {
    int adjvex;                    //邻接点的序号
    ArcNode *next;                 //指向下一个边结点的指针
};
//定义顶点表结点
template<class T>
struct VertexNode {
    T vertex;                 //顶点的名称
    ArcNode *firstedge;        //指向第一个边表结点的头指针
};

//邻接表类
template<class T>
class ALGraph {
public:
    ALGraph(T a[], int n, int e);   //构造函数,初始化一个有n个顶点e条边的图
    ~ALGraph();                      //析构函数,释放邻接表中各边表结点的存储空间
    void DispALGraph();              //输出邻接表
    void CountInD(int ind[]);     //计算各个顶点的入度,存储在ind中
    void CountOutD(int outd[]);   //计算各个顶点的出度,存储在outd中
private:
    VertexNode<T> adjlist[MaxSize];  //存放顶点表的数组
    int vertexNum, arcNum;           //图的顶点数和边数
};

template<class T>
ALGraph<T>::ALGraph(T a[], int n, int e) {
    arcNum = e;                             //边数
    vertexNum = n;                            //顶点数
    int i, j;
    for (i = 0; i < vertexNum; i++) {
        adjlist[i].vertex = a[i];
        adjlist[i].firstedge = NULL;
    }
    for (int k = 0; k < arcNum; k++)    //依次输入每一条边,并在相应边表中插入结点
    {
        cin >> i >> j;                         //输入边所依附的两个顶点的序号
        ArcNode *s = new ArcNode;
        s->adjvex = j;  //生成一个边表结点s
        s->next = adjlist[i].firstedge;      //将结点s插入到i号表的头结点之后
        adjlist[i].firstedge = s;
    }
}

template<class T>
ALGraph<T>::~ALGraph() {
    for (int i = 0; i < vertexNum; i++) {
        ArcNode *p = adjlist[i].firstedge;
        while (p != NULL)                                              //循环删除
        {
            adjlist[i].firstedge = p->next;
            delete p;                                                 //释放结点空间
            p = adjlist[i].firstedge;
        }
    }
}

template<class T>
void ALGraph<T>::DispALGraph() {
    int i;
    ArcNode *p;
    cout << "Graph adjlist:\n";
    for (i = 0; i < vertexNum; i++) {
        cout << i << " " << adjlist[i].vertex << " "; //输出图中顶点的序号i及值
        for (p = adjlist[i].firstedge; p; p = p->next)
            cout << p->adjvex << " ";  //输出i号顶点的邻接点的序号
        cout << endl;
    }
}

//计算各个顶点的入度
template<class T>
void ALGraph<T>::CountInD(int ind[]) {
    for(int i = 0; i < vertexNum; i++)
        ind[i] = 0;
    for(int i = 0; i < vertexNum; i++) {
        for(ArcNode *p = adjlist[i].firstedge; p; p = p->next) {
            ind[p->adjvex]++;
        }
    }
}

//计算各个顶点的出度
template<class T>
void ALGraph<T>::CountOutD(int outd[]) {
    for(int i = 0; i < vertexNum; i++) {
        outd[i] = 0;
        for(ArcNode *p = adjlist[i].firstedge; p; p = p->next)outd[i]++;
    }
}
/*
6 7
0 1 0 3 1 2 1 5 2 4 3 5 4 5
 */
int main() {
    string a[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"}; //顶点信息
    int i, n, e;
    cin >> n >> e;  //输入顶点个数和边个数
    ALGraph<string> G(a, n, e);
    G.DispALGraph();
    int ind[MaxSize];
    cout << "Indegree:";
    G.CountInD(ind);
    for (i = 0; i < n; i++)
        cout << ind[i] << " ";// 输出各个顶点的入度
    cout << endl;
    int outd[MaxSize];
    cout << "OutDegree:";
    G.CountOutD(outd);
    for (i = 0; i < n; i++)
        cout << outd[i] << " "; // 输出各个顶点的出度
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值