#include <iostream>
using namespace std;
struct Node
{
int val;
Node* next;
};
/* function: 增加一个新的头结点 */
void add(Node* head, int new_value)
{
Node* new_head = new Node{new_value, head};
head = new_head;
}
/* function: 遍历链表 */
void travel(Node* head)
{
for(Node* it = head; it != nullptr; it = it->next)
{
cout << it->val << ' ';
}
cout << endl;
}
int main()
{
Node nodeA{5, nullptr}, nodeB{6, &nodeA}, nodeC{7, &nodeB};
Node * head = &nodeC;
travel(head);
add(head, 4);
travel(head);
return 0;
}
上述代码的执行结果是什么?为什么?