2015-11-06 62 views
-1

我是C编程新手,在我的程序中遇到以下问题。我在一个函数(POLYmake)中创建了一个多项式作为链表,并且我想在主函数中返回变量poly1中的这个列表(以及第二个多项式的poly2)。 return语句的语法应该是什么?函数返回链表 - C

这里是我的代码:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 


typedef struct node 
{ 
int sunt; 
int dun; 
struct node* next; 
} Poly; 


Poly POLYmake(); 

int main() 
{ 
Poly poly1, poly2; 

printf("Doste to 1o poluwnumo : \n"); 
poly1 = POLYmake(); 

printf("Doste to 2o poluwnumo : \n"); 
poly2 = POLYmake(); 

} 

Poly POLYmake() 
{ 
Poly *head; 
Poly *curr; 
head = NULL; 

int stop = 1; 
int i = 1; 
int suntelestis, dunami; 
char x; 
while(stop != 0) 
{ 
    curr = (Poly*)malloc(sizeof(Poly)); 
    int y=1; 
    printf("Doste to suntelesti tou %dou orou : ",i); 
    scanf("%d",&suntelestis); 
    curr->sunt = suntelestis; 

    printf("Doste ti dunami tou %dou orou : ",i); 
    scanf("%d",&dunami); 
    curr->dun = dunami; 

    curr->next = head; 
    head = curr; 

    printf("Yparxei kai allos oros tou polywnumou? (Y or N) : "); 
    scanf("%s",&x); 

    while(y == 1) 
    { 
     if(x == 'Y') 
     { 
      y=0; 
      i++;     
     } 
     else if (x == 'N') 
     { 
      stop=0; 
      y=0; 
     } 
     else 
     { 
      printf("Yparxei kai allos oros tou polywnumou? (Y or N) : "); 
      scanf("%s",&x); 
     } 
    } 
} 
return ????????? ; 
} 

回答

0
Poly *POLYMake() 
{ 

... 
    return head; 
} 

应该做的伎俩。

+0

当我返回头时,我得到这条消息“[错误]返回类型'结构聚*'时不兼容的类型,但预计”聚“。如果我将POLYMake声明为指针函数,那么Poly变量poly1和poly2必须声明为指针。但根据我的练习,poly1和poly2必须是Poly变量。 –

+0

问题的原理如何? –

+0

我必须在函数中创建两个多项式,然后我必须将它们添加到另一个函数中:Poly POLYadd(Poly p,Poly q)。所以这个函数有Poly参数,它实际上是我在POLYMake函数中创建的两个多项式。 –