Реализация большой тройки в связанном списке с использованием рекурсии - PullRequest
0 голосов
/ 26 сентября 2019

У меня много проблем, когда я пытаюсь заставить моего конструктора копирования и оператора присваивания выполнить домашнюю задачу.Я предполагаю реализовать их рекурсивно.

Я получаю предупреждение о том, что

variable 'LList::head' is uninitialized on `LList::LList(const LList& a)`

Может ли кто-нибудь помочь мне с этим?Вот мой код

class  LList {
   public:
      LList();
      void cons(int x);
      void append(int x);
      LList(const LList&);
      LList& operator = (const LList&);
      ~LList();
   private:
      struct Node {
         int item;
         Node* next;
      };
      Node* head;
}

LList::LList() {
   this->head = nullptr;  
}

void LList::cons(int x){
   Node* temp = new Node;
   temp->item = x;
   temp->next = head;
   head = temp;
}

void LList::append(int x, Node* head){
    if(isEmpty()) return cons(x);
    if(head->next == nullptr){
        Node* temp = new Node;
        temp->item = x;
        temp->next = nullptr;
        head->next = temp;
    }
    append(x, head->next);
}
void LList::append(int x) {
    append(x, head);
}

LList::~LList(){
   Node* curr;
   while (head != nullptr){
      curr = head->next;
      delete head;
      head = curr;
   }
}

А вот что я пробовал для конструктора копирования и назначения копирования.

void LList::copylist(Node* head1, Node* head2) {
    if (head1 == nullptr) {
        return;
    }
    head2->item = head1->item;
    copylist(head1->next, head2->next);
}
LList::LList(const LList& a) {
    LList newlist;
    copylist(a.head, newlist.head);
}

LList& LList::operator = (const LList& a) {
    LList newlist;
    copylist(a.head, newlist.head);
    return *this;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...