新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用圖的鄰接表法創(chuàng)建圖的實現(xiàn)完整C代碼怎么寫
這篇文章將為大家詳細講解有關(guān)用圖的鄰接表法創(chuàng)建圖的實現(xiàn)完整C代碼怎么寫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
10年積累的成都網(wǎng)站設(shè)計、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有武勝免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
/* 無向圖的鄰接表法創(chuàng)建圖的C代碼實現(xiàn) */ #include#include #include #define MaxSize 20 //圖頂點的最大數(shù)量 typedef char VertexType; //全局變量,記錄圖的結(jié)點的數(shù)量 int VertexNum; //定義圖頂點 typedef struct GraphNode { VertexType ver; struct GraphNode *next; }GraphNode; //用鄰接表法創(chuàng)建圖 void CreateGraph( GraphNode **g ) { VertexType ch; //用來接收頂點名稱 int i = 0; GraphNode *p, *q; (*g) = (GraphNode *)malloc(sizeof(GraphNode)*MaxSize);//分配一個結(jié)構(gòu)體數(shù)組 printf("請輸入圖的頂點:\n"); //存儲圖的頂點 scanf("%c", &ch); while( '\n' != ch ) { (*g)[i].ver = ch; (*g)[i].next = NULL; i++; scanf("%c", &ch); } VertexNum = i; //記錄頂點數(shù) for( i=0; i ver ); scanf("%c", &ch); while( '\n' != ch ) { p = (GraphNode *)malloc(sizeof(GraphNode)); p->ver = ch; q->next = p; q = p; q->next = NULL; scanf("%c", &ch); } } } //打印鄰接表法創(chuàng)建的圖 void PrintGraph( GraphNode *g ) { GraphNode *p; printf("圖的頂點為:\n"); //打印頂點 for( int i=0; i ver); p = p->next; } printf("\n"); } } int main() { GraphNode *g; CreateGraph( &g ); PrintGraph( g ); return 0; }
測試的圖:
關(guān)于用圖的鄰接表法創(chuàng)建圖的實現(xiàn)完整C代碼怎么寫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:用圖的鄰接表法創(chuàng)建圖的實現(xiàn)完整C代碼怎么寫
分享鏈接:http://www.ef60e0e.cn/article/pgppoi.html