图:

clipboard.png

clipboard.png

创建方式

clipboard.png

clipboard.png

clipboard.png

实现代码

    p->ilink = adjlist[x].firstedge;
    adjlist[x].firstedge = p;

    p->jlink = adjlist[y].firstedge;
    adjlist[y].firstedge = p;

BFS

    void BFS(int x)
    {
        cout << x << " ";
        queue<int>q;
        EdgeNode *p;
        // 记录是否访问过该点
        adjlist[x].tag = 1;
        p = adjlist[x].firstedge;
        // 记录路径长度为1的节点个数
        while (p) {
            if (p->ivex != x)
            {
                if (adjlist[p->ivex].tag == 0)
                {
                    adjlist[p->ivex].tag = 1;
                    q.push(p->ivex);
                }
                if (!p->jlink)
                {
                    break;
                }
                if (p->jlink)
                {
                    p = p->jlink;
                }
            }
            else
            {
                if (adjlist[p->jvex].tag == 0)
                {
                    adjlist[p->jvex].tag = 1;
                    q.push(p->jvex);
                }
                if (!p->ilink)
                {
                    break;
                }
                if (p->ilink)
                {
                    p = p->ilink;
                }
            }
        }
        // q队列中存入广度遍历的节点
        while (!q.empty())
        {
            int k;
            k = q.front();
            cout << k << " ";
            p = adjlist[q.front()].firstedge;
            q.pop();
            while (p) {
                if (p->ivex != k)
                {
                    if (adjlist[p->ivex].tag == 0)
                    {
                        adjlist[p->ivex].tag = 1;
                        q.push(p->ivex);
                    }
                    if (!p->jlink)
                    {
                        break;
                    }
                    else p = p->jlink;
                }
                else
                {
                    if (adjlist[p->jvex].tag == 0)
                    {
                        adjlist[p->jvex].tag = 1;
                        q.push(p->jvex);
                    }
                    if (!p->ilink)
                    {
                        break;
                    }
                    else p = p->ilink;
                }
            }
        }
        for (int i = 0; i < 10; i++)
        {
            adjlist[i].tag = 0;
        }
    }

DFS

void DFS(int x)
{
    int k;
    k = x;
    stack<int>q;
    q.push(x);
    cout << x << " ";
    EdgeNode *p;
    // 记录是否访问过该点
    adjlist[x].tag = 1;
    p = adjlist[x].firstedge;
    while (!q.empty()) 
    {
        int status = 0;
        if (p->ivex != k)
        {
            if (adjlist[p->ivex].tag == 0)
            {
                k = p->ivex;
                adjlist[p->ivex].tag = 1;
                cout << p->ivex << " ";
                p = adjlist[k].firstedge;
                q.push(p->ivex);
                status = 1;
            }
            else
            {
                if (!p->jlink)
                {
                    k = q.top();
                    p = adjlist[q.top()].firstedge;
                    q.pop();
                    break;
                }
                
            }
            if (status == 0)
            {
                status = 1;
                p = p->jlink;

            }
        }
        else
        {
            if (adjlist[p->jvex].tag == 0)
            {
                k = p->jvex;
                adjlist[p->jvex].tag = 1;
                cout << p->jvex << " ";
                p = adjlist[k].firstedge;
                q.push(p->jvex);
                status = 1;
            }
            else
            {
                if (!p->ilink)
                {
                    k = q.top();
                    p = adjlist[q.top()].firstedge;
                    q.pop();
                    break;
                }
                
            }
            if (status == 0)
            {
                status = 1;
                p = p->ilink;
            }
        }
    }
}
void showGraph()
{
    for (int i = 0; i < 5; i++)
    {
        EdgeNode *p = NULL;
        if (adjlist[i].firstedge)
        {
            p = adjlist[i].firstedge;

            cout << i << " " << p->ivex << p->jvex << " ";

        }
        while (p)
        {
            if (p->ivex == i)
            {
                p = p->ilink;
            }
            else
            {
                p = p->jlink;
            }
            if (!p)
                break;
            else {
                cout << p->ivex << p->jvex << " ";
            }
        }
        cout << endl;
    }
}

吴闯
30 声望5 粉丝