数据结构图的基本实现

#include <iostream>
#include <algorithm>
#include <string.h> 
#include <string>
#include <cmath>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
const int N = 100;

using namespace std;

int V, E;
typedef struct arcnode
{
    struct arcnode *next;
    int vex;
    arcnode(const int &d) : vex(d), next(NULL) {}
} arcnode;
typedef struct node
{
    char ve;
    arcnode *fir = new arcnode(0);
} arclist[N];
class arc
{
private:
    arclist ar;
    int no, ed;
    bool vis[N];

public:
    arc()
    {
        no = ed = 0;
        for (int i = 0; i < 100; ++i)
            ar[i].ve = '#';
    }
    void visit()
    {
        for (int i = 0; i < 100; ++i)
            vis[i] = false;
    }
    void insert_node(char c)
    /**
     * 由于采用的是顺序表结构存储
     * 只需在设置一个记录结点个数的变量,每次新
     * 加入一个结点就顺序接在数组尾巴,然后记录
     * 结点个数的变量自增 1
     * @param {*char c}
     */
    {
        ar[no++].ve = c;
    }
    void delete_node(char c)
    /**
     * 首先在结点数组中找到要删除结点的下标,然后
     * 将该结点后面的数据均往前移动一个位置以覆盖数据,从而达到删除
     * 该结点目的。同时在邻接表中也要删除对应的边关系,首先将该节点
     * 的边关系的存储链表删除,然后遍历其他每一个结点的存储边关系的
     * 存储链表,找到其中包含该节点的下标的结点,将其删去,同时对大于
     * 该节点的其他数据,若存储下标大于它,则应自减 1
     * @param {*char c}
     */
    {
        int i;
        for (i = 0; i < no; ++i)
            if (ar[i].ve == c)
                break;
        for (int j = i; j < no - 1; ++j)
            ar[j] = ar[j + 1];
        no--;
        for (int k = 0; k < no; ++k)
        {
            arcnode *T = ar[k].fir;
            while (T->next)
            {
                if (T->next->vex == i)
                {
                    if (T->next->next)
                        T->next = T->next->next;
                    else
                        T->next = NULL;
                    break;
                }
                else if (T->next->vex > i)
                {
                    T->next->vex--;
                    T = T->next;
                }
                else
                    T = T->next;
            }
        }
    }
    void insert_edge(char u, char v)
    /**
     * @description: 首先应找到边对应两个结点的存储下标。然后分别
     * 在以u为起点的存储边关系的链表中连接v;然后在以v为起点的存储
     * 边关系的链表中连接u
     * @param {*char u, char v}
     */
    {
        int x = -1, y = -1;
        for (int i = 0; i < no; ++i)
            if (ar[i].ve == u)
                x = i;
            else if (ar[i].ve == v)
                y = i;
        arcnode *p = new arcnode(0);
        p->vex = y;
        arcnode *q = ar[x].fir;
        while (q->next)
            q = q->next;
        p->next = q->next;
        q->next = p;

        arcnode *r = new arcnode(0);
        r->vex = x;
        q = ar[y].fir;
        while (q->next)
            q = q->next;
        r->next = q->next;
        q->next = r;

        ed++;
    }
    void delete_edge(char u, char v)
    /**
     * @description: 
     * 删边操作同删点操作的思想
     * @param {*char u, char v}
     */
    {
        int x = -1, y = -1;
        for (int i = 0; i < no; ++i)
            if (ar[i].ve == u)
                x = i;
            else if (ar[i].ve == v)
                y = i;
        arcnode *T = ar[x].fir;
        while (T->next)
            if (T->next->vex != y)
                T = T->next;
            else
            {
                if (T->next->next)
                    T->next = T->next->next;
                else
                    T->next = NULL;
                break;
            }

        T = ar[y].fir;
        while (T->next)
            if (T->next->vex != y)
                T = T->next;
            else
            {
                if (T->next->next)
                    T->next = T->next->next;
                else
                    T->next = NULL;
                break;
            }
        ed--;
    }

    void bfs(int u)
    {
        queue<int> q;
        int v;
        cout << ar[u].ve << " ";
        q.push(u);
        vis[u] = true;
        while (!q.empty())
        {
            v = q.front();
            q.pop();
            arcnode *p = ar[v].fir;
            while (p->next)
            {
                p = p->next;
                if (!vis[p->vex])
                {
                    cout << ar[p->vex].ve << " ";
                    vis[p->vex] = true;
                    q.push(p->vex);
                }
            }
        }
    }
    void dfs(int u)
    {
        cout << ar[u].ve << " ";
        vis[u] = true;
        arcnode *p = ar[u].fir;
        while (p->next)
        {
            p=p->next;
            if (!vis[p->vex])
                dfs(p->vex);
        }
    }

    void display_edge()
    {
        cout << "修改后的结果" << endl;
        for (int i = 0; i < no; cout << endl, ++i)
        {
            cout << ar[i].ve << "  :";
            arcnode *T = new arcnode(0);
            T = ar[i].fir->next;
            while (T)
            {
                cout << T->vex << " ";
                T = T->next;
            }
            delete T;
        }
    }
};
void Arc()
{
    cout << "邻接表:\n";
    arc g;
    cout << "请输入点数,边数:\n";
    cin >> V >> E;
    cout << "请输入点\n";
    char cc, u, v;
    for (int i = 0; i < V; ++i)
    {
        cin >> cc;
        g.insert_node(cc);
    }
    cout << "请输入边\n";
    for (int i = 0; i < E; ++i)
    {
        cin >> u >> v;
        g.insert_edge(u, v);
    }
    g.display_edge();
    cout << "请输出宽搜结果\n";
    g.visit();
    g.bfs(0);
    cout << endl;
    cout << "请输出深搜结果\n";
    g.visit();
    g.dfs(0);
    cout << endl;
    cout << "请输入要删除的点的数及点\n";
    cin >> V;
    for (int i = 0; i < V; ++i)
    {
        cin >> cc;
        g.delete_node(cc);
    }
    g.display_edge();
    cout << "请输入要删除的边的数及边\n";
    cin >> E;
    for (int i = 0; i < E; ++i)
    {
        cin >> u >> v;
        g.delete_edge(u, v);
    }
    g.display_edge();
}

int main()
{
    int _ = 1;
    //sf(_);
    while (_--)
    {
        Arc();
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值