递归删除链表中的数据

时间:2017-08-16 00:33:42

标签: c++ recursion linked-list

我有一个定义为

的链接列表
struct Node{
   int data;
   Node *next;
};

struct LinkedList{
   Node *head;
};

我希望以递归方式浏览我的链表并删除具有指定数据类型的节点(并正确地重新加入节点)。我想出了如何迭代地执行此操作,但我还在努力以递归方式执行此操作。这是我到目前为止所得到的:

void deleteNodeRecursively(LinkedList* list, int value){
 if (list->head==NULL){
  return;
 } else if (list->head->data==value){
  list->head=list->head->next;
  deleteNodeRecursively(list,value);
 } else {

 }
}

基本上,我的策略是确定头部是否有数据。如果是,那么我只需用下一个节点替换头部。问题是我的其他声明,我知道我必须移动'到下一个节点。我不仅要继续前进到下一个节点,而且还必须确保它是LinkedList格式,这样才能正确使用头部。我不知道如何继续前进而不删除我的所有列表。我有复制的模糊想法?我不太清楚现在该做什么。

编辑:我不想编辑我的结构定义,因为我将它们用于我的其他程序。

2 个答案:

答案 0 :(得分:1)

有很多方法可以做。

在调用递归函数之前,我个人通过一个简单的方法来处理检查。

if(list->head->data == value)
{
    Node* temp = list->head;
    list->head = list->head->next;
    delete temp;
}
if(list->head != null)
    deleteNodeRecursively(list->head,list->head->next,value);

你的递归函数应该是,

void deleteNodeRecursively(Node* parent, Node* child, int value){
if (child == NULL){ // if null that means end of the list
    return;
}
if( child->data == value)
{
    parent->next = child->next; // attach the parent with the next of child. Thus removes the child form the list
    delete child; // clear the dynamically allocated memory
    deleteNodeRecursively(parent,parent->next,value);
}
else
    deleteNodeRecursively(child,child->next,value); // move to the next
}

附加如果可以避免递归,请避免递归。因为如果列表足够长,它将在到达结束之前消耗完整的堆栈内存。

答案 1 :(得分:0)

  

我正在努力递归地做这件事。   我的策略是确定头部是否有数据

一个好的策略,类似于MASh提案,以及我在下面的努力。

也许以下removeR()方法将有助于完成你的方法。

在这个例子中我没有提供'tail recursion'。以下方法是我的“传统”即简单实现。 (最近,我发现我很少使用列表,而更喜欢std :: vector。)

环境:

Ubuntu 15.10,64位,戴旧戴尔,

g ++ - 5(Ubuntu 5.2.1-23ubuntu1~15.10)5.2.1 20151028

代码:

#include <chrono>
// 'compressed' chrono access --------------vvvvvvv
typedef std::chrono::high_resolution_clock  HRClk_t; // std-chrono-hi-res-clk
typedef HRClk_t::time_point                 Time_t;  // std-chrono-hi-res-clk-time-point
typedef std::chrono::milliseconds           MS_t;    // std-chrono-milliseconds
typedef std::chrono::microseconds           US_t;    // std-chrono-microseconds
typedef std::chrono::nanoseconds            NS_t;    // std-chrono-nanoseconds
using   namespace std::chrono_literals;          // support suffixes like 100ms, 2s, 30us
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <cassert>


class Node_t
{
private:
   size_t   m_id;    // node indx
   int      m_data;
   Node_t*  m_next;

   static size_t M_id;

public:
   Node_t() = delete;  // disallow default ctor

   Node_t(int d) : m_id(++M_id) , m_data(d) , m_next(nullptr) { }

   ~Node_t() { std::cout << "  ~Node_t() " << show() << std::endl; }

   void appendR(Node_t* n)
      {
         if(m_next) m_next->appendR(n); // spin to end of list
         else       m_next = n;         // append to end
      }

   bool dataMatch(int v) { return (m_data == v); }
   Node_t* next() { return (m_next); }

   // when m_next->m_data matches v,  returns true, else false
   bool removeR(int v);

   std::string showR()
      {
         std::string s = show();
         if(m_next)
            s += m_next->showR(); // spin to end of list
         return (s);
      }

   std::string show()
      {
         std::stringstream ss;
         if (false) ss << "[" << std::to_string(m_id)   << "]";  // node indx
         if (false)  ss << std::to_string(m_id)   << ":";  // node indx
         ss  <<                  std::to_string(m_data) << "  ";
         return ss.str();
      }

   size_t size() {
      size_t retVal = 1;
      if (m_next) { retVal += m_next->size(); } // spin to end of list
      return retVal;
   }

}; // class Node_t

size_t Node_t::M_id = 0; // unique id for each node

class LinkedList_t
{
private:
   Node_t* m_head;

public:
   LinkedList_t() : m_head (nullptr) { }
   ~LinkedList_t() = default;

