日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

邻接表存储图利用BFS遍历

發布時間:2024/8/23 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 邻接表存储图利用BFS遍历 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//今天上機寫的鄰接表存儲圖利用BFS遍歷: #include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> using namespace std; struct node//存節點所連接的點 {int id;node *next; }; struct list//存各個節點的頂點值 {int data;node *first; }AdjList[1000]; struct ALGraph {int v,n;//v頂點數,邊數 }ALGraph; int vis[1000];//訪問標記 void create_list(node *&head,int n) {node *p=head;for(int i=0;i<n;++i){node *pnew=new node;scanf("%d",&pnew->id);pnew->next=NULL;p->next=pnew;p=p->next;}p->next=NULL;p=head->next;while(p){printf("%d ",p->id);p=p->next;}putchar('\n'); } void createAdjList() {printf("請輸入領接表的頂點和弧數:");scanf("%d%d",&ALGraph.v,&ALGraph.n);for(int i=1;i<=ALGraph.v;++i){printf("請輸入第%d個頂點值\n",i);scanf("%d",&AdjList[i].data);printf("請輸入該點所相鄰點的個數:");int n;scanf("%d",&n);printf("如果相鄰點存在請輸入各個頂點,不存在請輸入下個頂點信息\n");node *head=new node;if(n>0){create_list(head,n);AdjList[i].first=head->next;//第一個為空}elseAdjList[i].first=NULL;} } void BFS() {printf("%d ",AdjList[1].data);for(int i=1;i<=ALGraph.v;++i){while(AdjList[i].first&&!vis[i]){printf("%d ",AdjList[i].first->id);AdjList[i].first=AdjList[i].first->next;vis[i]=1;}}/*for(int i=1;i<=ALGraph.v;++i){cout<<AdjList[i].data<<" ";while(AdjList[i].first){printf("%d ",AdjList[i].first->id);AdjList[i].first=AdjList[i].first->next;}putchar('\n');}*/putchar('\n'); } int main() {memset(vis,0,sizeof(vis));int x;createAdjList();BFS();return 0; }

總結

以上是生活随笔為你收集整理的邻接表存储图利用BFS遍历的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。