資料結構之---C語言實現拓撲排序AOV圖

來源:互聯網
上載者:User

標籤:++i   ack   logic   資料結構   jlist   tar   tac   第一個   rem   

//有向圖的拓撲排序//楊鑫#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_NAME 3#define MAX_VERTEX_NUM 20typedef int InfoType;//存放網的權值typedef char VertexType[MAX_NAME];//字串類型typedef enum{DG, DN, AG, AN}GraphKind;//{有向圖,有向網,無向圖,無向網}//圖的鄰接表格儲存體typedef struct ArcNode{int adjvex;//該弧所指向的頂點的位置struct ArcNode *nextarc;//指向嚇一條的指標InfoType *info;//網的權值指標}ArcNode;typedef struct VNode{VertexType data;//頂點資訊ArcNode *firstarc;//第一個表結點的地址,指向第一條依附該頂點的弧的指標}VNode, AdjList[MAX_VERTEX_NUM];//頭結點typedef struct{AdjList vertices;int vexnum, arcnum;//圖的當前頂點數和弧數int kind;//圖的種類標誌}ALGraph;//若G中存在頂點u,則返回該頂點在圖中的位置。都則返回-1int LocateVex(ALGraph G, VertexType u){int i;for(i = 0; i < G.vexnum; ++i){if(strcmp(u, G.vertices[i].data) == 0)return i;return -1;}}//採用鄰接表格儲存體結構,構造沒有相關資訊的圖G(用一個函數構造4種圖)int CreateGraph(ALGraph *G){int i, j, k;int w;//權值VertexType va, vb;ArcNode *p;printf("請輸入圖的類型(有向圖:0,有向網:1。無向圖:2,無向網:3):");scanf("%d", &(*G).kind);printf("請輸入圖的頂點數和邊數:(以空格間隔): \n");scanf("%d%d", &(*G).vexnum, &(*G).arcnum);printf("請輸入%d個頂點的值(小於%d個字元):\n", (*G).vexnum, MAX_NAME);for(i = 0; i < (*G).vexnum; ++i)                //構造頂點向量{scanf("%s", (*G).vertices[i].data);(*G).vertices[i].firstarc = NULL;}if((*G).kind == 1 || (*G).kind == 3)//網{printf("請順序輸入每條弧(邊)的權值,弧尾和弧頭(以空格作為間隔):\n");}else//圖{printf("請順序輸入每條弧(邊)的弧尾和弧頭(以空格作為間隔):\n");}for(k = 0; k < (*G).arcnum; ++k){if((*G).kind == 1 || (*G).kind == 3)scanf("%d%s%s", &w, va, vb);elsescanf("%s%s", va, vb);i = LocateVex(*G, va);//弧尾j = LocateVex(*G, vb);//弧頭p = (ArcNode*)malloc(sizeof(ArcNode));p->adjvex = j;if((*G).kind == 1 || (*G).kind == 3){p->info = (int *)malloc(sizeof(int));*(p->info) = w;}else{p->info = NULL;}p->nextarc = (*G).vertices[i].firstarc;                 //插在表頭(*G).vertices[i].firstarc = p;if((*G).kind >= 2)//無向圖或網。產生第二個表結點{p = (ArcNode*)malloc(sizeof(ArcNode));p->adjvex = i;if((*G).kind == 3){p->info = (int*)malloc(sizeof(int));*(p->info) = w;}else{p->info = NULL;}p->nextarc = (*G).vertices[j].firstarc;               //插在表頭(*G).vertices[j].firstarc = p;     }}return 1;}//輸出圖的鄰接表Gvoid Display(ALGraph G){int i;ArcNode *p;switch(G.kind){case DG:printf("有向圖\n");break;case DN:printf("有向網\n");break;case AG:printf("無向圖\n");break;case AN:printf("無向網\n");}printf("%d 個頂點: \n", G.vexnum);for(i = 0; i < G.vexnum; ++i){printf("%s ", G.vertices[i].data);}printf("\n%d條弧(邊):\n", G.arcnum);for(i = 0; i < G.vexnum; i++){p = G.vertices[i].firstarc;while(p){if(G.kind <= 1){printf("%s->%s", G.vertices[i].data, G.vertices[p->adjvex].data);if(G.kind == DN)printf(":%d ", *(p->info));}else{if(i < p->adjvex){printf("%s--%s", G.vertices[i].data, G.vertices[p->adjvex].data);if(G.kind == AN)printf(":%d ", *(p->info));}}p = p->nextarc;}printf("\n");}}//求頂點的入度void FindInDegree(ALGraph G, int indegree[]){int i;ArcNode *p;//賦初值for(i = 0; i < G.vexnum; i++){indegree[i] = 0;}for(i = 0; i < G.vexnum; i++){p = G.vertices[i].firstarc;while(p){indegree[p->adjvex]++;p = p->nextarc;}}}//棧類型typedef int SElemType;#define STACK_INIT_SIZE 10//儲存空間初始分配量#define STACKINCREMENT 2//儲存空間分配增量//棧的順序儲存結構表示typedef struct SqStack{SElemType *base;//基地址SElemType *top;//棧頂指標int stacksize;//當前已經分配的儲存空間}SqStack;                                              //構造一個空棧int InitStack(SqStack *S){//為棧底分分配一個指定大小的儲存空間(*S).base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));if(!(*S).base)exit(0);(*S).top = (*S).base;//棧底與棧頂指標同樣(*S).stacksize = STACK_INIT_SIZE;return 1;}//若棧S為空白棧(棧底指標和棧頂指標同樣), 則返回1。否則返回0int StackEmpty(SqStack S){if(S.top == S.base)return 1;elsereturn 0;}//插入元素e為新的棧頂元素int Push(SqStack *S, SElemType e){if((*S).top - (*S).base >= (*S).stacksize){(*S).base = (SElemType *)realloc((*S).base,((*S).stacksize + STACKINCREMENT)*sizeof(SElemType));if(!(*S).base)exit(0);(*S).top = (*S).base + (*S).stacksize;   (*S).stacksize += STACKINCREMENT;}*((*S).top)++= e;return 1;}//若棧不為空白,則刪除S棧頂元素用e返回其值。並返回1。否則返回0int Pop(SqStack *S, SElemType *e){if((*S).top == (*S).base){return 0;}*e = *--(*S).top;return 1;}//有向圖的G採用鄰接表格儲存體結構。若G無迴路,則輸出G的頂點的一個拓撲結構int TopologicalSort(ALGraph G){int i, k, count, indegree[MAX_VERTEX_NUM];SqStack S;ArcNode *p;FindInDegree(G, indegree);InitStack(&S);for(i = 0; i < G.vexnum; ++i){if(!indegree[i])Push(&S, i);count = 0;//棧不空while(!StackEmpty(S)){Pop(&S, &i);printf("%s", G.vertices[i].data);//輸出i號頂點並計數++count;//對i號頂點的每一個鄰接點的入度減1for(p == G.vertices[i].firstarc; p; p = p->nextarc){k = p->adjvex;if(!(--indegree[k]))//若入度減為0,則入棧Push(&S, k);}}if(count < G.vexnum){printf("此有向圖有迴路\n");return 0;}else{printf("為一個拓撲序列!!\n");}}}int main(){ALGraph f;printf("請選擇有向圖\n");CreateGraph(&f);Display(f);TopologicalSort(f);return 0;}

結果:


資料結構之---C語言實現拓撲排序AOV圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.