我正在尝试编写一个函数,从给定的链表中删除所有奇数元素,并返回

时间:2017-05-10 10:43:37

标签: c function linked-list

删除了一个新奇数元素列表的地址。我发现这个任务非常复杂,如果你能帮助修复或改进我的代码,我会很高兴。

这是我迄今为止所做的:

typedef struct list{
      int data;
      struct  list* next;
} List;


List* removeOddValues(List** source)
{
      List* curr= *source;
      List* prev;
      List* odd= NULL;

      while (curr)
      {
        if ((curr->data)%2==1)  //odd//
          {
           insertNodeToEnd(&odd, curr->data); //creating a new list of those odd elements//
           prev->next = curr->next;
          }
        else
          {
           prev = curr;
           curr= curr->next;
          }
      }
     return odd;   //returning the new list as wanted//
}

List* createNewNode(int newData, List*  next)
{
       List* newNode = (List)calloc(1, sizeof(List));
       newNode->data = newData;
       newNode->next = next;

       return newNode;
}

void insertNodeToEnd(List** list, type  newData) //insert a new node to list //
{
       LNode* newNode = createNewNode(newData, NULL);

    list->next= newNode;

1 个答案:

答案 0 :(得分:0)

创建单独的链表并在删除每个奇数元素时,将该特定元素插入该LL。还有新LL的头部参考并将其返回。它将是整个删除的奇数元素的LL。