2015-11-06 165 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函數中創建的兩個多項式。 –