C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题

Problems w/ callback function over integers of a LinkedList nested within another LinkedList in C++

本文关键字:LinkedList 回调 问题 函数 整数 中的 嵌套 另一个 C++      更新时间:2023-10-16

我在迭代和应用回调函数时遇到问题,该函数嵌套在另一个链接列表中。调用board.forEach(printBoard);,我没有得到76543217654321的预期输出,相反,我没有得到任何输入。我很难弄清楚问题,因为A.forEach(printByte);确实返回了7654321.如果您知道我的代码出了什么问题,请告诉我。谢谢。

template <class T> 
class LinkedList {
  struct Node {
      T x;
      Node *next;
  };
  private:
    Node *head;
  public:
    LinkedList(){
        head = NULL;
    }
    ~LinkedList(){
        Node *next = head;
        while(next) {  
            Node *deleteMe = next;
            next = next->next;
            delete deleteMe;
        }
    }
    // prepend a new value to the beginning of the list
    void add(T val){
        Node *n = new Node();
        n->x = val;
        n->next = head;
        head = n;
    }
    void forEach(void (*callback)(T)) {
      Node *n = head;
      while (n != NULL) {
        (*callback)(n->x);
        n = n->next;
      }
    }
};
void printInt(int x) {
  Serial.print(x);
}
void printBoard(LinkedList<int> x) {
  x.forEach(printInt);
}
void setup() {
  Serial.begin(9600); 
  LinkedList<int> A;
  A.add(1);
  A.add(2);
  A.add(3);
  A.add(4);
  A.add(5);
  A.add(6);
  A.add(7);
  A.forEach(printInt); // returns 7654321
  LinkedList<LinkedList<int>> board;
  board.add(A);
  board.add(A);
  board.forEach(printBoard); // returns nothing
}

