gpt4 book ai didi

c - 函数返回链表 - C

转载 作者:行者123 更新时间:2023-11-30 19:41:26 24 4
gpt4 key购买 nike

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

这是我的代码:

#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 ????????? ;
}

最佳答案

Poly *POLYMake()
{

...
return head;
}

应该可以解决问题。

关于c - 函数返回链表 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33576809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com