您的位置:首页 > 大数据 > 人工智能

daimachuan

2016-07-26 13:47 375 查看
query_graph.c

#include<stdio.h>

#include<stdlib.h>

#include<string>

#include<map>

#include<iostream>

using namespace std;

map<int,string>VId2Label;

map<string,int>VLabel2Id;

pair<map<string,int>::iterator,bool>Insert_Pair;

struct EdgeNode

{
string valued;
EdgeNode*next;
int adjvex;

};

struct vertexNode

{
EdgeNode*firstedge;
string data;

};

struct AdjList

{
int verNum;
int ArcNum;
vertexNode vertices[10];

};

int countnode;

void Create_GF(AdjList*G)

{
countnode=0;
int flag=0;
string x,la,y;
while(cin>>x>>la>>y)
{
int len=y.length();
if(y[len-1]=='}'){flag=1;y=y.substr(0,len-1);}
Insert_Pair=VLabel2Id.insert(pair<string,int>(x,countnode));
if(Insert_Pair.second==true)
{
VId2Label.insert(pair<int,string>(countnode,x));
G->vertices[countnode].firstedge=NULL;
countnode++;
}
Insert_Pair=VLabel2Id.insert(pair<string,int>(y,countnode));
if(Insert_Pair.second==true)
{
VId2Label.insert(pair<int,string>(countnode,y));
G->vertices[countnode].firstedge=NULL;
countnode++;
}
int st=VLabel2Id[x];
int ed=VLabel2Id[y];
EdgeNode*s=new EdgeNode;
s->valued=la;
s->adjvex=ed;
s->next=G->vertices[st].firstedge;
G->vertices[st].firstedge=s;
if(flag==1)break;
}

}

void Print_GF(AdjList *G)

{
for(int i=0;i<countnode;i++)
{
cout<<VId2Label[i];
EdgeNode*p=G->vertices[i].firstedge;
while(p)
{
cout<<" -> "<<VId2Label[p->adjvex]<<" ( BY:"<<p->valued<<") ";
p=p->next;
}
cout<<endl;
}

}

void Delete_GF(AdjList*G)

{
for(int i=0;i<countnode;i++)
{
EdgeNode*q;
EdgeNode*p=G->vertices[i].firstedge;
while(p)
{
q=p;
p=p->next;
delete q;
}
G->vertices[i].firstedge=NULL;
}

}

int main()

{
char tmp;
cin>>tmp;
while(tmp!='{')cin>>tmp;
getchar();
AdjList G;
Create_GF(&G);
Print_GF(&G);
Delete_GF(&G);

}

jiantu

#include<stdio.h>

#include<stdlib.h>

#include<string>

#include<map>

#include<iostream>

using namespace std;

map<int,string>VId2Label;

map<string,int>VLabel2Id;

pair<map<string,int>::iterator,bool>Insert_Pair;

struct EdgeNode

{
string valued;
EdgeNode*next;
int adjvex;

};

struct vertexNode

{
EdgeNode*firstedge;
string data;

};

struct AdjList

{
int verNum;
int ArcNum;
vertexNode vertices[10];

};

int count;

void Create_GF(AdjList*G)

{
cin>>G->verNum;
count=0;
for(int i=0;i<G->verNum;i++)
{
cin>>G->vertices[i].data;
Insert_Pair=VLabel2Id.insert(pair<string,int>(G->vertices[i].data,count));
if(Insert_Pair.second==true)
{
VId2Label.insert(pair<int,string>(count,G->vertices[i].data));
count++;
}
G->vertices[i].firstedge=NULL;
}

cin>>G->ArcNum;
string x,y,la;
for(int i=0;i<G->ArcNum;i++)
{
cin>>x>>y>>la;
int st=VLabel2Id[x];
int ed=VLabel2Id[y];
EdgeNode*s=new EdgeNode;
s->valued=la;
s->adjvex=ed;
s->next=G->vertices[st].firstedge;
G->vertices[st].firstedge=s;
}

}

void Print_GF(AdjList *G)

{
for(int i=0;i<count;i++)
{
cout<<VId2Label[i];
EdgeNode*p=G->vertices[i].firstedge;
while(p)
{
cout<<"->"<<VId2Label[p->adjvex]<<"(BY"<<p->valued<<") ";
p=p->next;
}
cout<<endl;
}

}

void Delete_GF(AdjList*G)

{
for(int i=0;i<count;i++)
{
EdgeNode*q;
EdgeNode*p=G->vertices[i].firstedge;
while(p)
{
q=p;
p=p->next;
delete q;
}
G->vertices[i].firstedge=NULL;
}

}

int main()

{
AdjList G;
Create_GF(&G);
Print_GF(&G);
Delete_GF(&G);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: