SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
template<typename T>
class List
{
private:
struct Node;
struct Iterator;
struct ConstIterator;
public:
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = value_type const&;
using pointer = value_type*;
using const_pointer = value_type const*;
using iterator = Iterator;
using const_iterator = ConstIterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private:
struct Node
{
Node () : data ()
{
}
Node (T const& v) : data (v)
{
}
Node (T const& v, Node* n, Node* p) : data (v), next (n), prev (p)
{
}
Node (T&& v) : data (std::move (v))
{
}
Node (T&& v, Node* n, Node* p) : data (std::move (v)), next (n), prev (p)
{
}
// this is start of range to remove
// end is inclusive end of range to remove
static void
unhook (Node* begin, Node* end)
{
begin -> prev -> next = end -> next;
end -> next -> prev = begin -> prev;
}
// insert [first,last] before this
void
hook (Node* first, Node* last)
{
first -> prev = prev;
last -> next = this;
prev -> next = first;
prev = last;
}
// insert first before this
void
hook (Node* first)
{
hook (first, first);
}
void
unhook ()
{
Node::unhook (this, this);
}
T data;
Node* next{nullptr};
Node* prev{nullptr};
};
struct Iterator
{
using value_type = List::value_type;
using pointer = List::pointer;
using reference = List::reference;
using difference_type = List::difference_type;
using iterator_category = std::bidirectional_iterator_tag;
public:
Iterator () noexcept = default;
Iterator (Iterator const&) noexcept = default;
Iterator (Iterator&&) noexcept = default;
~Iterator () = default;
Iterator&
operator= (Iterator const&) noexcept = default;
Iterator&
operator= (Iterator&&) noexcept = default;
Iterator (Node const* n) : m_nodePtr (const_cast<Node*> (n))
{
}
reference operator* () const
{
return m_nodePtr->data;
}
pointer operator-> () const
{
return &(m_nodePtr->data);
}
// advances to the "next" pointer, returns reference to self
Iterator&
operator++ ()
{
m_nodePtr = m_nodePtr -> next;
return *this;
}
// advances to the "next" pointer, returns copy of self prior to advancement
Iterator
operator++ (int)
{
iterator temp(*this);
++(*this);
return temp;
}
// advances to the "prev" pointer, returns reference to self
Iterator&
operator-- ()
{
m_nodePtr = m_nodePtr -> prev;
return *this;
}
// advances to the "prev" pointer, returns copy of self prior to advancement
Iterator
operator-- (int)
{
iterator temp(*this);
--(*this);
return temp;
}
// compares the underlying pointers for equality
friend bool
operator== (Iterator const& i, Iterator const& j)
{
return i.m_nodePtr == j.m_nodePtr;
}
friend bool
operator!= (Iterator const& i, Iterator const& j)
{
return !(i == j);
}
private:
Node* m_nodePtr{nullptr};
friend class List;
};
struct ConstIterator
{
using value_type = List::value_type;
using pointer = List::const_pointer;
using reference = List::const_reference;
using difference_type = List::difference_type;
using iterator_category = std::bidirectional_iterator_tag;
public:
ConstIterator () noexcept = default;
ConstIterator (ConstIterator const&) noexcept = default;
ConstIterator (ConstIterator&&) noexcept = default;
~ConstIterator () = default;
ConstIterator&
operator= (ConstIterator const&) noexcept = default;
ConstIterator&
operator= (ConstIterator&&) noexcept = default;
ConstIterator (Node const* n) : m_nodePtr (const_cast<Node*> (n))
{
}
ConstIterator (Iterator const& i) : m_nodePtr (i.m_nodePtr)
{
}
reference operator* () const
{
return m_nodePtr->data;
}
pointer operator-> () const
{
return &(m_nodePtr->data);
}
ConstIterator&
operator++ ()
{
m_nodePtr = m_nodePtr -> next;
return *this;
}
ConstIterator
operator++ (int)
{
iterator temp(*this);
++(*this);
return temp;
}
ConstIterator&
operator-- ()
{
m_nodePtr = m_nodePtr -> prev;
return *this;
}
ConstIterator
operator-- (int)
{
iterator temp(*this);
--(*this);
return temp;
}
friend bool
operator== (ConstIterator const& i, ConstIterator const& j)
{
return i.m_nodePtr == j.m_nodePtr;
}
friend bool
operator!= (ConstIterator const& i, ConstIterator const& j)
{
return !(i == j);
}
private:
Node* m_nodePtr{nullptr};
friend class List;
};
// transfers [first, last) to before pos and sets all links
static void
transfer (const_iterator pos, const_iterator first, const_iterator last)
{
if (first == last)
{
return;
}
first.m_nodePtr->prev->next = last.m_nodePtr->next;
last.m_nodePtr->next->prev = first.m_nodePtr->prev;
pos.m_nodePtr->hook(first.m_nodePtr, last.m_nodePtr);
}
public:
// default constructor
List ()
/* remember to include the member initializer list */
{
// TODO
// make m_header a circular node
}
// size-value constructor
explicit List (size_type count, T const& value) : List ()
{
// TODO
}
explicit List (size_type count) : List ()
{
while (count--)
{
emplace_back ();
}
}
// range constructor
template<typename InputIt, Requires (concepts::ForwardIterator, InputIt)>
List (InputIt first, InputIt last) : List ()
{
// TODO
}
// copy constructor
List (List const& other) : List (other.begin (), other.end ())
{
}
// move constructor
List (List&& other)
: m_header (std::exchange (other.m_header, Node ()))
, m_size (std::exchange (other.m_size, 0))
{
}
// intializer_list constructor
List (std::initializer_list<T> init) : List (init.begin (), init.end ())
{
}
// destructor
~List ()
{
// TODO
// Remember to delete all allocated nodes!
}
// copy assignment
List&
operator= (List const& other)
{
// TODO
// Remember to check for self-assignment
// Hint: look at versions of assign() below...
}
// move assignment
List&
operator= (List&& other) noexcept
{
if (&other != this)
{
clear ();
m_header.next = std::exchange (other.m_header.next, &(other.m_header));
m_header.prev = std::exchange (other.m_header.prev, &(other.m_header));
m_size = std::exchange (other.m_size, 0);
}
return *this;
}
// initializer_list assignment
List&
operator= (std::initializer_list<T> ilist)
{
assign (ilist);
return *this;
}
void
assign (size_type count, T const& value)
{
List l (count, value);
swap (l);
}
template<typename InputIt, Requires (concepts::ForwardIterator, InputIt)>
void
assign (InputIt first, InputIt last)
{
List l (first, last);
swap (l);
}
void
assign (std::initializer_list<T> ilist)
{
// TODO
}
...
private:
Node m_header;
size_type m_size;
};
...
Please complete the implementation marked TODO for the above member functions.

