图邻接表C语言实现 拓扑排序

ALGpraph.h

 

#pragma once /************************************************************************/ /* 图的邻接表存储结构 */ /************************************************************************/ #define MaxVertexNum 100 #define QueueSize 30 typedef char VertexType; typedef int ArcType; typedef struct node //边表结点 { int adjvex; //邻接点域 struct node* next; //域链 //若是要表示边上的权,则应增加一个数据域 }ArcNode; typedef struct vnode //顶点边结点 { VertexType vertex; //顶点域 ArcNode* firstedge;//边表头指针 }VertexNode; typedef VertexNode AdjList[MaxVertexNum]; //AdjList是邻接表类型 typedef struct { AdjList adjlist; //邻接表 int n; //图中当前顶点数 int e; //图中当前边数 }ALGraph; //对于简单的应用,无须定义此类型,可直接使用AdjList类型 ALGraph* initALGraph(); int TopoSort(ALGraph* a);  

 

TopoSort.c

#include "ALGraph.h" #include "stack.h" #include #include /********************************************************************************** Function Name:initALGraph Return Type: void Function Description: 初始化有向无环图 **********************************************************************************/ int indegree[MaxVertexNum+1]={0}; ALGraph* initALGraph() { ALGraph* a = NULL; ArcNode* e = NULL; int i, j, k; char v1, v2; printf("请输入顶点数和边数(输入格式为:顶点数,边数): "); scanf("%d,%d", &i, &j); if(i<0 || j<0) return NULL; a = (ALGraph*)malloc(sizeof(ALGraph)); if(a == NULL) return NULL; a->n = i; a->e = j; for(i=0; in; i++) { printf("请输入顶点信息 每个顶点以回车作为结束: "); fflush(stdin); scanf("%c",&(a->adjlist[i].vertex)); // 读入顶点信息 a->adjlist[i].firstedge=NULL; // 点的边表头指针设为空 } for(k=0; ke; k++) { printf("请输入边的信息(输入格式为:i,j,方向为i->j): "); fflush(stdin); scanf("%c,%c", &v1, &v2); for(i=0; v1!=a->adjlist[i].vertex; i++); //找到顶点对应的存储序号 for(j=0; v2!=a->adjlist[j].vertex; j++); //找到顶点对应的存储序号 e = (ArcNode*)malloc(sizeof(ArcNode)); // e->adjvex = i; // e->next = a->adjlist[j].firstedge; // a->adjlist[j].firstedge = e; // indegree[j]++; e = (ArcNode*)malloc(sizeof(ArcNode)); e->adjvex = j; e->next = a->adjlist[i].firstedge; a->adjlist[i].firstedge = e; indegree[j]++; } return a; } /********************************************************************************** Function Name: TopoSort Return Type: ALGraph* a Function Description: 拓扑排序 **********************************************************************************/ int TopoSort(ALGraph* a) { int i, j, count = 0; ArcNode* p = NULL; Stack s = CreatStack(); for(i=0; in; i++) { if(indegree[i] == 0) { push(s, i); } } printf("拓扑排序为:/n"); while(!isEmpty(s)) { j = pop(s); count++; printf("%c/n", a->adjlist[j].vertex); p = a->adjlist[j].firstedge; while(p) { if(--indegree[p->adjvex] == 0) { push(s, p->adjvex); } p = p->next; } } if(count < a->n) { printf("该图有回路/n"); return 0; } return 1; }  

 

stack.h

#ifndef _STACK_H #define _STACK_H typedef int ElementType; struct stack; typedef struct stack *Stack; Stack CreatStack(); int isEmpty(Stack s); int push(Stack s, ElementType e); int pop(Stack s); int makeEmpty(Stack s); int isFull(Stack s); #endif 

 

stack.c

#include "stack.h" #include #include #define initStackSize 5 #define emptyTOS -1 struct stack { int size; int top; ElementType *array; }; Stack CreatStack() { Stack s; s = (Stack)malloc(sizeof(struct stack)); if(s == NULL) { free(s); return NULL; } s->array = (ElementType *)malloc(sizeof(ElementType) * initStackSize); if(s->array == NULL) { free(s->array); return NULL; } memset(s->array, 0, initStackSize); makeEmpty(s); s->size = 0; return s; } int isFull(Stack s) { return s->top+1 == s->size; } int isEmpty(Stack s) { return s->top == emptyTOS; } int makeEmpty(Stack s) { s->top = emptyTOS; return 1; } int push(Stack s, ElementType e) { if(s != NULL) { if(isFull(s)) { s->array = (ElementType *)realloc(s->array, sizeof(ElementType) * (s->size + initStackSize)); } s->array[++s->top] = e; s->size++; return 1; } return 0; } int pop(Stack s) { if(!isEmpty(s)) { s->size--; return s->array[s->top--]; } else return -1; }  

 

main.c

#include "ALGraph.h" int main() { ALGraph*a = initALGraph(); TopoSort(a); return 0; } 

你可能感兴趣的:(数据结构)