在链表中,定义一个结构体
struct Node
{
int x;
Node* next;
Node(int x)
{
data = x;
next = NULL;
}
};
并在主函数中创建一个head
此时有两种情况
第一种是
Node head;
第二种是
Node* head = new head;
此时再创建一个函数show
void show(Node *head)
{
while (head)
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
}
如果是第一种情况,则需要这样写参数
show(&head);
第二种是这样
show(head);
此时我就有几个问题:
为什么在Node结构体里面需要Node* next,是因为要获取指针吗?
为什么第一种情况传入的是解地址符(应该是传入了一个Node把)
第二种只需要传入指针
这两种有什么区别吗
常用的是哪种
谢谢大佬了