1、已知图的领结链表,设计算法生成相应的逆领接链表,时间复杂度为O(N+E)
思路:
- 遍历所有顶点的边,一边遍历,一边创建逆领接表,指向与顶点互换
- 知识点补充
- 邻接表:反映的是顶点出度的情况。
- 逆邻接表:反映的是顶点的入度情况。
代码:
typedef struct ArcNode{ //领接边
int adjvex;
struct ArcNode *next;
}ArcNode;
typedef struct VNode{ //领接顶点
int data;
struct ArcNode *firstarc;
}VNode;
typedef struct AGraph{ //领接表
VNode adjlist[maxsize];
int vexnum, edgenum;
}AGraph;
//思路:层次遍历每一条边,然后把每条边的指向的节点改为顶点节点,其起始节点为其指向节点
void GetReserseAdjlist(AGraph *G, AGraph *R){ //G为领接链表,R为逆领接链表
R->edgenum = G->edgenum; //初始化逆领接链表
R->vexnum = G->vexnum;
for(int i = 0; i < G->vexnum; i++){
R->adjlist[i].data = G->adjlist[i].data;
R->adjlist[i].firstarc = NULL;
}
for(int i =0; i < G->vexnum; i++){ //遍历领接表
ArcNode *p = G->adjlist[i].firstarc;
while(p!=NULL){
struct ArcNode *temp = (struct ArcNode *)malloc(sizeof(struct ArcNode));
temp->adjvex = i; //指向换一下
temp->next = R->adjlist[p->adjvex].firstarc; //不带头结点的头插法
R->adjlist[p->adjvex].firstarc = temp;
p = p->next; //继续往下找
}
}
}
逆邻接链表生成算法
1万+

被折叠的 条评论
为什么被折叠?