   void append(Node_t* n)
      {
         assert(n);
         if (nullptr == m_head) m_head = n;
         else                   m_head->appendR(n);
      }

   void removeR(int v);

   std::string show()
      {
         std::string s("ll is empty");
         if(m_head)
            s = m_head->showR();
         return (s);
      }

   size_t size() {
      size_t retVal = 0;
      if (m_head) retVal += m_head->size();
      return retVal;
   }

}; // class LinkedList_t


void LinkedList_t::removeR(int v)
{
   if(nullptr == m_head) // empty list check
   {
      std::cout << "\n  empty" << std::endl;
      return;
   }

   if(m_head->dataMatch(v))      // check data at head
   {
      Node_t* tmp = m_head;
      m_head      = tmp->next(); // unlink prev head
      delete  tmp;
      removeR(v);                // local recursion... node 2 might also contain v
   }

   // not empty && data at head not v then use node recursion
   if(m_head)  (void)m_head->removeR(v);
}

bool Node_t::removeR(int v)
{
   bool  match = false;
   if (m_next) match = m_next->removeR(v); // spin to end of list

   // during decurse
   if(match) // m_next->data matches v
   {
      Node_t* matchNode = m_next;  // node-to-remove
      m_next = m_next->m_next;     // remove link to node-to-remove
      delete matchNode;            // remove node
   }
   // else no remove to do

   return (m_data == v);  // continue decurse, checking for another  match
}


class T516_t
{
private:
   LinkedList_t m_ll;

public:

   T516_t() = default;
   ~T516_t() = default;

   int exec()
      {
         show("m_ll.show().1");

         std::cout << "\n  m_ll.size() = " << m_ll.size() << std::endl;

         for(int i=0; i<6; ++i)
         {
            Node_t* n = new Node_t(i+10); assert(n); // 10..15
            m_ll.append(n);
         }
         for(int i=0; i<6; ++i)
         {
            Node_t* n = new Node_t(i+10); assert(n); // 10..15
            m_ll.append(n);
         };

         show("m_ll.show().2");

         std::cout << "\n  m_ll.size() = " << m_ll.size() << std::endl;

         show("remove(15)");
         m_ll.removeR(15);

         show("m_ll.show().3");

         show("remove(10)");
         m_ll.removeR(10);

         show("m_ll.show().z");

         for (int i=0; i<6;++i)
         {
            std::cout << "\n  remove(" << i+10 << ")    " << std::endl;
            m_ll.removeR(i+10);

            show("m_ll.show()...");
         }

         return(0);
      }

private:

   void show(std::string lbl) {
      std::cout << "\n  " << lbl
                << "  sz:"   << m_ll.size()
                << "   "  << m_ll.show() << std::endl;
   }

}; // class T516_t


int main(int , char** )
{
   Time_t start_us = HRClk_t::now();

   int retVal = -1;
   {
      T516_t   t516;
      retVal = t516.exec();
   }

   auto  duration_us = std::chrono::duration_cast<US_t>(HRClk_t::now() - start_us);

   std::cout << "\n\n  FINI   " << duration_us.count() << " us" << std::endl;
   return(retVal);
}

结果:

R02: dumy524.cc
rm -f dumy524
g++-5 -m64  -O3 -ggdb -std=c++14 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Wcast-align -Wcast-qual -Wconversion -Wpointer-arith -Wunused -Woverloaded-virtual   -O0   dumy524.cc  -o dumy524  -L../../bag -lbag_i686

real    0m1.065s
user    0m0.920s
sys 0m0.100s

  m_ll.show().1  sz:0   ll is empty

  m_ll.size() = 0

  m_ll.show().2  sz:12   10  11  12  13  14  15  10  11  12  13  14  15  

  m_ll.size() = 12

  remove(15)  sz:12   10  11  12  13  14  15  10  11  12  13  14  15  
  ~Node_t() 15  
  ~Node_t() 15  

  m_ll.show().3  sz:10   10  11  12  13  14  10  11  12  13  14  

  remove(10)  sz:10   10  11  12  13  14  10  11  12  13  14  
  ~Node_t() 10  
  ~Node_t() 10  

  m_ll.show().z  sz:8   11  12  13  14  11  12  13  14  

  remove(10)    

  m_ll.show()...  sz:8   11  12  13  14  11  12  13  14  

  remove(11)    
  ~Node_t() 11  
  ~Node_t() 11  

  m_ll.show()...  sz:6   12  13  14  12  13  14  

  remove(12)    
  ~Node_t() 12  
  ~Node_t() 12  

  m_ll.show()...  sz:4   13  14  13  14  

  remove(13)    
  ~Node_t() 13  
  ~Node_t() 13  

  m_ll.show()...  sz:2   14  14  

  remove(14)    
  ~Node_t() 14  
  ~Node_t() 14  

  empty

  m_ll.show()...  sz:0   ll is empty

  remove(15)    

  empty

  m_ll.show()...  sz:0   ll is empty


  FINI   591 us