mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 11:06:29 +08:00
Compare commits
3 commits
ff48b86ff7
...
6def079d32
Author | SHA1 | Date | |
---|---|---|---|
|
6def079d32 | ||
|
356cc35e8a | ||
|
2ef65eaa6b |
3 changed files with 28 additions and 11 deletions
|
@ -38,11 +38,13 @@ GraphAdjList *newGraphAdjList() {
|
|||
void delGraphAdjList(GraphAdjList *graph) {
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
AdjListNode *cur = graph->heads[i];
|
||||
if (cur == NULL) {
|
||||
continue;
|
||||
}
|
||||
cur = cur->next;
|
||||
while (cur != NULL) {
|
||||
AdjListNode *next = cur->next;
|
||||
if (cur != graph->heads[i]) {
|
||||
free(cur);
|
||||
}
|
||||
cur = next;
|
||||
}
|
||||
free(graph->heads[i]->vertex);
|
||||
|
@ -61,6 +63,14 @@ AdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
AdjListNode** findNodeV2(GraphAdjList *graph, Vertex *vet) {
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
if (graph->heads[i]->vertex == vet) {
|
||||
return &(graph->heads[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 添加边辅助函数 */
|
||||
void addEdgeHelper(AdjListNode *head, Vertex *vet) {
|
||||
AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));
|
||||
|
@ -119,15 +129,17 @@ void addVertex(GraphAdjList *graph, Vertex *vet) {
|
|||
|
||||
/* 删除顶点 */
|
||||
void removeVertex(GraphAdjList *graph, Vertex *vet) {
|
||||
AdjListNode *node = findNode(graph, vet);
|
||||
AdjListNode **node = findNodeV2(graph, vet);
|
||||
assert(node != NULL);
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
AdjListNode *cur = node, *pre = NULL;
|
||||
while (cur) {
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
free(pre);
|
||||
AdjListNode **cur_ref = node, **pre_ref = NULL;
|
||||
while (*cur_ref) {
|
||||
pre_ref = cur_ref;
|
||||
*cur_ref = ((*cur_ref)->next);
|
||||
free(*pre_ref);
|
||||
*pre_ref = NULL;
|
||||
}
|
||||
AdjListNode *cur = NULL, *pre = NULL;
|
||||
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||
for (int i = 0; i < graph->size; i++) {
|
||||
cur = graph->heads[i];
|
||||
|
@ -145,7 +157,7 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) {
|
|||
// 将该顶点之后的顶点向前移动,以填补空缺
|
||||
int i;
|
||||
for (i = 0; i < graph->size; i++) {
|
||||
if (graph->heads[i] == node)
|
||||
if (graph->heads[i] == *node)
|
||||
break;
|
||||
}
|
||||
for (int j = i; j < graph->size - 1; j++) {
|
||||
|
@ -160,6 +172,9 @@ void printGraph(const GraphAdjList *graph) {
|
|||
printf("邻接表 =\n");
|
||||
for (int i = 0; i < graph->size; ++i) {
|
||||
AdjListNode *node = graph->heads[i];
|
||||
if (node == NULL) {
|
||||
continue;
|
||||
}
|
||||
printf("%d: [", node->vertex->val);
|
||||
node = node->next;
|
||||
while (node) {
|
||||
|
|
|
@ -12,6 +12,8 @@
|
|||
<p align="center">
|
||||
<a href="https://www.hello-algo.com/zh-hant/">
|
||||
<img src="https://www.hello-algo.com/zh-hant/index.assets/btn_read_online_dark.svg" width="145"></a>
|
||||
<a href="https://github.com/krahets/hello-algo/releases">
|
||||
<img src="https://www.hello-algo.com/zh-hant/index.assets/btn_download_pdf_dark.svg" width="145"></a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
|
|
|
@ -155,7 +155,7 @@
|
|||
<a href="https://github.com/krahets/hello-algo/releases">
|
||||
<img class="device-on-hover" style="height: 75%;" src="../assets/hero/pdf_ipad.png" alt="">
|
||||
<div class="text-button">
|
||||
<span>PDF(簡中)</span>
|
||||
<span>下載 PDF</span>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512">
|
||||
<path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z" />
|
||||
</svg>
|
||||
|
|
Loading…
Reference in a new issue