mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 04:06:28 +08:00
Compare commits
3 commits
a77a9233c9
...
b61fcb97e9
Author | SHA1 | Date | |
---|---|---|---|
|
b61fcb97e9 | ||
|
292b107af2 | ||
|
2ef65eaa6b |
3 changed files with 27 additions and 12 deletions
|
@ -38,11 +38,13 @@ GraphAdjList *newGraphAdjList() {
|
||||||
void delGraphAdjList(GraphAdjList *graph) {
|
void delGraphAdjList(GraphAdjList *graph) {
|
||||||
for (int i = 0; i < graph->size; i++) {
|
for (int i = 0; i < graph->size; i++) {
|
||||||
AdjListNode *cur = graph->heads[i];
|
AdjListNode *cur = graph->heads[i];
|
||||||
|
if (cur == NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
while (cur != NULL) {
|
while (cur != NULL) {
|
||||||
AdjListNode *next = cur->next;
|
AdjListNode *next = cur->next;
|
||||||
if (cur != graph->heads[i]) {
|
free(cur);
|
||||||
free(cur);
|
|
||||||
}
|
|
||||||
cur = next;
|
cur = next;
|
||||||
}
|
}
|
||||||
free(graph->heads[i]->vertex);
|
free(graph->heads[i]->vertex);
|
||||||
|
@ -61,6 +63,14 @@ AdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {
|
||||||
return NULL;
|
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) {
|
void addEdgeHelper(AdjListNode *head, Vertex *vet) {
|
||||||
AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));
|
AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));
|
||||||
|
@ -119,15 +129,17 @@ void addVertex(GraphAdjList *graph, Vertex *vet) {
|
||||||
|
|
||||||
/* 删除顶点 */
|
/* 删除顶点 */
|
||||||
void removeVertex(GraphAdjList *graph, Vertex *vet) {
|
void removeVertex(GraphAdjList *graph, Vertex *vet) {
|
||||||
AdjListNode *node = findNode(graph, vet);
|
AdjListNode **node = findNodeV2(graph, vet);
|
||||||
assert(node != NULL);
|
assert(node != NULL);
|
||||||
// 在邻接表中删除顶点 vet 对应的链表
|
// 在邻接表中删除顶点 vet 对应的链表
|
||||||
AdjListNode *cur = node, *pre = NULL;
|
AdjListNode **cur_ref = node, **pre_ref = NULL;
|
||||||
while (cur) {
|
while (*cur_ref) {
|
||||||
pre = cur;
|
pre_ref = cur_ref;
|
||||||
cur = cur->next;
|
*cur_ref = ((*cur_ref)->next);
|
||||||
free(pre);
|
free(*pre_ref);
|
||||||
|
*pre_ref = NULL;
|
||||||
}
|
}
|
||||||
|
AdjListNode *cur = NULL, *pre = NULL;
|
||||||
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||||
for (int i = 0; i < graph->size; i++) {
|
for (int i = 0; i < graph->size; i++) {
|
||||||
cur = graph->heads[i];
|
cur = graph->heads[i];
|
||||||
|
@ -145,7 +157,7 @@ void removeVertex(GraphAdjList *graph, Vertex *vet) {
|
||||||
// 将该顶点之后的顶点向前移动,以填补空缺
|
// 将该顶点之后的顶点向前移动,以填补空缺
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < graph->size; i++) {
|
for (i = 0; i < graph->size; i++) {
|
||||||
if (graph->heads[i] == node)
|
if (graph->heads[i] == *node)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
for (int j = i; j < graph->size - 1; j++) {
|
for (int j = i; j < graph->size - 1; j++) {
|
||||||
|
@ -160,6 +172,9 @@ void printGraph(const GraphAdjList *graph) {
|
||||||
printf("邻接表 =\n");
|
printf("邻接表 =\n");
|
||||||
for (int i = 0; i < graph->size; ++i) {
|
for (int i = 0; i < graph->size; ++i) {
|
||||||
AdjListNode *node = graph->heads[i];
|
AdjListNode *node = graph->heads[i];
|
||||||
|
if (node == NULL) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
printf("%d: [", node->vertex->val);
|
printf("%d: [", node->vertex->val);
|
||||||
node = node->next;
|
node = node->next;
|
||||||
while (node) {
|
while (node) {
|
||||||
|
|
|
@ -16,5 +16,5 @@ func TestBubbleSort(t *testing.T) {
|
||||||
|
|
||||||
nums1 := []int{4, 1, 3, 1, 5, 2}
|
nums1 := []int{4, 1, 3, 1, 5, 2}
|
||||||
bubbleSortWithFlag(nums1)
|
bubbleSortWithFlag(nums1)
|
||||||
fmt.Println("冒泡排序完成后 nums1 = ", nums)
|
fmt.Println("冒泡排序完成后 nums1 = ", nums1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,5 +16,5 @@ func TestBubbleSort(t *testing.T) {
|
||||||
|
|
||||||
nums1 := []int{4, 1, 3, 1, 5, 2}
|
nums1 := []int{4, 1, 3, 1, 5, 2}
|
||||||
bubbleSortWithFlag(nums1)
|
bubbleSortWithFlag(nums1)
|
||||||
fmt.Println("泡沫排序完成後 nums1 = ", nums)
|
fmt.Println("泡沫排序完成後 nums1 = ", nums1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue