关于函数定义中linklist *L和linklist &L的问题!
本帖最后由 WP叶超 于 2017-10-18 11:47 编辑今天在学校机房进行实验,做的是一元多项式的加法题目。
在函数定义的时候我用的polylist *L,实测能运行,但是老师说这种方法只能偶然能运行(我也不知道这个偶然是什么意思...),因为polylist *L不是一个指针,一定要我改成 polylist &L
如下就是两种不同的写法:(加粗的表示不同之处)
-----------------------------------------------------------------------------------
Status poly_create(polylist *L){//创建链表
polynode *p, *q;
int len,i;
float val1;
int val2;
printf("你的多项式有几项?:");
scanf("%d",&len);
*L = (polylist)malloc(size);
q = *L;
----------------------------------------------------------------------------------
Status poly_create(polylist &L){//创建链表
polynode *p, *q;
int len,i;
float val1;
int val2;
printf("你的多项式有几项?:");
scanf("%d",&len);
L = (polylist)malloc(size);
q = L;
--------------------------------------------------------------------------------
但是两个都可以运行,并且结果都是对的,我们老师说&L是对的,*L是错的...
恳请大佬们帮我解决一下这个问题:
两个方法有什么不同之处?
附上代码(两种不同方式):
-----------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define size sizeof(polynode)
typedef int Status;
typedef int ElemType;
typedef struct polynode{
float coef; //系数
int expn; //指数
struct polynode *next;
}polynode, *polylist;
Status InitList(polylist *L){
L = (polylist)malloc(size);
if(!(*L))
exit(0);
(*L)->next = NULL;
return OK;
}
Status poly_create(polylist *L){//创建链表
polynode *p, *q;
int len,i;
float val1;
int val2;
printf("你的多项式有几项?:");
scanf("%d",&len);
*L = (polylist)malloc(size);
q = *L;
for(i = 0; i < len; ++i){
p = (polylist)malloc(size);
scanf("%f%d", &val1, &val2);
p->coef = val1;
p->expn = val2;
q->next = p;
q = p;
}
q->next = NULL;
return OK;
}
Status poly_display(polylist L){//显示链表内容
polynode *p;
p = L->next;
while (p != NULL){
printf(" %.2fX(%d) ",p->coef,p->expn);
p = p->next;
}
putchar(10);
return OK;
}
Status poly_add(polylist La, polylist Lb, polylist *Lc){
polynode *p,*q,*s,*r;
p = La->next;
q = Lb->next;
*Lc = (polylist)malloc(size);
r = *Lc;
while(p && q){
if(p->expn == q->expn && (p->coef + q->coef) != 0){
s = (polylist)malloc(size);
s->coef = (p->coef) + (q->coef);
s->expn = p->expn;
r->next = s;
r = s;
p = p->next;
q = q->next;
continue;
}
if(p->expn == q->expn && (p->coef + q->coef) == 0){
p = p->next;
q = q->next;
continue;
}
if(p->expn > q->expn){
s = (polylist)malloc(size);
s->coef = q->coef;
s->expn = q->expn;
r->next = s;
r = s;
q = q->next;
continue;
}
if(p->expn < q->expn){
s = (polylist)malloc(size);
s->coef = p->coef;
s->expn = p->expn;
r->next = s;
r = s;
p = p->next;
continue;
}
}
while( p ){
s = (polylist)malloc(size);
s->coef = p->coef;
s->expn = p->expn;
r->next = s;
r = s;
p = p->next;
}
while( q ){
s = (polylist)malloc(size);
s->coef = q->coef;
s->expn = q->expn;
r->next = s;
r = s;
q = q->next;
}
r->next = NULL;
return OK;
}
int main(){
polylist La, Lb, Lc;
InitList(&La);
InitList(&Lb);
InitList(&Lc);
poly_create(&La);
poly_display(La);
putchar(10);
poly_create(&Lb);
poly_display(Lb);
putchar(10);
poly_add(La,Lb,&Lc);
poly_display(Lc);
putchar(10);
return 0;
}
---------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define size sizeof(polynode)
typedef int Status;
typedef int ElemType;
typedef struct polynode{
float coef; //系数
int expn; //指数
struct polynode *next;
}polynode, *polylist;
Status InitList(polylist *L){
(*L)= (polylist)malloc(size);
if(!(*L))
exit(0);
(*L)->next = NULL;
return OK;
}
Status poly_create(polylist &L){//创建链表
polynode *p, *q;
int len,i;
float val1;
int val2;
printf("你的多项式有几项?:");
scanf("%d",&len);
L = (polylist)malloc(size);
q = L;
for(i = 0; i < len; ++i){
p = (polylist)malloc(size);
scanf("%f%d", &val1, &val2);
p->coef = val1;
p->expn = val2;
q->next = p;
q = p;
}
q->next = NULL;
return OK;
}
Status poly_display(polylist L){//显示链表内容
polynode *p;
p = L->next;
while (p != NULL){
printf(" %.2fX(%d) ",p->coef,p->expn);
p = p->next;
}
putchar(10);
return OK;
}
Status poly_add(polylist La, polylist Lb, polylist &Lc){
polynode *p,*q,*s,*r;
p = La->next;
q = Lb->next;
Lc = (polylist)malloc(size);
r = Lc;
while(p && q){
if(p->expn == q->expn && (p->coef + q->coef) != 0){
s = (polylist)malloc(size);
s->coef = (p->coef) + (q->coef);
s->expn = p->expn;
r->next = s;
r = s;
p = p->next;
q = q->next;
continue;
}
if(p->expn == q->expn && (p->coef + q->coef) == 0){
p = p->next;
q = q->next;
continue;
}
if(p->expn > q->expn){
s = (polylist)malloc(size);
s->coef = q->coef;
s->expn = q->expn;
r->next = s;
r = s;
q = q->next;
continue;
}
if(p->expn < q->expn){
s = (polylist)malloc(size);
s->coef = p->coef;
s->expn = p->expn;
r->next = s;
r = s;
p = p->next;
continue;
}
}
while( p ){
s = (polylist)malloc(size);
s->coef = p->coef;
s->expn = p->expn;
r->next = s;
r = s;
p = p->next;
}
while( q ){
s = (polylist)malloc(size);
s->coef = q->coef;
s->expn = q->expn;
r->next = s;
r = s;
q = q->next;
}
r->next = NULL;
return OK;
}
int main(){
polylist La, Lb, Lc;
InitList(&La);
InitList(&Lb);
InitList(&Lc);
poly_create(La);
poly_display(La);
putchar(10);
poly_create(Lb);
poly_display(Lb);
putchar(10);
poly_add(La,Lb,Lc);
poly_display(Lc);
putchar(10);
return 0;
}
------------------------------------------------------------
可能是担心结构体typedef的指针别名忘记写*吧?或者防止形参写错,二次指针忘加*?
我觉得没什么区别,都是能运行的,而且没出现过错误{:10_272:} 丶忘却的年少o 发表于 2017-10-18 11:51
可能是担心结构体typedef的指针别名忘记写*吧?或者防止形参写错,二次指针忘加*?
我觉得没什么区别,都 ...
我也一直用的是*L,一直没出过错。
但是想想老师说的 polylist *L不是一个指针,又觉得我这个方法行不通了。{:5_107:}{:5_107:} WP叶超 发表于 2017-10-18 11:54
我也一直用的是*L,一直没出过错。
但是想想老师说的 polylist *L不是一个指针,又觉得我这个方法行不通 ...
啊?从 polylist *L来看,polylist 本身是一个指针类型,polylist *应该是指向该指针类型的指针。似乎可以这么说{:10_272:}
其实我也不太懂,还在学{:10_319:} 两种都可以实现,但是按你老师的方法来写是个好习惯,p是指针,*p就是指向指针的指针,有点拗口是吧。 Status poly_create(polylist &L)
c语言不能这么干吧,这是c++的语法。传引用调用。
c的话得用二级指针来实现吧。。。。。{:9_241:} 用引用更加安全和易懂,在传递参数的时候直接扔进去就行了,可以有效避免传递错误的格式,但是指针就不行,必须得是地址才行,同时要求操作的时候要更小心
页:
[1]