Weitere ähnliche Inhalte

Ähnlich wie include ltfunctionalgt include ltiteratorgt inclu.pdf

Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdfNeed help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
actexerode
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
LeonardN9WWelchw
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
mail931892
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
fmac5
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
feroz544
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
rohassanie
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
anwarsadath111
 
#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf
annucommunication1
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
fantoosh1
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 
Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdf
kitty811
 
I want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfI want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdf
bermanbeancolungak45
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
mckellarhastings
 

Ähnlich wie include ltfunctionalgt include ltiteratorgt inclu.pdf (20)

Pointers
PointersPointers
Pointers
 
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdfNeed help with the TODO's (DONE IN C++) #pragma once   #include -funct.pdf
Need help with the TODO's (DONE IN C++) #pragma once #include -funct.pdf
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
 
#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf#include iostream #includeData.h #includePerson.h#in.pdf
#include iostream #includeData.h #includePerson.h#in.pdf
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdf
 
#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx#include iostream#include d_node.h #include d_nodel.h.docx
#include iostream#include d_node.h #include d_nodel.h.docx
 
I want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfI want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdf
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
 

Mehr von naslin841216

Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas  Maria ailelere destek salay.pdfYnetim Liderlik Vaka almas  Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
naslin841216
 
Study B Roots Roots and stems often appear similar except .pdf
Study B Roots Roots and stems often appear similar except .pdfStudy B Roots Roots and stems often appear similar except .pdf
Study B Roots Roots and stems often appear similar except .pdf
naslin841216
 
