Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 该函数使用链表分隔偶数和奇数_C_Linked List_Singly Linked List - Fatal编程技术网

C 该函数使用链表分隔偶数和奇数

C 该函数使用链表分隔偶数和奇数,c,linked-list,singly-linked-list,C,Linked List,Singly Linked List,我正在尝试,从用户那里获取一个数字并创建两个链表,一个只有偶数,另一个有奇数,我必须返回插入的总数,并通过引用传递链表。 我的主要问题是2个新链表的返回,这里是我到目前为止所做的。我犯了一个错误 错误C2440“函数”:无法从“节点**”转换为“节点” #include <stdio.h> #include <stdlib.h> typedef struct node{ int data; struct node

我正在尝试,从用户那里获取一个数字并创建两个链表,一个只有偶数,另一个有奇数,我必须返回插入的总数,并通过引用传递链表。 我的主要问题是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;
        }
    }

如果您想通过引用进行传递,可以按以下方式进行

int get_values(node **even, node **odd) {
    int value, counter_total = 0;

    printf("enter value:");
    scanf_s("%d", &value);

    while (value != -1) {
        if (value % 2 == 0) {
            while(*even != NULL) even= &(*even)->next; //Move the pointer till last node. 

            *even = (node *)malloc(sizeof(node));
            (*even)->data = value;//current value
            (*even)->next = NULL;
            counter_total++;
        }
        else {
            while(*odd != NULL) odd= &(*odd)->next;
            *odd = (node *)malloc(sizeof(node));
            (*odd)->data = value;//current value
            (*odd)->next = NULL;
            counter_total++;
        }
        printf("enter a value positive value");
        scanf_s("%d", &value);
    }

    return counter_total;
}

你的代码有很多错误

getvalues的函数定义应该有一个双指针。 在函数中,您正在对函数参数进行malloc。而您需要malloc一个局部变量并将其添加到列表中。 当单个do while循环足够时,您将添加不必要的代码重复。 请参阅下面的固定代码

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

即使_节点是一个指针,get_值也接受指针,但您正在传递两个指针的地址。好的,我应该省略指针解引用。但是仍然亮着不起作用。你必须改变那些指针的值,对吗?因此,可能必须更改函数的签名。在//2之前和插入时,代码的用途是什么?你能评论一下你的代码吗?看到了吗,您使用了双指针,因为您想返回新链表的两个地址?没有双指针难道不可能吗?因为我们可以在主节点中声明2种类型的对象。并且通过引用传递它们不知道为什么使用指针指向指针…函数中的指针本身可以更改值。想象一下指针最初为NULL,并且添加了列表的第一个元素的情况。然后节点指针将有一个新值,而不是NULL。这个改变的指针需要从函数中取出,所以我们通过引用传递指针本身。i、 e.双指针。