邻接表01


bool insert_vertex(ListGraph *G, VertexType v){
if(locate_vertex(G,v) != -1){ 
    return false; 
}
if(G -> vexnum == MAX_VERTEX_NUM){ 
    return false; 
}
VNode temp ;

temp.data = v;
temp.firstarc  = NULL;
G -> vertex[G -> vexnum] = temp;
G -> vexnum++;
return true;
}


bool insert_arc(ListGraph *G, VertexType v, VertexType w){
if(locate_vertex(G,v) == -1 || locate_vertex(G,w) == -1){
    return false;
}
ArcNode *temp, *p, *q, no_sense;
p = &no_sense;
p -> adjvex = w;
p -> nextarc = NULL;
int i = 0;
int j = 0;
i = locate_vertex(G,v);
j = locate_vertex(G,w);
temp = (G -> vertex[i].firstarc);

if(temp == NULL){
    G -> vertex[i].firstarc = p;
    p -> nextarc = NULL;
    return true;
}

while(temp -> nextarc != NULL && temp -> adjvex != w){
    temp = temp -> nextarc;
}
if(temp -> adjvex == p ->adjvex){
    return false;
}
if(temp -> nextarc == NULL){
    temp -> nextarc = p;
    p -> nextarc = NULL;
    
}
if(G -> type ==UDG){
    i = locate_vertex(G,w);
    if(locate_vertex(G,w) == -1){
        return false;
    }
    //j = locate_vertex(G,w);
    temp = (G -> vertex[v].firstarc);

    if(temp == NULL){
        G -> vertex[i].firstarc = p;
        p -> nextarc = NULL;
        return true;
    }

    while(temp -> nextarc != NULL && temp -> adjvex != w){
        temp = temp -> nextarc;
    }   
    if(temp -> adjvex == p ->adjvex){
        return false;
    }
    if(temp -> nextarc == NULL){
        temp -> nextarc = p;
        p -> nextarc = NULL;
    
    }
}
G -> arcnum++;
return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值