Please answer all or do not answer at all 1 Nearly all ign.pdf
Please answer all or do not answer at all 1 Nearly all ign.pdfPlease answer all or do not answer at all 1 Nearly all ign.pdf
Please answer all or do not answer at all 1 Nearly all ign.pdf
naslin841216
 
need on c++ Task 1 Design a class for Singly linked List wi.pdf
need on c++ Task 1 Design a class for Singly linked List wi.pdfneed on c++ Task 1 Design a class for Singly linked List wi.pdf
need on c++ Task 1 Design a class for Singly linked List wi.pdf
naslin841216
 
As of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdf
As of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdfAs of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdf
As of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdf
naslin841216
 
ACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdf
ACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdfACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdf
ACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdf
naslin841216
 
El caso Una maana de lunes a viernes en 2007 Bethanye Blo.pdf
El caso  Una maana de lunes a viernes en 2007 Bethanye Blo.pdfEl caso  Una maana de lunes a viernes en 2007 Bethanye Blo.pdf
El caso Una maana de lunes a viernes en 2007 Bethanye Blo.pdf
naslin841216
 

Mehr von naslin841216 (20)

Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas  Maria ailelere destek salay.pdfYnetim Liderlik Vaka almas  Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
 
Which of the following is not component of Emotional Intelli.pdf
Which of the following is not component of Emotional Intelli.pdfWhich of the following is not component of Emotional Intelli.pdf
Which of the following is not component of Emotional Intelli.pdf
 
Study B Roots Roots and stems often appear similar except .pdf
Study B Roots Roots and stems often appear similar except .pdfStudy B Roots Roots and stems often appear similar except .pdf
Study B Roots Roots and stems often appear similar except .pdf
 
Resumen del caso 253 Contratos de compraventa internaciona.pdf
Resumen del caso 253 Contratos de compraventa internaciona.pdfResumen del caso 253 Contratos de compraventa internaciona.pdf
Resumen del caso 253 Contratos de compraventa internaciona.pdf
 
Question Content Area Use the information provided for Prive.pdf
Question Content Area Use the information provided for Prive.pdfQuestion Content Area Use the information provided for Prive.pdf
Question Content Area Use the information provided for Prive.pdf
 
PREGUNTA 16 Los siguientes son genotipos de merocigotos de.pdf
PREGUNTA 16  Los siguientes son genotipos de merocigotos de.pdfPREGUNTA 16  Los siguientes son genotipos de merocigotos de.pdf
PREGUNTA 16 Los siguientes son genotipos de merocigotos de.pdf
 
Please answer all or do not answer at all 1 Nearly all ign.pdf
Please answer all or do not answer at all 1 Nearly all ign.pdfPlease answer all or do not answer at all 1 Nearly all ign.pdf
Please answer all or do not answer at all 1 Nearly all ign.pdf
 
please summarize i will hit like The year 2021 in Sweden con.pdf
please summarize i will hit like The year 2021 in Sweden con.pdfplease summarize i will hit like The year 2021 in Sweden con.pdf
please summarize i will hit like The year 2021 in Sweden con.pdf
 
On average indoor cats live to 12 years old with a standard.pdf
On average indoor cats live to 12 years old with a standard.pdfOn average indoor cats live to 12 years old with a standard.pdf
On average indoor cats live to 12 years old with a standard.pdf
 
Grace makes sure that she walks by her bosss office several.pdf
Grace makes sure that she walks by her bosss office several.pdfGrace makes sure that she walks by her bosss office several.pdf
Grace makes sure that she walks by her bosss office several.pdf
 
Conversion of G3P to RuBP energy and it is coupled to releas.pdf
Conversion of G3P to RuBP energy and it is coupled to releas.pdfConversion of G3P to RuBP energy and it is coupled to releas.pdf
Conversion of G3P to RuBP energy and it is coupled to releas.pdf
 
need on c++ Task 1 Design a class for Singly linked List wi.pdf
need on c++ Task 1 Design a class for Singly linked List wi.pdfneed on c++ Task 1 Design a class for Singly linked List wi.pdf
need on c++ Task 1 Design a class for Singly linked List wi.pdf
 
