关于C语言的一个问题。

hejian29 2004-12-11 07:03:55
先给出程序,其实是数据结构里的。
typedef struct nList
{
int data;
struct nList *next;
}nList,*pList; //这里一个链表的数据结构
void CreateList(nList &list,int n) //创建一个长度为n的链表。。。。不过这里有问题。
{
..........
}

问题是为什么在创建链表的那个函数头里nList &list里,加上&就会出错呢?可是书上都是这样的呀?
狂急,请高手指点。
...全文
92 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
C_020511 2004-12-11
  • 打赏
  • 举报
回复
我看,是为了值传递。
loveyou19840806 2004-12-11
  • 打赏
  • 举报
回复
C中没有引用这个语法,只有C++中才有,
函数声明部分这样写:void CreateList(nList *list,int n)

函数调用部分这样写:CreateList(&list,n) ;

chinayang9 2004-12-11
  • 打赏
  • 举报
回复
函数声明部分这样写:void CreateList(nList &list,int n)

函数调用部分这样写:CreateList(&list,n) ;

但要注意实参的list要先有一个malloc为它分配空间
hejian29 2004-12-11
  • 打赏
  • 举报
回复
换句话说怎么做到指针变理的双向值传递。
hejian29 2004-12-11
  • 打赏
  • 举报
回复
那如果要在主函数中能够调用的话
这是一个创建链表的主函数,能帮我改一下吗?
#include <stdio.h>
#include <stdlib.h>
#define DATATYPE int
typedef struct List
{
DATATYPE data;
struct List *next;
}List,*pList;
/*创建带有头结点的含有n个元素的链表*/
void CreateList(pList L,int n)
{
int i;
List *p;
L=(List*)malloc(sizeof(List));/*创建头结点*/
L->next=NULL;
for(i=0;i<n;i++)/*尾插入法插入结点*/
{
p=(List*)malloc(sizeof(List));
printf("请输入新元素的值:");
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
}
/*打印链表中的所有元素*/
void PrintList(pList L)
{
List *p;
p=L->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}

main()
{
pList Lt;
CreateList(Lt,5);
PrintList(Lt);
}
chenee543216 2004-12-11
  • 打赏
  • 举报
回复
&list 是list的地址,
如果你定义过list 则&list 是常数,如果你没有定义 list则 &list 是个无意义的数字!
void CreateList(nlist XXX ... ) 中 XXX为 nlist 类型的变量(形参)
hejian29 2004-12-11
  • 打赏
  • 举报
回复
怎么没人帮忙啊?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