/* 이것은 연결 목록에 노드를 추가하는 것입니다. 함수를 추가 할 때마다 헤드 포인터가 null로 설정되는 이유를 알아낼 수 없습니다. */ // 노드의 구조체 선언 구조체 노드 { int data; node * next; };C++에서 연결된 목록에 노드 추가
node *head;
//adding node to the head pointer
void addnode(node* head,int d)
{
node *temp = new node;
temp->data =d;
temp->next=NULL;
node* tmp=head;
if(tmp!=NULL)
{
cout<<"shal";
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=temp;
}
else
{
//cout<<temp->data;
head=temp;
}
cout<<"dh"<<head->data;
}
//main function
int main()
{`enter code here`
head=NULL;
//calling the add function
addnode(head,10);
//head is being taking as null here
addnode(head,20);
}
/* 출력 : 잘못 어디로 갔는지 dh10nulldh20null 이해에서 저를 도와주세요. 고마워. */
글로벌'head'와 매개 변수'head'가 있습니까? Eww. 한 가지만 전역 사용을 중지하십시오. – crashmstr
값을 통한 전달과 참조로 전달의 동작의 차이점을 이해해야합니다. – Speed8ump