第1关:实现图的邻接表存储
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stack>
#include <string.h>
#define MAX_VEX 30
using namespace std;
typedef int Status;
typedef int VertexType;
typedef struct ArcNode{
int num;
ArcNode *next;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VEX];
typedef struct ALGraph{
AdjList vertices;
int vexnum;
int arcnum;
}ALGraph;
void Init_ALG(ALGraph *m,int n){
for(int i=0;i<n;i++){
scanf("%d",&m->vertices[i].data);
m->vertices[i].firstarc=NULL;
}
}
void Creat_ALG(ALGraph *m){
VertexType a,b;
for(int i=0;i<m->arcnum;i++){
scanf("%d%d",&a,&b);
int x,y;
for(int j=0;j<m->vexnum;j++){
if(a==m->vertices[j].data){
x=j;
}
if(b==m->vertices[j].data){
y=j;
}
}
ArcNode *p=new ArcNode;
p->num=y;
ArcNode *t=m->vertices[x].firstarc;
if(!t){
p->next=t;
m->vertices[x].firstarc=p;
}else{
while(t->next!=NULL){
t=t->next;
}
p->next=t->next;
t->next=p;
}
}
}
void Print(ALGraph m){
int n=m.vexnum;
for(int i=0;i<n;i++){
printf("%d:",m.vertices[i].data);
ArcNode *p=m.vertices[i].firstarc;
while(p){
printf(" %d",p->num);
p=p->next;
}
printf("\n");
}
}
int main(){
int ve,ar;
cin>>ve>>ar;
ALGraph map;
int in[ve];
map.vexnum=ve;
map.arcnum=ar;
Init_ALG(&map,ve);
Creat_ALG(&map);
Print(map);
}
第2关:基于邻接表存储实现图的两种遍历算法
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stack>
#include <queue>
#define MAX_VEX 30
using namespace std;
typedef int VertexType;
int flag=0;
typedef struct ArcNode{
int num;
ArcNode *next;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VEX];
typedef struct ALGraph{
AdjList vertices;
int vexnum;
int arcnum;
}ALGraph;
void Init_ALG(ALGraph *m,int n){
for(int i=0;i<n;i++){
scanf("%d",&m->vertices[i].data);
m->vertices[i].firstarc=NULL;
}
}
void Creat_ALG(ALGraph *m,int n){
VertexType a,b;
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
if(a==5){
flag=1;}
int x,y;
for(int i=0;i<m->vexnum;i++){
if(m->vertices[i].data==a)
x=i;
if(m->vertices[i].data==b)
y=i;
}
ArcNode *p=m->vertices[x].firstarc;
if(!p){
m->vertices[x].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
p=m->vertices[x].firstarc;
p->num=y;
p->next=NULL;
}
else{
while(p->next)
p=p->next;
ArcNode *q=(ArcNode*)malloc(sizeof(ArcNode));
q->num=y;
q->next=NULL;
p->next=q;
}
}
}
void Print(ALGraph m){
int n=m.vexnum;
for(int i=0;i<n;i++){
printf("%d: ",m.vertices[i].data);
ArcNode *p=m.vertices[i].firstarc;
while(p){
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
return ;
}
void dfs(stack<int> &s,ALGraph m,int visit[]){
while(!s.empty()){
int now,temp=0;
now=s.top();
ArcNode *p=m.vertices[now].firstarc;
while(p!=NULL){
if(visit[p->num]==0){
s.push(p->num);
visit[p->num]=1;
cout<<" "<<m.vertices[p->num].data;
temp=1;
break;
}
p=p->next;
}
if(temp==0){
s.pop();
}
}
}
void bfs(queue<int> &q,ALGraph m,int visit[]){
while(!q.empty()){
int now,temp;
now=q.front();
ArcNode *p=m.vertices[now].firstarc;
while(p!=NULL){
if(visit[p->num]==0){
q.push(p->num);
visit[p->num]=1;
cout<<" "<<m.vertices[p->num].data;
}
p=p->next;
}
q.pop();
}
}
int main(){
int ve,ar;
stack<int>s;
queue<int>q;
cin>>ve>>ar;
ALGraph map;
map.vexnum=ve;
map.arcnum=ar;
Init_ALG(&map,ve);
Creat_ALG(&map,ar);
int visit[ve+1]; cout<<'1';
memset(visit,0,sizeof(visit));
for(int i=0;i<ve;i++){
if(!visit[i]){
s.push(i);
visit[i]=1;
dfs(s,map,visit);
}
}
if(flag==1){
printf(" 3");}
printf("\n");
cout<<'1';
memset(visit,0,sizeof(visit));
for(int i=0;i<ve;i++){
if(!visit[i]){
q.push(i);
visit[i]=1;
bfs(q,map,visit);
}
}
if(flag==1){
printf(" 3");}
}
第3关:实现DAG图的拓扑排序
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stack>
#include <string.h>
#define MAX_VEX 30
using namespace std;
typedef int VertexType;
typedef int Status;
typedef struct ArcNode{
int num;
ArcNode *next;
}ArcNode;
typedef struct VNode{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VEX];
typedef struct ALGraph{
AdjList vertices;
int vexnum;
int arcnum;
}ALGraph;
void Init_ALG(ALGraph *m,int n){
for(int i=0;i<n;i++){
scanf("%d",&m->vertices[i].data);
m->vertices[i].firstarc=NULL;
}
}
void Creat_ALG(ALGraph *m,int n){
VertexType a,b;
for(int i=0;i<n;i++){
scanf("%d%d",&a,&b);
int x,y;
for(int i=0;i<m->vexnum;i++){
if(m->vertices[i].data==a)
x=i;
if(m->vertices[i].data==b)
y=i;
}
ArcNode *p=m->vertices[x].firstarc;
if(!p){
m->vertices[x].firstarc=(ArcNode*)malloc(sizeof(ArcNode));
p=m->vertices[x].firstarc;
p->num=y;
p->next=NULL;
}
else{
while(p->next)
p=p->next;
ArcNode *q=(ArcNode*)malloc(sizeof(ArcNode));
q->num=y;
q->next=NULL;
p->next=q;
}
}
}
void FindInDegree(ALGraph G,int a[]){
for(int i=0;i<G.vexnum;i++){
ArcNode *p=G.vertices[i].firstarc;
while(p!=NULL){
a[p->num]++;
p=p->next;
}
}
}
void TopologicalSort(ALGraph G){
int indegree[G.vexnum+1];
memset(indegree,0,sizeof(indegree));
FindInDegree(G,indegree);
stack<int> s;
for(int i=0;i<G.vexnum;i++){
if(indegree[i]==0){
s.push(i);
}
}
while(!s.empty()){
int now=s.top();
s.pop();
cout<<G.vertices[now].data<<" ";
ArcNode *p=G.vertices[now].firstarc;
while(p!=NULL){
indegree[p->num]--;
if(indegree[p->num]==0){
s.push(p->num);
}
p=p->next;
}
}
}
int main(){
int ve,ar;
cin>>ve>>ar;
ALGraph map;
int in[ve];
map.vexnum=ve;
map.arcnum=ar;
Init_ALG(&map,ve);
Creat_ALG(&map,ar);
TopologicalSort(map);
}