In the test of hypotheses about three or more population mea.pdf
In the test of hypotheses about three or more population mea.pdfIn the test of hypotheses about three or more population mea.pdf
In the test of hypotheses about three or more population mea.pdf
 
During the current year Brewer Company acquired all of the .pdf
During the current year Brewer Company acquired all of the .pdfDuring the current year Brewer Company acquired all of the .pdf
During the current year Brewer Company acquired all of the .pdf
 
ii If the second segment is lost what is the acknowledgeme.pdf
ii If the second segment is lost what is the acknowledgeme.pdfii If the second segment is lost what is the acknowledgeme.pdf
ii If the second segment is lost what is the acknowledgeme.pdf
 
Given the matrices B132122Fpath dM403 1759 V84 H666 .pdf
Given the matrices B132122Fpath dM403 1759 V84 H666 .pdfGiven the matrices B132122Fpath dM403 1759 V84 H666 .pdf
Given the matrices B132122Fpath dM403 1759 V84 H666 .pdf
 
Destruction results in loss of communication between the R a.pdf
Destruction results in loss of communication between the R a.pdfDestruction results in loss of communication between the R a.pdf
Destruction results in loss of communication between the R a.pdf
 
As of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdf
As of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdfAs of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdf
As of 2010 Xerox Corporation NYSE XRX is a 22 billion .pdf
 
ACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdf
ACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdfACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdf
ACCT 215 CT Accounting Cycle Problem The John Marshall Com.pdf
 
El caso Una maana de lunes a viernes en 2007 Bethanye Blo.pdf
El caso  Una maana de lunes a viernes en 2007 Bethanye Blo.pdfEl caso  Una maana de lunes a viernes en 2007 Bethanye Blo.pdf
El caso Una maana de lunes a viernes en 2007 Bethanye Blo.pdf
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 

