指针引用作参数问题

来源:百度知道 编辑:UC知道 时间:2024/05/12 16:30:45
//用create创建链表,然后删除,查看链表,不知道为什么,没法运行
//指针引用和指针直接作参数有区别吗?为什么在函数中用指针作参数的
//时候create函数编译不过
#include <iostream.h>
#include <malloc.h>

typedef struct node
{
int no;
struct node *next;
} Node;

void create(Node *&h)
{
Node *s, *r;
int n;
h=NULL;
cout<<"树序(以负数结束): ";
cin>>n;
while(n>0)
{
s=(Node *)malloc(sizeof(Node));
s->no=n; s->next=NULL;
if(h=NULL)
{
h=s;
r=h;
}
else
{
r->next=s;
r=s;
}
cin>>n;
}
}

int len(const Node *h)
{
int i=0;
while(h!=NULL)
{
i++;
h=h->next;
}
return i;
}

void del(Node *&h, int i)
{
Node *p=h, *q;
if(i<1 || i>len(h))
return;
if(1==i)
{
h=h->next;
free(p);
}

#include <iostream.h>
#include <malloc.h>

typedef struct node
{
int no;
struct node *next;
} Node;

void create(Node *&h)
{
Node *s, *r;
int n;
h=NULL;
cout<<"树序(以负数结束): ";
cin>>n;
while(n>0)
{
s=(Node *)malloc(sizeof(Node));
s->no=n; s->next=NULL;
if(h==NULL) //这里应该为==
{
h=s;
r=h;
}
else
{
r->next=s;
r=s;
}
cin>>n;
}
}

int len(const Node *h)
{
int i=0;
while(h!=NULL)
{
i++;
h=h->next;
}
return i;
}

void del(Node *&h, int i)
{
Node *p=h, *q;
if(i<1 || i>len(h))
return;
if(1==i)
{
h=h->next;
free(p);
}
else
{
while(i-- > 2)
p=p->next; //*p from head to node will be deleted