我认为代码有一些内存问题。节点的地址(指针(重复(双重释放问题(。如果要复制结构,则需要为链表分配新的内存和重新寻址节点。

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std ;
template <class T> 
class LinkedList {
  struct Node {
      T *x;
      Node *next;
  };
  private:
    Node *head;
  public:
    LinkedList(){
        printf("constructor:%pn", this) ;
        head = NULL;
    }
    ~LinkedList(){
        printf("destructor:%pn", this) ;
        Node *next = head;
        while(next) {  
            Node *deleteMe = next;
            next = next->next;
            printf("delete node:%pn", deleteMe) ;
            delete deleteMe->x ;    // x
            delete deleteMe;
        }
    }
    LinkedList* Dup() {
        LinkedList *newList = new LinkedList<T>() ;
        Node *n = head ;
        while (n != NULL) {
            T *newx = new T(); 
            *newx=*(n->x) ;
//          newList->addTail(n->x) ;
            newList->addTail(newx) ;
            n = n->next;
        }
        return newList ;
    }
    // prepend a new value to the beginning of the list
    // val : caller must allocate memory.
    void add(T *val){
        Node *n = new Node();
        printf(" addhead: new alloc node %pn", n) ;
//      T *newval = new T();
//      *newval = *val ;
//        n->x = newval;
        n->x = val ;
        n->next = head;
        head = n;
    }
    // add to end
    // val : caller must allocate memory.
    void addTail(T *val) {
        Node *n = new Node();
        Node *l = head ;
        printf(" addtail: new alloc node %pn", n) ;
//      T *newval = new T();
//      *newval = *val ;
//        n->x = newval;
        n->x = val ;
        n->next = NULL ;
        if ( l==NULL ) {
            head = n ;
            return ;
        }
        while(l->next!=NULL) {
            l = l->next ;
        }
        l->next = n ;
    }
    void forEach(void (*callback)(T*)) {
      Node *n = head;
      while (n != NULL) {
        (*callback)(n->x);
        n = n->next;
      }
      printf("n");
    }
};
void printInt(int *x) {
//  Serial.print(x);
    printf("%d ", *x) ;
}
void printBoard(LinkedList<int> *x) {
  x->forEach(printInt);
}
int main() {
  //Serial.begin(9600); 
  printf("make An") ;
  int avail_Afree=0 ;
#if 0
    // use stack memory
  LinkedList<int> A;
  LinkedList<int> *pA=&A;
#else
    // use heap
  LinkedList<int> *pA = new LinkedList<int>();
  avail_Afree=1 ;
#endif
  int val=0 ;
  for (int i=1; i<=7; i++) {
    printf("add node:%dn", i) ;
//    val=i ;
    int *pval = new int() ;
    *pval = i ;
//    pA->add(&val);
    pA->add(pval);
  }
  pA->forEach(printInt);

    // prepare copied data.
  LinkedList<int> *pA2, *pA3 ;
printf("make A2n") ;
  pA2 = pA->Dup() ;
  pA2->forEach(printInt) ;
printf("make A3n") ;
  pA3 = pA->Dup() ;
  pA3->forEach(printInt) ;

    // make board
  LinkedList<LinkedList<int> > *pBoard;
  printf("make Boardn") ;
  pBoard = new LinkedList<LinkedList<int> > () ;
    printf("  Board add: A2n") ;
  pBoard->add(pA2);
    printf("  Board add: A3n") ;
  pBoard->add(pA3);
    printf(" print Boardn") ;
  pBoard->forEach(printBoard); // returns nothing
  if ( avail_Afree ) {
    printf("delete pA:%pn", pA) ;
    delete pA ;
  }
/* 
  printf("delete pA2:%pn", pA2) ;
  delete pA2 ;
  printf("delete pA3:%pn", pA3) ;
  delete pA3 ;
  // pBoard will free A2,A3.
*/
  printf("delete pBoard:%pn", pBoard) ;
  delete pBoard ;
  return 0 ;
}

输出是这个

make A
constructor:0x7fb8f8d00000
add node:1
 addhead: new alloc node 0x7fb8f8d00020
add node:2
 addhead: new alloc node 0x7fb8f8d00040
add node:3
 addhead: new alloc node 0x7fb8f8d00060
add node:4
 addhead: new alloc node 0x7fb8f8d00080
add node:5
 addhead: new alloc node 0x7fb8f8d000a0
add node:6
 addhead: new alloc node 0x7fb8f8d000c0
add node:7
 addhead: new alloc node 0x7fb8f8d000e0
7 6 5 4 3 2 1 
make A2
constructor:0x7fb8f8d000f0
 addtail: new alloc node 0x7fb8f8d00110
 addtail: new alloc node 0x7fb8f8d00130
 addtail: new alloc node 0x7fb8f8d00150
 addtail: new alloc node 0x7fb8f8d00170
 addtail: new alloc node 0x7fb8f8d00190
 addtail: new alloc node 0x7fb8f8d001b0
 addtail: new alloc node 0x7fb8f8d001d0
7 6 5 4 3 2 1 
make A3
constructor:0x7fb8f8d001e0
 addtail: new alloc node 0x7fb8f8d00200
 addtail: new alloc node 0x7fb8f8d00220
 addtail: new alloc node 0x7fb8f8d00240
 addtail: new alloc node 0x7fb8f8d00260
 addtail: new alloc node 0x7fb8f8d00280
 addtail: new alloc node 0x7fb8f8d002a0
 addtail: new alloc node 0x7fb8f8d002c0
7 6 5 4 3 2 1 
make Board
constructor:0x7fb8f8d002d0
  Board add: A2
 addhead: new alloc node 0x7fb8f8d002e0
  Board add: A3
 addhead: new alloc node 0x7fb8f8d002f0
 print Board
7 6 5 4 3 2 1 
7 6 5 4 3 2 1 
delete pA:0x7fb8f8d00000
destructor:0x7fb8f8d00000
delete node:0x7fb8f8d000e0
delete node:0x7fb8f8d000c0
delete node:0x7fb8f8d000a0
delete node:0x7fb8f8d00080
delete node:0x7fb8f8d00060
delete node:0x7fb8f8d00040
delete node:0x7fb8f8d00020
delete pBoard:0x7fb8f8d002d0
destructor:0x7fb8f8d002d0
delete node:0x7fb8f8d002f0
destructor:0x7fb8f8d001e0
delete node:0x7fb8f8d00200
delete node:0x7fb8f8d00220
delete node:0x7fb8f8d00240
delete node:0x7fb8f8d00260
delete node:0x7fb8f8d00280
delete node:0x7fb8f8d002a0
delete node:0x7fb8f8d002c0
delete node:0x7fb8f8d002e0
destructor:0x7fb8f8d000f0
delete node:0x7fb8f8d00110
delete node:0x7fb8f8d00130
delete node:0x7fb8f8d00150
delete node:0x7fb8f8d00170
delete node:0x7fb8f8d00190
delete node:0x7fb8f8d001b0
delete node:0x7fb8f8d001d0