A-C/ex5_1_1.c

150 lines
3.1 KiB
C
Raw Normal View History

2022-05-11 13:33:37 +00:00
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAX 100
int visited[MAX];
//<2F>ڽӱ<DABD>
typedef struct node{ //<2F>߱<EFBFBD>
int adjvex;
struct node *next;
}EdgeNode;
typedef struct vexnode{ //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
char data;
EdgeNode *firstedge;
}VHeadNode;
typedef struct{
int n;
int e;
VHeadNode adjlist[MAX]; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}adjlist,*AdjList;
void CreateAGraph(AdjList G){
char ch1,ch2;
EdgeNode *p;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d", &G->n);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d", &G->e);
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>\n");
for(int i=0; i<G->n; i++) {
getchar();
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><><EFBFBD><EFBFBD><EFBFBD>", i+1);
scanf("%c", &(G->adjlist[i].data));
G->adjlist[i].firstedge=NULL;
}
for(int i=0; i<G->e; i++){
getchar();
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1,<2C><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>", i+1);
scanf("%c,%c",&ch1,&ch2);
for(int j=0; j<G->n; j++){
for(int k=0; k<G->n; k++){
if(ch1==G->adjlist[j].data && ch2==G->adjlist[k].data){
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex = k;
p->next = G->adjlist[j].firstedge;
G->adjlist[j].firstedge = p;
p=(EdgeNode *)malloc(sizeof(EdgeNode));
p->adjvex = j;
p->next = G->adjlist[k].firstedge;
G->adjlist[k].firstedge = p;
}
}
}
}
}
void DisplayAGraph(AdjList G){
EdgeNode *p;
printf("ͼ<EFBFBD><EFBFBD><EFBFBD>ڽӱ<EFBFBD>Ϊ<EFBFBD><EFBFBD>\n");
for(int i=0; i<G->n; i++){
printf("%2d [%c]", i, G->adjlist[i].data);
p = G->adjlist[i].firstedge;
while(p!=NULL){
printf("-->[%d]", p->adjvex);
p=p->next;
}
printf("\n");
}
}
void clear(){
for(int i=0; i<MAX; i++){
visited[i] = 0;
}
}
void DFS(AdjList G, int v){
EdgeNode *p;
printf("(%d,%c)", v, G->adjlist[v].data);
visited[v] = 1;
p = G->adjlist[v].firstedge;
while(p!=NULL){
if(visited[p->adjvex]==0){
DFS(G, p->adjvex);
}
p=p->next;
}
}
void BFS(AdjList G, int v){
int Qu[MAX], front=0, rear=0;
EdgeNode *p;
printf("(%d,%c)", v, G->adjlist[v].data);
visited[v]=1;
rear = (rear + 1) % MAX;
Qu[rear] = v;
while(front != rear){
front = (front + 1) % MAX;
v = Qu[front];
p = G->adjlist[v].firstedge;
while(p!=NULL){
if(visited[p->adjvex]==0){
visited[p->adjvex]=1;
printf("(%d,%c)", p->adjvex, G->adjlist[p->adjvex].data);
rear = (rear + 1) % MAX;
Qu[rear]=p->adjvex;
}
p=p->next;
}
}
}
int main(){
int choice;
adjlist G;
for(;;){
printf("\t\t1.<2E><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ڽӱ<DABD>\n");
printf("\t\t2.<2E><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ڽӱ<DABD>\n");
printf("\t\t3.<2E>ڽӱ<DABD><D3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD>\n");
printf("\t\t4.<2E>ڽӱ<DABD><D3B1>Ĺ<EFBFBD><C4B9>ȱ<EFBFBD><C8B1><EFBFBD>\n");
printf("\t\t0.<2E>˳<EFBFBD>\n");
printf("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
CreateAGraph(&G);
break;
case 2:
DisplayAGraph(&G);
break;
case 3:
clear();
printf("<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>");
DFS(&G, 0);
printf("\n");
break;
case 4:
clear();
printf("<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>");
BFS(&G, 0);
printf("\n");
break;
default:
break;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
getch();
system("cls");
}
printf("<EFBFBD><EFBFBD>лʹ<EFBFBD>ã<EFBFBD><EFBFBD>ټ<EFBFBD>\n");
return 0;
}