数据结构-拓扑排序
拓撲排序:
因為上一篇有點小問題(Stack.base == Stack.top 寫成了 Stack.base == Stack.base)導致死循環,最后導致內存溢出,地址訪問沖突
參考上一篇的鏈接:
https://blog.csdn.net/qq_43552933/article/details/119745683
更正后的代碼如下:
1.棧代碼:
#pragma once #include<stdio.h> #define maxSize 100 typedef struct stack {int * base;int * top; }stack; bool init(stack & Stack) {//棧的初始化Stack.base = new int[maxSize];if (!Stack.base) {return false;}Stack.top = Stack.base;return true; } bool push(stack & Stack,int e) {//入棧if (Stack.top - Stack.base == maxSize) {//滿棧,不能再插入return false;}*(Stack.top) = e;Stack.top++;return true; } bool pop(stack & Stack, int &e) {//出棧if (Stack.base == Stack.top) {//棧空return false;}Stack.top--;e = *(Stack.top);return true; } int getTop(stack &Stack) {if (Stack.base == Stack.top) {//棧空return -1;}return *(Stack.top - 1); } void printStack(stack Stack) {//遍歷棧中的元素while (Stack.base != Stack.top) {printf("%d ", *(Stack.top - 1));Stack.top--;} } bool empty(stack Stack) {//棧空的判斷if (Stack.base == Stack.top) {return true;}return false; } void testStack() {//測試棧是否有問題stack Stack;init(Stack);int value;while (1) {scanf_s("%d", &value);if (value == -1) {break;}push(Stack, value);}printStack(Stack); }2.拓撲排序代碼:
#include<stdio.h> #include<stdlib.h> #include "stack.h" #define typeNode int //每個頭結點的標識數據類型 #define N 100 //最大結點數 int degree[N]; int result[N]; typedef struct dNode {//每個頭結點后緊跟的單位結點int data;struct dNode * next; }dNode; typedef struct mNode {//鄰接表中每一行的頭結點typeNode data;dNode * first;//指向第一個有效的后繼結點 }mNode; typedef struct {mNode vNode[N];//所有頭結點int vNum, eNum;//圖中頂點的數量和邊數量 }zNode; void init(zNode &ZNode) {printf("規定頂點從0開始取\n");scanf_s("%d%d", &ZNode.vNum, &ZNode.eNum);//輸入有向圖的頂點數和邊數for (int i = 0; i < ZNode.vNum; i++) {//規定頂點從0開始取scanf_s("%d", &ZNode.vNode[i].data);ZNode.vNode[i].first = NULL;}for (int i = 0; i < ZNode.eNum; i++) {int u, v;scanf_s("%d%d", &u, &v);//u頂點到v頂點有邊dNode* p = new dNode();p->data = v;p->next = ZNode.vNode[u].first;//只有指針域,指向地址ZNode.vNode[u].first= p;} } void print(zNode ZNode) { printf("遍歷鏈表:\n");for (int i = 0; i < ZNode.vNum; i++) {dNode* temp = ZNode.vNode[i].first;printf("%d ->", ZNode.vNode[i].data);while(temp){printf("%d ->",temp->data);temp = temp->next;}printf("NULL\n");} } void calculate(zNode ZNodeReverse) {//返回有向圖每個頂點的入度//每個頂點的入度for (int i = 0; i < ZNodeReverse.vNum; i++) {int tempLength = 0;dNode* p = new dNode();p = ZNodeReverse.vNode[i].first;while (p) {tempLength ++;p = p->next;}degree[i] = tempLength;} } bool tuoPuSort(zNode ZNode,int result[],stack &Stack) {//拓撲排序只是找到學習的先后依賴順序,并不是排序int count = 0;//計數for (int i = 0; i < ZNode.vNum; i++) {if (!degree[i]) {//對入度為0的點入棧push(Stack, i);}}while (!empty(Stack)) {dNode* p ;int tempV = getTop(Stack);result[count] = tempV;count++;int e=0;pop(Stack, e);p = ZNode.vNode[tempV].first;//取以tempV頂點為入度點while (p) {//和tempV相連的鄰接邊的入度減1int v = p->data;degree[v]--;if (!degree[v]) {push(Stack, v);}p = p->next;}}if (count < ZNode.vNum) {return false;}else {return true;} } int main() {zNode ZNode,ZNodeReverse;printf("鄰接表的構造:\n");init(ZNode);print(ZNode);printf("逆鄰接表的構造:\n");init(ZNodeReverse);print(ZNodeReverse);calculate(ZNodeReverse);stack Stack;init(Stack);printf("拓撲序列如下: ");if (tuoPuSort(ZNode, result,Stack)) {printf("\n可以構成拓撲序列!");};for (int i = 0; i < ZNode.vNum; i++) {printf("%d ", result[i]);}printf("\n");system("pause");return 0; }如果存在什么問題,歡迎批評指正!謝謝!
總結
- 上一篇: math:线性代数之行列式
- 下一篇: wordList04