![](/img/trans.png)
[英]What kind of data structure is most efficient for a DAG given no node class?
[英]what kind of type is Node?
我必須用這個參數做 void initRT(Node *n;) 。 我應該怎么辦??
struct RtPkt{
int sourceid;
int destid;
int mincost[5];
};
struct dr_table {
int costs[5];
int nexthop[5];
};
int connectcosts[NUMNODES][NUMNODES];
/* Setup the network costs */
void initcosts() {
static int initialized = 0;
if (!initialized) {
/* initialize by hand since not all compilers allow array initilization */
connectcosts[0][1] = connectcosts[1][0] = 1; // x0
connectcosts[1][2] = connectcosts[2][1] = 1; // x1
connectcosts[2][3] = connectcosts[3][2] = 2; // x2
connectcosts[0][3] = connectcosts[3][0] = 6; // x3
connectcosts[1][4] = connectcosts[4][1] = 5; // x4
connectcosts[0][2] = connectcosts[2][0] = 4; // x5
connectcosts[3][4] = connectcosts[4][3] = 1; // x6
/* Not connected nodes */
connectcosts[0][4] = connectcosts[4][0] = connectcosts[1][3] =
connectcosts[3][1] = connectcosts[2][4] = connectcosts[4][2] =
999;
/* Loopback links */
connectcosts[0][0] = connectcosts[1][1] = connectcosts[2][2] =
connectcosts[3][3] = connectcosts[4][4] = 0;
}
}
/**
* H synarthsh ayth pairnei san orisma enan deikth se Node. To pedio
* id ths domhs prepei na einai arxikopoihmeno sto index toy komboy (p.x.
* 0 gia to node 0, 1 gia to node 1, kok) H synarthsh ayth prepei na
* arxikopoihsei to routing table toy komboy me bash ton pinaka connectcosts
* poy orizetai kai arxikopoieitai sto node.c kai katopin na steilei ena
* katallhlo RtPkt se oloys toys geitonikoys komboys toy node.
*/
struct RtPkt packet;
void initRT(Node *n;) {
int i,j;
packet.sourceid=i;
for (i=0;i<5;i++)
for (j=0;j<5;j++)
{
n->costs[i]=connectcosts[i][j];
}
packet.mincost[i]=n.costs[i];
if (packet.sourceid=0)
{
n->nexthop[0]=0;
n->nexthop[1]=1;
n->nexthop[2]=2;
n->nexthop[3]=3;
n->nexthop[4]=999;
tolayer2(packet);
packet.destid=1;
tolayer2(packet);
packet.destid=2;
tolayer2(packet);
packet.sourceid=3;
tolayer2(packet);
}
else if (packet.sourceid=1)
{
n->nexthop[0]=0;
n->nexthop[1]=1;
n->nexthop[2]=2;
n->nexthop[3]=999;
n->nexthop[4]=4;
tolayer2(packet);
packet.destid=0;
tolayer2(packet);
packet.destid=2;
tolayer2(packet);
packet.sourceid=4;
tolayer2(packet);
}
else if (packet.sourceid=2)
{
n->nexthop[0]=0;
n->nexthop[1]=1;
n->nexthop[2]=2;
n->nexthop[3]=3;
n->nexthop[4]=999;
tolayer2(packet);
packet.destid=0;
tolayer2(packet);
packet.destid=1;
tolayer2(packet);
packet.sourceid=3;
tolayer2(packet);
}
else if (packet.sourceid=3)
{
n->nexthop[0]=0;
n->nexthop[1]=999;
n->nexthop[2]=2;
n->nexthop[3]=3;
n->nexthop[4]=4;
tolayer2(packet);
packet.destid=0;
tolayer2(packet);
packet.destid=2;
tolayer2(packet);
packet.sourceid=4;
tolayer2(packet);
}
else if (packet.sourceid=4)
{
n->nexthop[0]=0;
n->nexthop[1]=999;
n->nexthop[2]=2;
n->nexthop[3]=3;
n->nexthop[4]=4;
tolayer2(packet);
packet.destid=1;
tolayer2(packet);
packet.destid=3;
tolayer2(packet);
}
}
}
您創建的結構或 class 必須是 Node 的子類,那么您應該沒有問題將該類型的 object 傳遞給 initptr function。
void yourcode()
{
// declare an instance of a node on the stack
Node n;
// pass it by pointer to the initptr function
initptr(&n);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.