网络拓扑结构:
A、B、C、D为路由器,1、2、3、4、5为网络
采用邻接表
胡乱一写,代码相当冗余,也没对某些方法进行封装
采用邻接表
胡乱一写,代码相当冗余,也没对某些方法进行封装
#include "stdio.h"
#include<windows.h>
typedef struct RouNode
{
char router;
struct RouNode *nextrou;
} RouNode;
void main()
{
//声明并初始化邻接表
RouNode rounode[4];
RouNode *next;
RouNode *A1 = new RouNode();
RouNode *A2 = new RouNode();
RouNode *A3 = new RouNode();
RouNode *B1 = new RouNode();
RouNode *B2 = new RouNode();
RouNode *C1 = new RouNode();
RouNode *C2 = new RouNode();
RouNode *C3 = new RouNode();
RouNode *D1 = new RouNode();
RouNode *D2 = new RouNode();
rounode[0].router = 'A';
rounode[0].nextrou = B1; B1->nextrou = D1; B1->router = 'B'; D1->router = 'D'; D1->nextrou = C3; C3->router = 'C'; C3->nextrou = NULL;
rounode[1].router = 'B';
rounode[1].nextrou = A1; A1->router = 'A'; A1->nextrou = C1; C1->router = 'C'; C1->nextrou = NULL;
rounode[2].router = 'C';
rounode[2].nextrou = B2; B2->router = 'B'; B2->nextrou = D2; D2->router = 'D'; D2->nextrou = A3; A3->router = 'A'; A3->nextrou = NULL;
rounode[3].router = 'D';
rounode[3].nextrou = A2; A2->router = 'A'; A2->nextrou = C2; C2->router = 'C'; C2->nextrou = NULL;
int A[6][3] = {3,1,'*',1,1,'*',2,1,'*',0,0,0,0,0,0,0,0,0};
int B[6][3] = {3,1,'*',5,1,'*',0,0,0,0,0,0,0,0,0,0,0,0};
int C[6][3] = {5,1,'*',4,1,'*',2,1,'*',0,0,0,0,0,0,0,0,0};
int D[6][3] = {1,1,'*',4,1,'*',0,0,0,0,0,0,0,0,0,0,0,0};
int x[6][3] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int y[6][3] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
while(1)
{
for(int i = 0; i < 4; i++)
{
if(rounode[i].router == 'A')
{
for(int m = 0; m < 6; m++)
{
x[m][0] = A[m][0];x[m][1] = A[m][1];x[m][2] = A[m][2];
}
}
if(rounode[i].router == 'B')
{
for(int m = 0; m < 6; m++)
{
x[m][0] = B[m][0];x[m][1] = B[m][1];x[m][2] = B[m][2];
}
}
if(rounode[i].router == 'C')
{
for(int m = 0; m < 6; m++)
{
x[m][0] = C[m][0];x[m][1] = C[m][1];x[m][2] = C[m][2];
}
}
if(rounode[i].router == 'D')
{
for(int m = 0; m < 6; m++)
{
x[m][0] = D[m][0];x[m][1] = D[m][1];x[m][2] = D[m][2];
}
}
// printf("%c\n",rounode[i].router);
next = rounode[i].nextrou;
while(next)
{
if(next->router == 'A')
{
for(int m = 0; m < 6; m++)
{
y[m][0] = A[m][0];y[m][1] = A[m][1];y[m][2] = A[m][2];
}
}
if(next->router == 'B')
{
for(int m = 0; m < 6; m++)
{
y[m][0] = B[m][0];y[m][1] = B[m][1];y[m][2] = B[m][2];
}
}
if(next->router == 'C')
{
for(int m = 0; m < 6; m++)
{
y[m][0] = C[m][0];y[m][1] = C[m][1];y[m][2] = C[m][2];
}
}
if(next->router == 'D')
{
for(int m = 0; m < 6; m++)
{
y[m][0] = D[m][0];y[m][1] = D[m][1];y[m][2] = D[m][2];
}
}
//邻居的路由表给自己,并更新
int m = 0;
int location = 0;
while(y[m][0] != 0)
{
int n = 0;bool have = false;
// printf("y中%d\n",y[m][0]);
while(x[n][0] != 0)
{
// printf("x中%d\n",x[n][0]);
if(x[n][0] == y[m][0])
{
have = true;
location = n;
}
n++;
}
if(!have)
{
//路由器中不存在此记录
x[n][0] = y[m][0];
x[n][1] = y[m][1] + 1;
x[n][2] = next->router;
// printf("添加成功\n");
}
else
{
//路由器中有此记录,判断下一跳是否为邻居
if(x[location][2] == next->router)
{
//下一跳为邻居
x[location][1] = y[m][1] + 1;
}
else
{
//下一跳不为邻居,判断距离
if(x[location][1] > y[m][1] + 1)
{
x[location][1] = y[m][1] + 1;
x[location][2] = next->router;
}
}
}
m++;
}
next = next->nextrou;
//转换之后,将x赋给A、B、C、D
if(rounode[i].router == 'A')
{
for(int m = 0; m < 6; m++)
{
for(int n = 0; n < 3; n++)
{
A[m][n] = x[m][n];
}
}
}
if(rounode[i].router == 'B')
{
for(int m = 0; m < 6; m++)
{
for(int n = 0; n < 3; n++)
{
B[m][n] = x[m][n];
}
}
}
if(rounode[i].router == 'C')
{
for(int m = 0; m < 6; m++)
{
for(int n = 0; n < 3; n++)
{
C[m][n] = x[m][n];
}
}
}
if(rounode[i].router == 'D')
{
for(int m = 0; m < 6; m++)
{
for(int n = 0; n < 3; n++)
{
D[m][n] = x[m][n];
}
}
}
}
}
int t = 0;
int n1 = 0,n2 = 0,n3 = 0,n4 = 0;
printf("----------------\n路由器A的路由表:\n");
t = 0;
while(C[t][0] != 0)
{
printf("---%d---%d---%c---\n",A[t][0],A[t][1],A[t][2]);
t++;n1++;
}
printf("----------------\n路由器B的路由表:\n");
t = 0;
while(C[t][0] != 0)
{
printf("---%d---%d---%c---\n",B[t][0],B[t][1],B[t][2]);
t++;n2++;
}
printf("----------------\n路由器C的路由表:\n");
t = 0;
while(C[t][0] != 0)
{
printf("---%d---%d---%c---\n",C[t][0],C[t][1],C[t][2]);
t++;n3++;
}
printf("----------------\n路由器D的路由表:\n");
t = 0;
while(C[t][0] != 0)
{
printf("---%d---%d---%c---\n",D[t][0],D[t][1],D[t][2]);
t++;n4++;
}
if(n1==n2 && n2==n3 && n3==n4 && n4==5)
{
break;
}
Sleep(2000);
}
}