Kürzlich hochgeladen (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

include ltfunctionalgt include ltiteratorgt inclu.pdf

  • 1. #include <functional> #include <iterator> #include <limits> #include <memory> template<typename T> class List { private: struct Node; struct Iterator; struct ConstIterator; public: using value_type = T; using size_type = std::size_t; using difference_type = std::ptrdiff_t; using reference = value_type&; using const_reference = value_type const&; using pointer = value_type*; using const_pointer = value_type const*; using iterator = Iterator; using const_iterator = ConstIterator; using reverse_iterator = std::reverse_iterator<iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>; private: struct Node { Node () : data () { } Node (T const& v) : data (v) { } Node (T const& v, Node* n, Node* p) : data (v), next (n), prev (p) { } Node (T&& v) : data (std::move (v)) { } Node (T&& v, Node* n, Node* p) : data (std::move (v)), next (n), prev (p) { } // this is start of range to remove
  • 2. // end is inclusive end of range to remove static void unhook (Node* begin, Node* end) { begin -> prev -> next = end -> next; end -> next -> prev = begin -> prev; } // insert [first,last] before this void hook (Node* first, Node* last) { first -> prev = prev; last -> next = this; prev -> next = first; prev = last; } // insert first before this void hook (Node* first) { hook (first, first); } void unhook () { Node::unhook (this, this); } T data; Node* next{nullptr}; Node* prev{nullptr}; }; struct Iterator { using value_type = List::value_type; using pointer = List::pointer; using reference = List::reference; using difference_type = List::difference_type; using iterator_category = std::bidirectional_iterator_tag; public: Iterator () noexcept = default; Iterator (Iterator const&) noexcept = default; Iterator (Iterator&&) noexcept = default;
  • 3. ~Iterator () = default; Iterator& operator= (Iterator const&) noexcept = default; Iterator& operator= (Iterator&&) noexcept = default; Iterator (Node const* n) : m_nodePtr (const_cast<Node*> (n)) { } reference operator* () const { return m_nodePtr->data; } pointer operator-> () const { return &(m_nodePtr->data); } // advances to the "next" pointer, returns reference to self Iterator& operator++ () { m_nodePtr = m_nodePtr -> next; return *this; } // advances to the "next" pointer, returns copy of self prior to advancement Iterator operator++ (int) { iterator temp(*this); ++(*this); return temp; } // advances to the "prev" pointer, returns reference to self Iterator& operator-- () { m_nodePtr = m_nodePtr -> prev; return *this; } // advances to the "prev" pointer, returns copy of self prior to advancement Iterator operator-- (int) {
  • 4. iterator temp(*this); --(*this); return temp; } // compares the underlying pointers for equality friend bool operator== (Iterator const& i, Iterator const& j) { return i.m_nodePtr == j.m_nodePtr; } friend bool operator!= (Iterator const& i, Iterator const& j) { return !(i == j); } private: Node* m_nodePtr{nullptr}; friend class List; }; struct ConstIterator { using value_type = List::value_type; using pointer = List::const_pointer; using reference = List::const_reference; using difference_type = List::difference_type; using iterator_category = std::bidirectional_iterator_tag; public: ConstIterator () noexcept = default; ConstIterator (ConstIterator const&) noexcept = default; ConstIterator (ConstIterator&&) noexcept = default; ~ConstIterator () = default; ConstIterator& operator= (ConstIterator const&) noexcept = default; ConstIterator& operator= (ConstIterator&&) noexcept = default; ConstIterator (Node const* n) : m_nodePtr (const_cast<Node*> (n)) { } ConstIterator (Iterator const& i) : m_nodePtr (i.m_nodePtr) { } reference operator* () const
  • 5. { return m_nodePtr->data; } pointer operator-> () const { return &(m_nodePtr->data); } ConstIterator& operator++ () { m_nodePtr = m_nodePtr -> next; return *this; } ConstIterator operator++ (int) { iterator temp(*this); ++(*this); return temp; } ConstIterator& operator-- () { m_nodePtr = m_nodePtr -> prev; return *this; } ConstIterator operator-- (int) { iterator temp(*this); --(*this); return temp; } friend bool operator== (ConstIterator const& i, ConstIterator const& j) { return i.m_nodePtr == j.m_nodePtr; } friend bool operator!= (ConstIterator const& i, ConstIterator const& j) { return !(i == j);
  • 6. } private: Node* m_nodePtr{nullptr}; friend class List; }; // transfers [first, last) to before pos and sets all links static void transfer (const_iterator pos, const_iterator first, const_iterator last) { if (first == last) { return; } first.m_nodePtr->prev->next = last.m_nodePtr->next; last.m_nodePtr->next->prev = first.m_nodePtr->prev; pos.m_nodePtr->hook(first.m_nodePtr, last.m_nodePtr); } public: // default constructor List () /* remember to include the member initializer list */ { // TODO // make m_header a circular node } // size-value constructor explicit List (size_type count, T const& value) : List () { // TODO } explicit List (size_type count) : List () { while (count--) { emplace_back (); } } // range constructor template<typename InputIt, Requires (concepts::ForwardIterator, InputIt)> List (InputIt first, InputIt last) : List () { // TODO
  • 7. } // copy constructor List (List const& other) : List (other.begin (), other.end ()) { } // move constructor List (List&& other) : m_header (std::exchange (other.m_header, Node ())) , m_size (std::exchange (other.m_size, 0)) { } // intializer_list constructor List (std::initializer_list<T> init) : List (init.begin (), init.end ()) { } // destructor ~List () { // TODO // Remember to delete all allocated nodes! } // copy assignment List& operator= (List const& other) { // TODO // Remember to check for self-assignment // Hint: look at versions of assign() below... } // move assignment List& operator= (List&& other) noexcept { if (&other != this) { clear (); m_header.next = std::exchange (other.m_header.next, &(other.m_header)); m_header.prev = std::exchange (other.m_header.prev, &(other.m_header)); m_size = std::exchange (other.m_size, 0); } return *this;
  • 8. } // initializer_list assignment List& operator= (std::initializer_list<T> ilist) { assign (ilist); return *this; } void assign (size_type count, T const& value) { List l (count, value); swap (l); } template<typename InputIt, Requires (concepts::ForwardIterator, InputIt)> void assign (InputIt first, InputIt last) { List l (first, last); swap (l); } void assign (std::initializer_list<T> ilist) { // TODO } ... private: Node m_header; size_type m_size; }; ... Please complete the implementation marked TODO for the above member functions.