構建有向圖鄰接表

來源:互聯網
上載者:User

標籤:style   blog   color   資料   for   io   

      建立一個有向圖的鄰接表,首先要構思好它的鄰接表裡麵包含哪些結構資料,然後根據哪些資料來建立相應的結構體。但也要注意資料的輸入。

 

#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 10typedef struct ArcNode             //弧節點結構體{     int adjvex;                            //該弧指向定點的位置     int data;     struct ArcNode * nextarc;    //指向下下一條弧的指標}ArcNode;typedef struct VNode             //定點結構體{     char data;     ArcNode * firstarc;              //指向第一條弧的指標}VNode, AdjList[MAX_SIZE];typedef struct                         //建立圖的結構體{     AdjList vertices;                   //頂點數組     int vexnum, arcnum;           //頂點個數及弧的條數}ALGraph;int Find(ALGraph G, char ch)    //尋找此節點位於頂點數組的那個位置{     int i;     for(i = 0; i<G.vexnum; i++)     {          if(ch == G.vertices[i].data)               break;     }     return i;}void Input(ALGraph & G)     //構建鄰接表的輸入操作{     int mark, data;               //mark是用來標記是否有下一條弧,data則用來儲存要輸入的資料     ArcNode * p;     char ch;     printf("請輸入圖的頂點數和弧的條數。\n");     scanf("%d%d", &G.vexnum, &G.arcnum);     printf("請輸入頂點資訊。\n");     getchar();     for(int i = 0; i<G.vexnum; i++)          {               scanf("%c", &G.vertices[i].data);               getchar();         }     printf("請按照各個頂點的順序來輸入弧節點。\n");     for(int i = 0; i<G.vexnum; i++)     {          mark = 1;          G.vertices[i].firstarc = NULL;          printf("請判斷以%c為弧尾的弧。\n", G.vertices[i].data);          while(mark)          {               printf("判斷是否有下一個弧,若有則輸入1,無則輸入0。\n");               scanf("%d", &mark);               getchar();               if(mark)               {                    p = (ArcNode *)malloc(sizeof(ArcNode));                    p->nextarc = NULL;                    printf("輸入弧節點的資訊:");                    scanf("%c", &ch);                    getchar();                    scanf("%d", &data);                    p->adjvex = Find(G, ch);                    p->data = data;                    p->nextarc = G.vertices[i].firstarc;                    G.vertices[i].firstarc = p;               }          }     }}void Output(ALGraph G)       //輸出圖的有關資訊{     for(int i = 0; i<G.vexnum; i++)     {          printf("%c : ", G.vertices[i].data);          while(G.vertices[i].firstarc)                  //輸出每個節點弧中所包含的資料          {               printf("%d  ", G.vertices[i].firstarc->data);               G.vertices[i].firstarc = G.vertices[i].firstarc->nextarc ;          }          printf("\n");     }}int main(){     ALGraph G;     Input(G);     Output(G);     return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.