#include <stdio.h>
#include <malloc.h>
#include <Windows.h>
#include <conio.h>
#include <queue>
#include <cstdlib>
#include <iostream>
using namespace std;
#define unmax 10000
#define White 0
#define Gray 1
#define Black 2
typedef struct point{
int color ;
int d ;
struct point *pi ;
int value ;
}*Ppoint;
typedef struct node{
Ppoint key ;
struct node *next ;
}*Pmap;
Pmap Create_Link(){
Pmap H,L,P;
int num ;
H = (Pmap)malloc(sizeof(struct node));
H->key = (Ppoint)malloc(sizeof(struct point));
P = (Pmap)malloc(sizeof(struct node));
P->key = (Ppoint)malloc(sizeof(struct point));
if (!H)
{
printf("error\n");
}
H->next = NULL ;
P = H ;
scanf("%d",&num);
while(num!=(-1)){
L = (Pmap)malloc(sizeof(struct node));
L->key = (Ppoint)malloc(sizeof(struct point));
L -> key->value = num ;
L->next = P->next ;
P->next = L ;
P = L ;
scanf("%d",&num);
}
return H ;
}
void Out_Putlink(Pmap Ptr){
if (!Ptr)
{
printf("output error\n");
exit(0);
}
Pmap ptr = (Pmap)malloc(sizeof(struct node));
ptr->key = (Ppoint)malloc(sizeof(struct point));
ptr = Ptr->next ;
while(ptr){
printf("%d ",ptr->key->value);
ptr = ptr->next ;
}
printf("\n");
}
void BFS(Pmap G[] , Ppoint s ,int n){
Pmap u ;
int i ;
for (i =0 ; i < n ;i++)
{
u = G[i]->next;
while(u){
if (u->key!=s)
{
u->key->color = White ;
u->key->d = unmax ;
u->key->pi = NULL ;
}
u = u->next ;
}
}
s->color = Gray ;
s->d = 0;
s->pi = NULL ;
queue<Ppoint>Q ;
Q.push(s);
while(!Q.empty()){
Ppoint x,v;
x = Q.front();
Q.pop();
Pmap ptr ;
int i ;
for (i = 0 ; i< n ; i++)
{
if (G[i]->next->key == x)
{
ptr = G[i] ;
}
}
while(ptr->next){
v = ptr->next->key;
if (v->color==White)
{
v->color = Gray ;
v->d++;
v->pi = x ;
}
Q.push(v);
}
x->color = Black ;
ptr->next = ptr->next->next ;
}
}
void Print_Path(Pmap G[],Ppoint s ,Ppoint v) {
if (s==v)
printf("%d",s->value);
else if(v->pi==NULL)
printf("no path\n");
else
Print_Path(G,s,v->pi);
printf("%d",v->value);
}
int main(){
Pmap Ptr[3] ;
int i ;
for (i=0 ; i < 3 ;i ++){
Ptr[i] = (Pmap)malloc(sizeof(struct node));
Ptr[i]->key =(Ppoint)malloc(sizeof(struct point));
Ptr[i] = Create_Link();
}
Ppoint s ;
//s->color=1;s->d=0;s->pi=NULL;s->value = 1;
BFS(Ptr,s,3);
for (i=0 ; i < 3 ;i ++)
Out_Putlink(Ptr[i]);
getch();
return 0 ;
}
乱写一通,我要放弃这样的图的构造了。。。。表示累觉不爱
乱图乱画(不知道自己写的什么鬼)
最新推荐文章于 2024-06-26 17:35:53 发布