为什么head指针必须为引用指针型?Node*&head?.请各位帮忙调试一下.
北狐狸 2006-03-29 03:42:48 //================================================
// lihubei@yahoo.com.cn
//================================================
//
#include<iostream.h>
typedef struct node
{
int data;
struct node *link;
} Node;
//
//================================================
//
//为什么head指针必须为引用指针型?Node*&head?.
void reverseMySingleLink ( Node* head, Node *p, Node *q)
//void reverseMySingleLink ( Node* &head, Node *p, Node *q)
{
cout<<endl<<"反序将开始__*head.data: "<<head->data<<endl;
if(!head||!head->link)
{
cout<<"空链表或单结点"<<endl;
return;
}
//至少有两个结点.
p=head->link;
head->link=0;
q=p->link;
while(q)
{
p->link=head;
head=p;
p=q;
q=q->link;
}
p->link=head;
head=p;
cout<<"反序将退出__*head.data: "<<head->data<<endl;
}
//
//================================================
//
void main()
{
Node *head,*p,*q;
head=new Node;
p=new Node;
q=new Node;
//
head->link=p;
p->link=q;
q->link=0;
//
head->data=1;
p->data=2;
q->data=3;
cout<<"反序前__ *head.data: "<<head->data<<endl;
//start reverse this single linkList.
reverseMySingleLink(head,p,q);
//
cout<<endl<<"反序后__ *head.data: "<<head->data<<endl;
}
//
//================================================