为什么我的第二个函数 (push2() ) 不工作?
why my second function (push2() ) not working?
**当我将 head 引用作为函数参数时它可以工作但是当我将 head 不工作时
我哪里弄错了?
帮帮我
**
#c++ ================================================ ================================================ ================================================ ================================================ ================================================ ================================================ ================================================ ================================================ ==============#
#include <iostream>
using namespace std;
struct node{
int data ;
node *link;
};
void push1(node** head_ref, int new_data)
{
node* new_node = new node();
new_node->data = new_data;
new_node->link = (*head_ref);
(*head_ref) = new_node;
}
void push2(node *head,int dat){ //it is no working ?
node *new_node = new node();
new_node->data = dat;
new_node->link = head;
head = new_node;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
node *head = new node();
head->data = 9;
head ->link = NULL;
push1(&head,8);
push1(&head ,7);
push1(&head ,6);
push2(head,5); //no working ?
node *ptr = head;
while(ptr !=NULL){
cout << ptr->data << " ";
ptr = ptr->link;
}
return 0;
}
第一个问题:
void push2(node *head,int dat){ //it is no working ?
好的,push2
的第一个参数应该是node *
。
node *head = new node();
而head
是一个node *
。
push1(&head,8);
呃,什么?为什么 &head
?如果 push2
取一个 node *
,而 head
是 一个 node *
,为什么要将 &head
传递给 push
?
第二个问题:
void push2(node *head,int dat){ //it is no working ?
node *new_node = new node();
new_node->data = dat;
new_node->link = head;
head = new_node; // there are two variables called 'head', which does this change?
}
为什么要在函数结束前修改 head
。当函数结束时,head
不再存在,因为它是函数的局部变量。什么都看不到新值。
您可能希望函数接受 node **
并传递给它 &head
。然后当它修改*head
时,它会修改调用者中的head
。您也可以通过引用传递 head
。
顺便说一下,在您获得更多经验之前,您应该避免为两个截然不同的变量赋予相同的名称。通过调用函数的参数 head
并调用 main
中的指针也 head
,你可能会让自己认为改变一个会改变另一个,当然,它不会.它们是两个具有相同名称但作用域不同的不同变量。
**当我将 head 引用作为函数参数时它可以工作但是当我将 head 不工作时
我哪里弄错了? 帮帮我
** #c++ ================================================ ================================================ ================================================ ================================================ ================================================ ================================================ ================================================ ================================================ ==============#
#include <iostream>
using namespace std;
struct node{
int data ;
node *link;
};
void push1(node** head_ref, int new_data)
{
node* new_node = new node();
new_node->data = new_data;
new_node->link = (*head_ref);
(*head_ref) = new_node;
}
void push2(node *head,int dat){ //it is no working ?
node *new_node = new node();
new_node->data = dat;
new_node->link = head;
head = new_node;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
node *head = new node();
head->data = 9;
head ->link = NULL;
push1(&head,8);
push1(&head ,7);
push1(&head ,6);
push2(head,5); //no working ?
node *ptr = head;
while(ptr !=NULL){
cout << ptr->data << " ";
ptr = ptr->link;
}
return 0;
}
第一个问题:
void push2(node *head,int dat){ //it is no working ?
好的,push2
的第一个参数应该是node *
。
node *head = new node();
而head
是一个node *
。
push1(&head,8);
呃,什么?为什么 &head
?如果 push2
取一个 node *
,而 head
是 一个 node *
,为什么要将 &head
传递给 push
?
第二个问题:
void push2(node *head,int dat){ //it is no working ?
node *new_node = new node();
new_node->data = dat;
new_node->link = head;
head = new_node; // there are two variables called 'head', which does this change?
}
为什么要在函数结束前修改 head
。当函数结束时,head
不再存在,因为它是函数的局部变量。什么都看不到新值。
您可能希望函数接受 node **
并传递给它 &head
。然后当它修改*head
时,它会修改调用者中的head
。您也可以通过引用传递 head
。
顺便说一下,在您获得更多经验之前,您应该避免为两个截然不同的变量赋予相同的名称。通过调用函数的参数 head
并调用 main
中的指针也 head
,你可能会让自己认为改变一个会改变另一个,当然,它不会.它们是两个具有相同名称但作用域不同的不同变量。