(11)图的表示.CPP
资源名称:datastruct1 [点击查看]
上传用户:wxj1219
上传日期:2013-01-31
资源大小:6k
文件大小:1k
源码类别:
数据结构
开发平台:
C/C++
- #include<iostream.h>
- #include<conio.h>
- #include<stdio.h>
- int NodeCount;
- char Node[20];
- char Graph[20][20];
- char Link[20][20];
- char Locate(char Char)
- {
- char Temp;
- for(Temp=0;Temp<NodeCount;Temp++)
- if(Node[Temp]==Char)
- break;
- return Temp;
- }
- void main()
- {
- char Temp1,Temp2,Temp3;
- clrscr();
- for(Temp2=0;Temp2<20;Temp2++)
- for(Temp1=0;Temp1<20;Temp1++)
- Graph[Temp2][Temp1]=0;
- for(Temp2=0;Temp2<20;Temp2++)
- for(Temp1=0;Temp1<20;Temp1++)
- Link[Temp2][Temp1]=255;
- cout<<"Input the node count:";
- cin>>NodeCount;
- cout<<"Input the node:";
- for(Temp1=0;Temp1<NodeCount;Temp1++)
- cin>>Node[Temp1];
- cout<<"Input the link:";
- Temp1=0;
- while(1)
- {
- scanf("%c,%c",&Temp1,&Temp2);
- if(Temp1=='$')
- break;
- Temp1=Locate(Temp1);
- Temp2=Locate(Temp2);
- Graph[Temp2][Temp1]=1;
- Graph[Temp1][Temp2]=1;
- }
- for(Temp2=0;Temp2<NodeCount;Temp2++)
- {
- for(Temp1=0;Temp1<NodeCount;Temp1++)
- cout<<(int)Graph[Temp2][Temp1]<<" ";
- cout<<endl;
- }
- for(Temp2=0;Temp2<NodeCount;Temp2++)
- {
- Temp3=0;
- for(Temp1=0;Temp1<NodeCount;Temp1++)
- if(Graph[Temp2][Temp1]==1)
- {
- Link[Temp2][Temp3]=Temp1;
- Temp3++;
- }
- }
- for(Temp1=0;Temp1<NodeCount;Temp1++)
- {
- cout<<Node[Temp1]<<":";
- for(Temp2=0;Temp2<NodeCount;Temp2++)
- {
- if(Link[Temp1][Temp2]==255)
- break;
- cout<<Node[Link[Temp1][Temp2]];
- }
- cout<<endl;
- }
- }