简体   繁体   中英

C++ Singly Linked List - Deque - InsertFront()

I'm trying to implement an insertfront() function for my singly list deque but I keep running into this one same problem.

I believe its due to the overloading of the operator=

Deque& operator=(const Deque&){return *this;};

For the assignment to work it requires operand A to be a reference of type Deque and operand B to be another reference of type Deque but I'm unsure on how to go about implementing this as I tried to do head& = new_node& and got hit with more errors.

Deque.cpp||In member function 'void Deque::insert_front(int)':|
Deque.cpp|21|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Node; _Dp = std::default_delete<Node>]'|
/usr/include/c++/6/bits/unique_ptr.h|357|note: declared here|

Here is my function

#include "Deque.h"
#include <iostream>
#include <memory>
#include <utility>
using std::cout;
using std::endl;
using std::move;

void Deque::insert_front(int a)
{
    std::unique_ptr<Node> new_node;

    new_node->val = move(a);

    new_node->next = move(head);
    head = move (new_node);
}

Deque.h

#include "Node.h"
#include <memory>
class Deque{
    public:
        Deque() = default;
        Deque(const Deque&);
        ~Deque(); //must use constant space
        Deque& operator=(const Deque&){return *this;};
        void insert_front(int); 

    private:

    friend Node;

        //Sentinel
        std::unique_ptr<Node> head ;
        std::unique_ptr<Node> tail ;
};

Node.h

#ifndef NODE_H
#define NODE_H

#include <iostream>
#include <memory>

class Node {
 public:
 Node(const Node& n) : val{n.val}, next{}
  {
  }
 Node(int v, std::unique_ptr<Node> n) : val{v}, next{move(n)}
  {
  }
 Node(int v) : val{v}
  {
  }

 private:
  int val = 0;
  std::unique_ptr<Node> next = nullptr;

  friend class Deque;
  friend std::ostream& operator<<(std::ostream&, const Node&);
};

#endif

In the function insertAtFront basically it is easy that after creating node we have to check that if the list is empty or not so we can check through head node if it is null then node is empty else not. If head is not null then we assign the address of first node in the newly nodes next pointer. And then we have to update head so we assign the newly created nodes address to head.

#include<iostream>
using namespace std;

//Node structure
struct node{
    int data;
    node* next;
};

//This pointer will always point first node
node* head = NULL;

//Function to add node at the front of the linked list
void insert_at_front(){
    node* t = new node;
    cout << "Enter the data in the node :";
    cin >> t->data;
    t->next = NULL;

    if(head == NULL){
        head = t;
    }
    else{
        //After adding the new node to the existing linked list 
        //then we have to modify the head pointer  
        t->next = head;
        head = t;

    }
    numberOfNodes++;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM