在C++中,可以通過定義一個指向下一個節點的指針來實現鏈表的下一個節點。具體實現步驟如下:
struct Node {
int data;
Node* next;
};
Node* head = new Node();
head->data = 1;
head->next = nullptr;
Node* newNode = new Node();
newNode->data = 2;
newNode->next = nullptr;
head->next = newNode;
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
這樣就實現了一個簡單的鏈表,并且可以在鏈表中添加新節點、遍歷節點等操作。