#include<stdio.h> #include<stdlib.h> Typedef struct Graph{ Char* vexs; Int** arcs; Int vexnum,arcnum; )Graph; Graph* initGraph(int vexnum){ Graph* G=(Graph*)malloc(sizeof(Graph)) G->vexs=(char*)malloc(sizeof (char)*vexnum) G->arcs=(int**)malloc(sizeof (int*)*vexnum) For(int i=0;i<vexnum;I++) { G->arcs[i]= (int*)malloc(sizeof (int)*vexnum)} G->vexnum=Vexnum; G->arcnum=0; Return G } Int createGraph(Graph* G,char* vexs,int* arcs) {for(i=0;i<G->vexnum;i++) G->vexs[i]=vexs[i]; For((j=0;j<G->vexnum;j++) G->arcs[i][j]=*(arcs+i*vexnum+j ) If(G->arcs[i][j]!=0) G->arcnum++; } G->arcnum/=2; } Void DFS(Graph* G,int *visit,int index){ Printf("%c",G->vexs[index]) Visit[index]=1; For(int i=0;i<G->vexnum;i++) If(G->arcs[index][i]==1&&visit[index]!=1) DFS(G,visit,i) } Void BFS(Graph* G,int *visit ,int index){ Printf("%c",&G->vexs[index]) Visit[index]=1; Queue* initQueue(); enQueue(Q,index); while(!isEmpty(Q)) int i=deQueue(); For(int j=0;j<G->vexnum;J++) If(G->arcs[i][j]==1&&!visit[j]) Printf("%c",G->vexs[j]) Visit[j]=1; enQueue(Q,j);} } #define MAXSIZE 5 Typedef struct Queue{ Int front Int rear Int data[MAXSIZE] }Queue; Queue* Q InitQueue() { Queue* Q=(Queue*)malloc(sizeof(QUeue)); Queue->front=Queue->rear=0; Return Q; } Int enQueue(Queue* Q, int data) If (isFull(Q)){ Return 0} Else Q->data[Q->rear]=data; Q->rear=(Q->rear+1)%MAXSIZE } Int deQueue(Queue* Q) If (isempty(Q)){ Return 0} Else Int data=Q->data[Q->front]; Q->front=(Q->front+1)%MAXSIZE Return data; } Void printfQueue(Queue* Q){ Int length=(Q->rea-Q->front+MAXSIZE)%MAXSIZE For(int i=0;i<length;i++) Printf("%d->",Q->data[Q->front]) Q->front=(Q->front+1)%MAXSIZE; Int main(){ Graph* G=initGraph(5); Int arcs[5][5]={ 0,1,1,1,0, 0,1,1,1,0, 0,1,1,1,0, 0,1,1,1,0, 0,1,1,1,0, }; CreateGraph(*G,"ABCDE",(int*)arcs); Int* visit=(int*)malloc(sizeof(int)*G->vexnum); For(int i=0;i<G->vexnum;i++) Visit[i]=0; DFS(G,visit,0); BFS(G,visit,0) }修改正确并转化为c语言代码
时间: 2024-03-08 11:50:25 浏览: 90
c语言函数库-第一章(C标准库).doc
抱歉,您提供的代码已经是C语言代码。不过,代码中存在一些拼写错误和语法错误,我给您修正一下:
```
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef struct Graph{
char* vexs;
int** arcs;
int vexnum, arcnum;
}Graph;
Graph* initGraph(int vexnum) {
Graph* G = (Graph*)malloc(sizeof(Graph));
G->vexs = (char*)malloc(sizeof(char) * vexnum);
G->arcs = (int**)malloc(sizeof(int*) * vexnum);
for(int i = 0; i < vexnum; i++) {
G->arcs[i] = (int*)malloc(sizeof(int) * vexnum);
}
G->vexnum = vexnum;
G->arcnum = 0;
return G;
}
int createGraph(Graph* G, char* vexs, int* arcs) {
for(int i = 0; i < G->vexnum; i++) {
G->vexs[i] = vexs[i];
for(int j = 0; j < G->vexnum; j++) {
G->arcs[i][j] = *(arcs + i * G->vexnum + j);
if(G->arcs[i][j] != 0) {
G->arcnum++;
}
}
}
G->arcnum /= 2;
}
typedef struct Queue{
int front;
int rear;
int data[MAXSIZE];
}Queue;
Queue* initQueue() {
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->front = Q->rear = 0;
return Q;
}
int isFull(Queue* Q) {
if((Q->rear + 1) % MAXSIZE == Q->front) {
return 1;
} else {
return 0;
}
}
int isEmpty(Queue* Q) {
if(Q->front == Q->rear) {
return 1;
} else {
return 0;
}
}
int enQueue(Queue* Q, int data) {
if(isFull(Q)) {
return 0;
} else {
Q->data[Q->rear] = data;
Q->rear = (Q->rear + 1) % MAXSIZE;
return 1;
}
}
int deQueue(Queue* Q) {
if(isEmpty(Q)) {
return 0;
} else {
int data = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return data;
}
}
void printfQueue(Queue* Q) {
int length = (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
for(int i = 0; i < length; i++) {
printf("%d->", Q->data[Q->front]);
Q->front = (Q->front + 1) % MAXSIZE;
}
}
void DFS(Graph* G, int* visit, int index){
printf("%c", G->vexs[index]);
visit[index] = 1;
for(int i = 0; i < G->vexnum; i++) {
if(G->arcs[index][i] == 1 && visit[i] != 1) {
DFS(G, visit, i);
}
}
}
void BFS(Graph* G, int* visit ,int index) {
printf("%c", G->vexs[index]);
visit[index] = 1;
Queue* Q = initQueue();
enQueue(Q, index);
while(!isEmpty(Q)) {
int i = deQueue(Q);
for(int j = 0; j < G->vexnum; j++) {
if(G->arcs[i][j] == 1 && !visit[j]) {
printf("%c", G->vexs[j]);
visit[j] = 1;
enQueue(Q, j);
}
}
}
}
int main() {
Graph* G = initGraph(5);
int arcs[5][5] = {
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 0}
};
createGraph(G, "ABCDE", (int*)arcs);
int* visit = (int*)malloc(sizeof(int) * G->vexnum);
for(int i = 0; i < G->vexnum; i++) {
visit[i] = 0;
}
DFS(G, visit, 0);
printf("\n");
for(int i = 0; i < G->vexnum; i++) {
visit[i] = 0;
}
BFS(G, visit, 0);
printf("\n");
return 0;
}
```
阅读全文