我正在尝试编写一个函数,该函数从给定的链表中删除所有奇数元素,并返回 [英] I am trying to write a function that removes all the odd elements from a given linked-list and also returns

查看:32
本文介绍了我正在尝试编写一个函数,该函数从给定的链表中删除所有奇数元素,并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除了新的奇数元素列表的地址.我发现此任务相当复杂,如果您能帮助修复或改进我的代码,我将感到很高兴.

an adress of a new list of the odd elements removed. I find this task quite complicated, and I would be happy if you could help to repair or improve my code.

这是我到目前为止所做的:

Here is what I've done by far:

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;

推荐答案

创建一个单独的链表,并在删除每个奇数元素时,将该特定元素插入该LL.还应拥有新LL的主要参考,然后将其退回.这将是全部删除的奇数元素的LL.

Create a separate linkedlist and on removing every odd element, insert that particular element to that LL. Also have the head reference of the new LL and return it. It will be a LL of entire removed odd elements.

这篇关于我正在尝试编写一个函数,该函数从给定的链表中删除所有奇数元素,并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