邻接表

来源:互联网 发布:python split去掉空格 编辑:程序博客网 时间:2024/05/16 07:49
#include <iostream>
#include <string>
#include <new>
#include <deque>
using namespace std;
typedef char POINTTYPE;
#define SAFE_DELETE(a) {delete a;a = NULL;}
#define SAFE_DELETEARRAY(a) {delete[] a;a = NULL;}
#define MAXPOINTSIZE 512

struct Edge
{
    int index;
    Edge *next;
    Edge(){CleanUp();}
    ~Edge()
    {
        while (next)
        {
            Edge * tempPoint = next;
            next = next->next;
            delete tempPoint;
            tempPoint = NULL;
        }

    }
    void CleanUp()
    {
        memset(this,0,sizeof(Edge));
    }
};
struct Point
{
    POINTTYPE info;
    Edge * first;
    Edge * last;
    Point(){CleanUp();}
    ~Point(){
        SAFE_DELETE(first);
        last = NULL;
    }
    void CleanUp()
    {
        memset(this,0,sizeof(Point));
    }
};

struct Graph
{
    Point* m_Point;
    int PointSize;
    int EdgeSize;
    bool HasDirected;
    bool HasWeight;
    Graph(){CleanUp();}
    ~Graph(){
        if (PointSize >1)
        {
            SAFE_DELETEARRAY(m_Point);
        }else if (PointSize == 1)
        {
            SAFE_DELETE(m_Point);
        }
    }
    void CleanUp()
    {
        memset(this,0,sizeof(Graph));
    }
};

Graph * CreateGraph(bool hasDirected =false,bool hasWeight = false)throw()
{
    Graph *m_graph  = new(nothrow) Graph;
    if (!m_graph)
    {
        cout<<"内存分配失败"<<endl;
        return NULL;
    }
    cout<<"请输入点个数"<<endl;
    cin>>m_graph->PointSize;
    while (m_graph->PointSize <= 0)
    {
        cout<<"请输入一个正数"<<endl;
        cin>>m_graph->PointSize;
    }
    m_graph->m_Point = new Point[m_graph->PointSize];
    cout<<"请输入点信息以及相邻点,非正数结束"<<endl;
    int tempInt;
    Edge * tempEdge;
    for (int i=0 ;i<m_graph->PointSize;i++)
    {
        cin>>m_graph->m_Point[i].info>>tempInt;
        while (tempInt>=0)
        {
            if (!m_graph->m_Point[i].last)
            {
                m_graph->m_Point[i].last = new Edge;
                m_graph->m_Point[i].last->index = tempInt;
            }else
            {
                tempEdge = new Edge;
                tempEdge->index = tempInt;
                m_graph->m_Point[i].last->next = tempEdge;
                m_graph->m_Point[i].last = tempEdge;
            }
            if (!m_graph->m_Point[i].first)
            {
                m_graph->m_Point[i].first = m_graph->m_Point[i].last;
            }
            cin>>tempInt;
        }
    }
    return m_graph;
}

void print(Graph * m_graph)
{
    Edge *tempPoint;
    for (int i = 0 ;i<m_graph->PointSize;i++)
    {
        cout<<m_graph->m_Point[i].info;
        tempPoint = m_graph->m_Point[i].first;
        while (tempPoint)
        {
            cout<<" "<<tempPoint->index;
            tempPoint = tempPoint->next;
        }
        cout<<endl;
    }
}

void delphExtend(Graph * m_graph,int index ,bool *visited)
{
    cout<<"访问"<<m_graph->m_Point[index].info<<endl;
    visited[index] = true;
    Edge * tempPoint = m_graph->m_Point[index].first;
    int visitedIndex = -1;
    while (tempPoint)
    {
        if (!visited[tempPoint->index])
        {
            visitedIndex = tempPoint->index;
            break;
        }
        tempPoint = tempPoint->next;
    }

    if (visitedIndex != -1)
    {
        delphExtend(m_graph,visitedIndex,visited);
    }
}

//深度优先遍历
void delph(Graph *m_graph)
{
    bool visited[MAXPOINTSIZE];
    memset(visited,0,sizeof(visited));
    for (int i = 0;i<m_graph->PointSize;i++)
    {
        if (!visited[i])
        {
            delphExtend(m_graph,i,visited);
        }
    }
}

deque<int> m_queue;
//广度优先遍历(Breadth First Search)
void bfsExtend(Graph *m_graph,int index ,bool * visited)
{
    cout<<"访问"<<m_graph->m_Point[index].info<<endl;
    visited[index] = true;
    Edge * tempPoint = m_graph->m_Point[index].first;
    while (tempPoint)
    {
        if (!visited[tempPoint->index] && find(m_queue.begin(),m_queue.end(),tempPoint->index)== m_queue.end())
        {
            m_queue.push_back(tempPoint->index);
        }
        tempPoint = tempPoint->next;
    }
    if (m_queue.size()>0)
    {
        if (!visited[m_queue.front()])
        {
            bfsExtend(m_graph,m_queue.front(),visited);
        }
        
        m_queue.pop_front();
    }
}

void bfs(Graph * m_graph)
{
    bool visited[MAXPOINTSIZE];
    memset(visited,0,sizeof(visited));
    for (int i = 0; i < m_graph->PointSize; i++)
    {
        if (!visited[i])
        {
            bfsExtend(m_graph,i,visited);
        }
    }
}

int main()
{
    Graph *a =CreateGraph();
    delph(a);
    cout<<"bfs"<<endl;
    bfs(a);
    print(a);
    delete a;
    return 0;
}