2 つの構造体を作成しました。
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)
}
}
}
実装したロジックが正しいかどうかを確認しようとしています。それが私がすべての行にコメントした理由です。ビジュアルについては、ページ上部のリンクを参照できます。