我创建了两个结构:
typedef struct mainNode {
int theNode;
int visited;
struct mainNode *next;
} node_rec, *node_ptr;
和
typedef struct neighborNode {
struct neighborNode *next;
} neighbor_node_rec, *neighbor_node_ptr;
我需要将其插入列表:
void insert(node_ptr head, int theNodeVal, int neighborNodeVal, int weight) {
//if list is empty
if (head == NULL) {
head = (node_ptr) malloc(sizeof (node_rec)); //create head of list
head->theNode = theNodeVal; //set head value to node value
head->next = NULL; //point to null
}
//while list is not pointing no null
while (head != NULL) {
//if node IS NOT equal to node value
if (head->theNode != theNodeVal) {
head->theNode = theNodeVal; //set head value (new node) to node value
head->next = tail; //connect to next node
tail->next = NULL; //point to null
}
else
{
//if node IS equal to node value (the node already exists)
tail->next = head // head is the new tail
head->neighbor = n; //point at the neighbor of head (new tail)
}
}
}
我正在尝试查看我实现的逻辑是否正确。这就是我评论每一行的原因。您可以参考页面顶部的链接以获得视觉效果。