Answered step by step
Verified Expert Solution
Question
1 Approved Answer
how do I make this work c + + , the assignment is to make raw pointers into smart pointers - the error is c
how do I make this work c the assignment is to make raw pointers into smart pointers the error is c on line iterator& operator function
#pragma once
#include
#include
#include
using namespace std;
template
class List
private:
class Node
public:
T data;
uniqueptr prev;
uniqueptr next;
bool isHiddenNode false;
;
uniqueptr head;
uniqueptr tail;
public:
class constiterator
protected:
uniqueptr current;
T& retrieve const return currentdata;
constiteratoruniqueptr p : currentp
friend class List;
public:
explicit constiterator : currentnullptr
T& operator const
return retrieve;
constiterator& operator
current currentnext;
return this;
constiterator operatorint
constiterator old this;
this;
return old;
bool operatorconst constiterator& rhs const
return current rhscurrent;
bool operator!const constiterator& rhs const
return this rhs;
;
public:
class iterator : public constiterator
protected:
iteratoruniqueptr p : constiteratorp
friend class List;
public:
explicit iterator
T& operator
return constiterator::retrieve;
const T& operator const
return constiterator::operator;
iterator& operator
thiscurrent constiterator::currentnext;
return this;
iterator operatorint
iterator old this;
this;
return old;
;
private:
void setupList
uniqueptr newNode new Node;
newNodenext tail;
newNodeprev head;
headnext newNode;
tailprev newNode;
void deleteListContents
uniqueptr current headnext;
uniqueptr temp nullptr;
while current tailprev
temp currentnext;
delete current;
current temp;
public:
List
head new Node;
headisHiddenNode true;
tail new Node;
tailisHiddenNode true;
headprev nullptr;
headnext tail;
tailprev head;
tailnext nullptr;
;
ListT newData
setupList;
headnextdata newData;
explicit ListList& rhs
copy constructor
deleteListContents;
head rhshead;
tail rhstail;
virtual ~List
And a destructor
deleteListContents;
deleteListContents leaves the head pointer, so explicit
delete req'd
delete head;
bool empty
return headnext tail;
iterator related methods
iterator begin return headnext ;
iterator end return tail;
constiterator cbegin const
return headnext ;
constiterator cend const
return tail ;
iterator eraseiterator itr
uniqueptr p itr.current;
iterator iterToReturn pnext ;
pprevnext pnext;
pnextprev pprev;
return iterToReturn;
iterator insertiterator itr, const T& x
uniqueptr p itr.current;
uniqueptr newNode new Node x pprev, p ;
pprev pprevnext newNode;
iterator eraseiterator from, iterator to
iterator itr from;
while itr to
itr eraseitr;
return to;
And the methods for the rest
void pushfrontT data
if thisempty
setupList;
uniqueptr actualHead headnext;
actualHeaddata data;
else
uniqueptr actualHead headnext;
uniqueptr newNode new Node;
newNodedata data;
newNodenext actualHead;
actualHeadprev newNode;
newNodeprev head;
headnext newNode;
void pushbackT data
if thisempty
setupList;
uniqueptr actualTail tailprev;
actualTaildata data;
else
uniqueptr actualTail tailprev;
uniqueptr newNode new Node;
newNodedata data;
newNodeprev actualTail;
actualTailnext newNode;
newNodenext tail;
tailprev newNode;
T front
uniqueptr actualHead headnext;
return actualHeaddata;
T back
uniqueptractualTail tailprev;
return actualTaildata;
void popback
if empty
uniqueptr lastNode tailprev;
tailprev lastNodeprev;
uniqueptrnewLastNode tailprev;
newLastNodenext tail;
delete lastNode;
lastNode nullptr;
else
std::cerr "popback: Attempt to pop from empty list. std::endl;
void popfront
if empty
uniqueptr firstNode headnext;
headnext firstNodenext;
uniqueptr newFirstNode headnext;
newFirstNodeprev head;
delete firstNode;
firstNode nullptr;
else
std::cerr "popback: Attempt to pop from empty list. std::endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started