假设graph
您的图形有一个全局变量(因为您没有它作为参数)。
该函数将返回已删除节点的数量。
您必须使用简单的循环遍历所有列表for
:
for (int i = 0; i < graph.nodes; i++)
然后,如果要删除第一个元素,则需要将其更改Node *
为 head 数组。这是通过while
循环完成的while (first && first->distance == distance)
删除前导头后,您可以使用while (n->next)
这将给出如下内容:
static int Node_delete(Graph *graph, int distance) {
int res = 0;
for (int i = 0; i < graph->nodes; i++) {
Node *first = graph->head[i];
Node *n = first;
while (first && first->distance == distance) {
first = first->next;
free(n);
res++;
n = first;
}
while (n && n->next) {
Node *next= n->next;
if (next->distance == distance) {
n->next = next->next;
free(next);
res++;
}
n = n->next;
}
graph->head[i] = first;
}
return res;
}
您将完整和删除元素的图表
0 -> d: 2, w: 3 -> d: 2, w: 3 -> d: 4, w: 5 -> Null
1 -> d: 7, w: 6 -> d: 4, w: 6 -> d: 5, w: 4 -> Null
2 -> d: 2, w: 7 -> d: 2, w: 7 -> d: 8, w: 9 -> Null
3 -> d: 10, w: 13 -> d: 10, w: 13 -> d: 11, w: 7 -> Null
0 -> d: 2, w: 3 -> d: 2, w: 3 -> Null
1 -> d: 7, w: 6 -> d: 5, w: 4 -> Null
2 -> d: 2, w: 7 -> d: 2, w: 7 -> d: 8, w: 9 -> Null
3 -> d: 10, w: 13 -> d: 10, w: 13 -> d: 11, w: 7 -> Null
出于测试目的,完成了包含您的架构、创建、打印和删除的完整代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node Node;
typedef struct Graph
{
// An array of pointers to Node to represent an adjacency list
size_t nodes;
Node *head[];
} Graph;
// Data structure to store adjacency list nodes of the graph
struct Node
{
int distance;
int weight;
Node *next;
};
static void graph_print(Graph *graph) {
for (int i = 0; i < graph->nodes; i++) {
printf("%d -> ", i);
for (Node *n = graph->head[i]; n; n = n->next) {
printf("d: %d, w: %d -> ", n->distance, n->weight);
}
printf("Null\n");
}
}
static int Node_delete(Graph *graph, int distance) {
int res = 0;
for (int i = 0; i < graph->nodes; i++) {
Node *first = graph->head[i];
Node *n = first;
while (first && first->distance == distance) {
first = first->next;
free(n);
res++;
n = first;
}
while (n && n->next) {
Node *next= n->next;
if (next->distance == distance) {
n->next = next->next;
free(next);
res++;
}
n = n->next;
}
graph->head[i] = first;
}
return res;
}
static Node* node_create(int distance, int weight, Node *next) {
Node *n = malloc(sizeof(Node));
n->distance = distance;
n->weight = weight;
n->next = next;
return n;
}
static void node_delete(Node *n) {
if (n) {
node_delete(n->next);
free(n);
}
}
static Graph* graph_create(int nodes) {
Graph *g = malloc(sizeof(size_t) + nodes * sizeof(Node));
g->nodes = nodes;
return g;
}
static void graph_delete(Graph *graph) {
for (int i = 0; i < graph->nodes; i++) {
node_delete(graph->head[i]);
}
free(graph);
}
int main(void)
{
Graph *graph = graph_create(4);
Node *n;
n = node_create(4, 5, NULL);
n = node_create(2, 3, n);
n = node_create(2, 3, n);
graph->head[0] = n;
n = node_create(5, 4, NULL);
n = node_create(4, 6, n);
n = node_create(7, 6, n);
graph->head[1] = n;
n = node_create(8, 9, NULL);
n = node_create(2, 7, n);
n = node_create(2, 7, n);
graph->head[2] = n;
n = node_create(11, 7, NULL);
n = node_create(10, 13, n);
n = node_create(10, 13, n);
graph->head[3] = n;
graph_print(graph);
Node_delete(graph, 4);
graph_print(graph);
graph_delete(graph);
return 0;
}