随笔 - 403  文章 - 0  评论 - 6  阅读 - 3254

12月7日

今天上午上了统一建模语言,然后去考了上篮,下午上了数据结构,然后复习了离散

7-1 邻接表存储实现图的深度优先遍历

 
复制代码
#include<iostream>
#include<cstdio>
using namespace std;
struct edge{
        int v;
        edge* next;
};
struct node{
        char val;
        edge* next;
}a[1010];
int n;
int find(char ch){
        for(int i=0;i<n;i++){
                    if(a[i].val==ch){
                                    return i;
                    }
        }
        return -1;
}

void add(int u,int v){
        edge* e=new edge();
        e->next=a[u].next;
        e->v=v;
        a[u].next=e;
}

bool st[1010];
void dfs(int u){
        cout<<a[u].val<<' ';
        st[u]=1;
        edge* e=a[u].next;
        while(e!=NULL){
                    if(st[e->v]==0){
                                    dfs(e->v);
                    }
                    e=e->next;
        }
}

int main(){
        int m;
        cin>>n>>m;
        for(int i=0;i<n;i++){
                    char ch;
                    cin>>ch;
                    a[i].val=ch;
        }

        for(int i=0;i<m;i++){
                    char u,v;
                    cin>>u>>v;
            int uu=find(u),vv=find(v);
                    add(uu,vv);
                    add(vv,uu);
        }

        char ch;
        cin>>ch;
        int k;
        if((k=find(ch))!=-1){
                    dfs(k);
        }else{
                    cout<<"error";
        }
        return 0;
}
复制代码

 


 
posted on   石铁生  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示