我正在尝试从用户那里获取一个数字并创建2个链接列表,一个列表仅包含偶数,另一个列表包含奇数,我必须返回已插入的总数,并通过引用传递链接列表。 我的主要问题是返回2个新的链表,这是我到目前为止所做的。我得到一个错误 错误C2440“功能”:无法从“节点**”转换为“节点”。
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
struct node* head;
int get_values(node,node);
void main() {
node *even_node,*odd_node;
get_values(&even_node,&odd_node);
}
int get_values(node *even, node *odd) {
int value, counter_total = 0;
node *curr_even;
node *curr_odd;
head = NULL;
printf("enter value:");
scanf_s("%d", &value);
if (value == -1) {
return NULL;
}
if (value % 2 == 0) {
even = (node*)malloc(sizeof(node));
curr_even = even;
even->data = value;
counter_total++;
}
else {
odd = (node*)malloc(sizeof(node));
curr_odd = odd;
odd->data = value;
counter_total++;
}
//2nd and on insertion.
while (value != -1) {
printf("enter a value positive value");
scanf_s("%d", &value);
if (value == -1) {
curr_even->next = NULL;
curr_odd->next = NULL;
break;
}
else if (value % 2 == 0) {
curr_even->next = (node *)malloc(sizeof(node));
curr_even = curr_even->next;
curr_even->data = value;//current value
counter_total++;
}
else {
curr_odd->next = (node*)malloc(sizeof(node));
curr_odd = curr_odd->next;
curr_odd->data = value; //current value
counter_total++;
}
return counter_total;
}
}
答案 0 :(得分:1)
您的代码有很多错误。
getvalues
的函数定义应该有一个双指针。malloc
正在使用函数参数。当您需要分配局部变量并将其添加到列表中时。请参阅下面的固定代码
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node;
struct node* head;
int get_values(node **,node **);
void main() {
node *even_node= NULL,*odd_node= NULL;
get_values(&even_node,&odd_node);
}
int get_values(node **even, node **odd) {
int value, counter_total = 0;
node *curr_even;
node *curr_odd;
node *new_node;
do
{
printf("enter value:");
scanf("%d", &value);
if (value == -1) {
return counter_total;
}
if (value % 2 == 0)
{
new_node = (node*)malloc(sizeof(node));
new_node -> data = value;
new_node -> next = NULL;
if (*even == NULL)
{
*even = new_node;
curr_even = *even;
}
else
{
curr_even ->next = new_node;
curr_even = curr_even -> next;
}
counter_total++;
}
else
{
new_node = (node*)malloc(sizeof(node));
new_node -> data = value;
new_node -> next = NULL;
if (*even == NULL)
{
*even = new_node;
curr_even = *even;
}
else
{
curr_even ->next = new_node;
curr_even = curr_even -> next;
}
counter_total++;
}
}while (1);
}
答案 1 :(得分:0)
如果您想通过引用获得通过,可以按照以下步骤进行。
import React from 'react';
import { TouchableOpacity } from 'react-native';
import PropTypes from 'prop-types';
import _ from 'lodash';
const TouchableDebounce = props => (
<TouchableOpacity
{...props}
onPress={
_.debounce(props.onPress, props.debounceTime, props.options)
}
>
{props.children}
</TouchableOpacity>
);
TouchableDebounce.propTypes = {
onPress: PropTypes.func.isRequired,
children: PropTypes.element,
options: PropTypes.objectOf(PropTypes.any),
debounceTime: PropTypes.number,
};
TouchableDebounce.defaultProps = {
children: null,
options: { 'leading': true, 'trailing': false },
debounceTime: 500,
};
export default TouchableDebounce;