SlideShare a Scribd company logo
#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.

More Related Content

PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
PDF
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
PDF
Consider a double-linked linked list implementation with the followin.pdf
PDF
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
PDF
for initializer_list include ltinitializer_listgt .pdf
PDF
I have to write a polynomial class linked list program and i do not .pdf
PDF
C++ extension methods
PPTX
Алексей Кутумов, Вектор с нуля
include ltfunctionalgt include ltiteratorgt inclu.pdf
Please code in C++ and do only the �TO DO�s and all of them. There a.pdf
Consider a double-linked linked list implementation with the followin.pdf
Please complete ALL of the �TO DO�s in this code. I am really strugg.pdf
for initializer_list include ltinitializer_listgt .pdf
I have to write a polynomial class linked list program and i do not .pdf
C++ extension methods
Алексей Кутумов, Вектор с нуля

Similar to include ltfunctionalgt include ltiteratorgt inclu.pdf (20)

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

More from naslin841216 (20)

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

Recently uploaded (20)

DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Computing-Curriculum for Schools in Ghana
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
Indian roads congress 037 - 2012 Flexible pavement
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Trump Administration's workforce development strategy
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Paper A Mock Exam 9_ Attempt review.pdf.
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Computing-Curriculum for Schools in Ghana
A systematic review of self-coping strategies used by university students to ...
LDMMIA Reiki Yoga Finals Review Spring Summer
Indian roads congress 037 - 2012 Flexible pavement
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Final Presentation General Medicine 03-08-2024.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
What if we spent less time fighting change, and more time building what’s rig...
Trump Administration's workforce development strategy
A powerpoint presentation on the Revised K-10 Science Shaping Paper
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
ChatGPT for Dummies - Pam Baker Ccesa007.pdf

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.