#include <iostream>
#include <deque>
#include <new>
using namespace std;

#define  MAXSIZE 512
//点
#define  INVALIDVALUE -1

struct Graph
{
    int edge[MAXSIZE][MAXSIZE];        //边
    char Point[MAXSIZE];            //点
    int PointSize;                    //点数量
    int edgeSize;                    //边数量
    bool hasDirected;                //是否有向图
    bool hasWeight;                    //是否带权

    Graph(){
        CleanUp();
        for (int i=0;i<MAXSIZE;i++)
        {
            /*for (int j = 0;j<MAXSIZE;j++)
            {
                edge[i][j] = INVALIDVALUE;
            }*/
            edge[i][i] = 1;
        }
    }
    ~Graph(){}
    void CleanUp();
};
void Graph::CleanUp()
{
    memset(edge,0,sizeof(edge));
    memset(Point,0,sizeof(Point));
    PointSize = 0;
    edgeSize = 0;
    hasDirected = false;
    hasWeight = false;

}

Graph * CreateGraph(bool hasDirected,bool hasWeight)
{
    Graph * graph = new(nothrow) Graph;

    if (!graph)
    {
        cout<<"内存分配失败"<<endl;
        return NULL;
    }

    cout<<"请输入点的数量"<<endl;
    cin>>graph->PointSize;
    if (graph->PointSize <= 0 || graph->PointSize >=MAXSIZE)
    {
        cout<<"点数量输入非法或者超过最大数量"<<endl;
        return NULL;
    }
    for (int i=1;i<=graph->PointSize;i++)
    {
        cin>>graph->Point[i];
    }

    cout<<"请输入边的数量"<<endl;
    cin>>graph->edgeSize;
    if (graph->edgeSize <= 0 || graph->edgeSize >=MAXSIZE)
    {
        cout<<"边数量输入非法或者超过最大数量"<<endl;
        return NULL;
    }
    int edgeA , edgeB , nWeight = 1;
    for (int i=1;i<=graph->edgeSize;i++)
    {
        cin>>edgeA>>edgeB;
        if (hasWeight)
        {
            cin>>nWeight;
        }
        graph->edge[edgeA][edgeB] = nWeight;
        if (!hasDirected)
        {
            graph->edge[edgeB][edgeA] = nWeight;
        }
    }
    graph->hasDirected = hasDirected;
    graph->hasWeight = hasWeight;
    return graph;
}

void Print(Graph *graph)
{
    if (!graph)
    {
        return;
    }
    for (int i = 1;i<=graph->PointSize;i++)
    {
        for (int j = 1; j<= graph->PointSize;j++)
        {
                cout<<graph->edge[i][j]<<" ";
        }
        cout<<endl;
    }
}

bool hasRoute(Graph *graph,int i,int j)
{
    if (graph->edge[i][j] !=0)
    {
        return true;
    }
    return false;
}

void EveryDepthPrior(Graph *graph,int index,bool * visited)
{
    cout<<"访问结点"<<index<<endl;
    visited[index] = true;
    for (int j = 1;j<=graph->PointSize;j++)
    {
        if (hasRoute(graph,index,j) && !visited[j])
        {
            EveryDepthPrior(graph,j,visited);
        }
    }
}

void DepthPrior(Graph *graph)
{
    bool visited[MAXSIZE];
    memset(visited,0,sizeof(visited));
    //遍历 是为防止 非连通图
    for (int i = 1;i<=graph->PointSize;i++)
    {
        if (!visited[i])
        {
            EveryDepthPrior(graph,i,visited);
        }
    }
}

deque<int> point;
void EveryScopePrior(Graph *graph,int index,bool * visited)
{
    cout<<"访问结点"<<index<<endl;
    visited[index] = true;
    for (int j = 1;j<=graph->PointSize;j++)
    {
        if (hasRoute(graph,index,j) && !visited[j])
        {
            point.push_front(j);
        }
    }
    if (point.size() > 0)
    {
        if (hasRoute(graph,index,point.back()) && !visited[point.back()])
        {
            EveryScopePrior(graph,point.back(),visited);
        }

         point.pop_back();
    }
}

void ScopePrior(Graph *graph)
{
    bool visited[MAXSIZE];
    memset(visited,0,sizeof(visited));
    //遍历 是为防止 非连通图
    for (int i = 1;i<=graph->PointSize;i++)
    {
        if (!visited[i])
        {
            EveryScopePrior(graph,i,visited);
        }
    }
}

int main()
{
    Graph * graph = CreateGraph(false,false);
    Print(graph);
    DepthPrior(graph);
    ScopePrior(graph);
    delete graph;
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值