Please correct my errors. upvote
/**
* Clears our entire list, making it empty
* Remember: All removal operations should be memory-leak free.
*/
void clear()
{
// Set a pointer to the head of the list
Node<T> *current = head_;
// Loop through each node in the list
while (current != nullptr)
{
Node<T> *next = current->getNext();
delete current;
current = next;
}
// Set head and tail pointers to null and reset the size of the list
head_ = nullptr;
tail_ = nullptr;
size_ = 0;
}
/**
* Insert an element after the node pointed to by the pos Iterator
*
* If the list is currently empty,
* ignore the iterator and just make the new node at the head/tail (list of length 1).
*
* If the incoming iterator is this->end(), insert the element at the tail
*
* Should return an iterator that points to the newly added node
*
* To avoid repeated code, it might be a good idea to have other methods
* rely on this one.
*/
Iterator insert_after(Iterator pos, const T& value) {
}
/**
* Insert a new element after the index pos.
* Should work with an empty list.
*
* Should return an iterator pointing to the newly created node
*
* To reduce repeated code, you may want to simply find
* an iterator to the node at the pos index, then
* send it to the other overload of this method.
*/
Iterator insert_after(size_t pos, const T &value)
{
Iterator it = begin();
for (size_t i = 0; i < pos && it != end(); i++)
{
++it;
}
return insert_after(it, value);
}
/**
* Erase the node pointed to by the Iterators cursor.
*
* If the 'pos' iterator does not point to a valid node,
* throw an std::range_error
*
* Return an iterator to the node AFTER the one we erased,
* or this->end() if we just erased the tail
*/
Iterator erase(Iterator pos)
{
Node<T> *node = pos.node_;
if (node == nullptr || node == head_ || node == tail_)
{
throw std::range_error("Invalid iterator");
}
Node<T> *prevNode = node->prev_;
Node<T> *nextNode = node->next_;
// Updates the next_ pointer of the previous node to point to the next node or updates the head_
pointer
if (prevNode != nullptr)
{
prevNode->next_ = nextNode;
}
else
{
head_ = nextNode;
}
if (nextNode != nullptr)
{
nextNode->prev_ = prevNode;
}
else
{
tail_ = prevNode;
}
Iterator nextIt(nextNode, head_, tail_);
delete node;
return nextIt;
}
/**
* Add an element just after the one pointed to by the 'pos' iterator
*
* Should return an iterator pointing to the newly created node
*/
Iterator push_after(Iterator pos, const T &value)
{
Node<T> *node = pos.node_;
if (node == nullptr)
{
throw std::range_error("Invalid iterator");
}
Node<T> *nextNode = node->next_;
Node<T> *newNode = new Node(value, node, nextNode);
node->next_ = newNode;
if (nextNode != nullptr)
{
nextNode->prev_ = newNode;
}
else
{
tail_ = newNode;
}
return Iterator(newNode, head_, tail_);
}
/**
* Add a new element to the front of our list.
*/
void push_front(const T &value)
{
Node<T> *new_node = new Node<T>(value, nullptr, head_);
if (head_ == nullptr)
{
tail_ = new_node;
}
else
{
head_->setPrevious(new_node);
}
head_ = new_node;
++size_;
}
/**
* Add an element to the end of this list.
*
* Should return an iterator pointing to the newly created node.
*/
Iterator push_back(const T &value)
{
Node<T> *new_node = new Node<T>(value, nullptr, tail_);
if (tail_ == nullptr)
{
head_ = new_node;
}
else
{
tail_ = new_node;
}
tail_ = new_node;
++size_;
return Iterator(new_node, head_, tail_);
}

More Related Content

PDF
How do you stop infinite loop Because I believe that it is making a.pdf
PDF
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
PPTX
Linked list
PDF
This is problem is same problem which i submitted on 22017, I just.pdf
PDF
In C++ I need help with this method that Im trying to write fillLi.pdf
DOCX
Write java program using linked list to get integer from user and.docx
PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Linked list
This is problem is same problem which i submitted on 22017, I just.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
Write java program using linked list to get integer from user and.docx
This assignment and the next (#5) involve design and development of a.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdf

Similar to Please correct my errors upvote Clears our entire .pdf (20)

PDF
#includeiostream #includecstdio #includecstdlib using na.pdf
PDF
To complete the task, you need to fill in the missing code. I’ve inc.pdf
PDF
Data Structures in C++I am really new to C++, so links are really .pdf
PPTX
linkedlist-130914084342-phpapp02.pptx
PPTX
DSA(1).pptx
PPTX
C Homework Help
PPT
Mi 103 linked list
PDF
Program to insert in a sorted list #includestdio.h#include.pdf
PPTX
C Exam Help
PDF
1#include stdio.h#include stdlib.h#include assert.h .pdf
PDF
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PPT
Linkedlist
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
PDF
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
PDF
The algorithm to reverse a linked list by rearranging the required p.pdf
PDF
C++Write a method Node Nodereverse() which reverses a list..pdf
PDF
Consider a double-linked linked list implementation with the followin.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
Data Structures in C++I am really new to C++, so links are really .pdf
linkedlist-130914084342-phpapp02.pptx
DSA(1).pptx
C Homework Help
Mi 103 linked list
Program to insert in a sorted list #includestdio.h#include.pdf
C Exam Help
1#include stdio.h#include stdlib.h#include assert.h .pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Linkedlist
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
The algorithm to reverse a linked list by rearranging the required p.pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
Consider a double-linked linked list implementation with the followin.pdf

More from kitty811 (20)

PDF
please fix this code so that the tests pass css Selec.pdf
PDF
please help Below are the ages at which US presidents bega.pdf
PDF
please fill in the blanks below the proc structure .pdf
PDF
Please explain these steps Not sure how to go from first li.pdf
PDF
Please help Edgar Given the following API for a mutable Em.pdf
PDF
Please help 1 Use any data structures or control flows as a.pdf
PDF
Please graph the spectrum using only MATLAB and provide the .pdf
PDF
please help Let X and Y be random variables with joint dens.pdf
PDF
Please go through the Review Article and submit a summary of.pdf
PDF
Please give me a new topic not the one already on cheggQU.pdf
PDF
please give ANY inswer in 30 min The CONTROL pyloric rhythm .pdf
PDF
please full it please full it 2 Genie and Dan have just ha.pdf
PDF
Please find the Below YAML code whihc is meant to achieve th.pdf
PDF
Please fix my errors class Iterator public Construc.pdf
PDF
Please fill in the blanks of the 5 questions Required inform.pdf
PDF
please fast answer Suppose that XU nif 01 You draw a sa.pdf
PDF
Please explain in steps so I may be able to follow and learn.pdf
PDF
Please explain why c is correct Why does this return and e.pdf
PDF
Please code in C in Visual Studio Thank you 1 Create a C.pdf
PDF
Please explain step by step Current Attempt in Progress As .pdf
please fix this code so that the tests pass css Selec.pdf
please help Below are the ages at which US presidents bega.pdf
please fill in the blanks below the proc structure .pdf
Please explain these steps Not sure how to go from first li.pdf
Please help Edgar Given the following API for a mutable Em.pdf
Please help 1 Use any data structures or control flows as a.pdf
Please graph the spectrum using only MATLAB and provide the .pdf
please help Let X and Y be random variables with joint dens.pdf
Please go through the Review Article and submit a summary of.pdf
Please give me a new topic not the one already on cheggQU.pdf
please give ANY inswer in 30 min The CONTROL pyloric rhythm .pdf
please full it please full it 2 Genie and Dan have just ha.pdf
Please find the Below YAML code whihc is meant to achieve th.pdf
Please fix my errors class Iterator public Construc.pdf
Please fill in the blanks of the 5 questions Required inform.pdf
please fast answer Suppose that XU nif 01 You draw a sa.pdf
Please explain in steps so I may be able to follow and learn.pdf
Please explain why c is correct Why does this return and e.pdf
Please code in C in Visual Studio Thank you 1 Create a C.pdf
Please explain step by step Current Attempt in Progress As .pdf

Recently uploaded (20)

PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Complications of Minimal Access-Surgery.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
My India Quiz Book_20210205121199924.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
English Textual Question & Ans (12th Class).pdf
PDF
Climate and Adaptation MCQs class 7 from chatgpt
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
Complications of Minimal Access-Surgery.pdf
Computer Architecture Input Output Memory.pptx
Journal of Dental Science - UDMY (2022).pdf
My India Quiz Book_20210205121199924.pdf
What’s under the hood: Parsing standardized learning content for AI
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
Core Concepts of Personalized Learning and Virtual Learning Environments
Cambridge-Practice-Tests-for-IELTS-12.docx
AI-driven educational solutions for real-life interventions in the Philippine...
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
B.Sc. DS Unit 2 Software Engineering.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
Environmental Education MCQ BD2EE - Share Source.pdf
English Textual Question & Ans (12th Class).pdf
Climate and Adaptation MCQs class 7 from chatgpt

Please correct my errors upvote Clears our entire .pdf

  • 1. Please correct my errors. upvote /** * Clears our entire list, making it empty * Remember: All removal operations should be memory-leak free. */ void clear() { // Set a pointer to the head of the list Node<T> *current = head_; // Loop through each node in the list while (current != nullptr) { Node<T> *next = current->getNext(); delete current; current = next; } // Set head and tail pointers to null and reset the size of the list head_ = nullptr; tail_ = nullptr; size_ = 0; } /** * Insert an element after the node pointed to by the pos Iterator * * If the list is currently empty, * ignore the iterator and just make the new node at the head/tail (list of length 1). * * If the incoming iterator is this->end(), insert the element at the tail * * Should return an iterator that points to the newly added node * * To avoid repeated code, it might be a good idea to have other methods * rely on this one. */ Iterator insert_after(Iterator pos, const T& value) { } /** * Insert a new element after the index pos. * Should work with an empty list.
  • 2. * * Should return an iterator pointing to the newly created node * * To reduce repeated code, you may want to simply find * an iterator to the node at the pos index, then * send it to the other overload of this method. */ Iterator insert_after(size_t pos, const T &value) { Iterator it = begin(); for (size_t i = 0; i < pos && it != end(); i++) { ++it; } return insert_after(it, value); } /** * Erase the node pointed to by the Iterators cursor. * * If the 'pos' iterator does not point to a valid node, * throw an std::range_error * * Return an iterator to the node AFTER the one we erased, * or this->end() if we just erased the tail */ Iterator erase(Iterator pos) { Node<T> *node = pos.node_; if (node == nullptr || node == head_ || node == tail_) { throw std::range_error("Invalid iterator"); } Node<T> *prevNode = node->prev_; Node<T> *nextNode = node->next_; // Updates the next_ pointer of the previous node to point to the next node or updates the head_ pointer if (prevNode != nullptr) { prevNode->next_ = nextNode; } else {
  • 3. head_ = nextNode; } if (nextNode != nullptr) { nextNode->prev_ = prevNode; } else { tail_ = prevNode; } Iterator nextIt(nextNode, head_, tail_); delete node; return nextIt; } /** * Add an element just after the one pointed to by the 'pos' iterator * * Should return an iterator pointing to the newly created node */ Iterator push_after(Iterator pos, const T &value) { Node<T> *node = pos.node_; if (node == nullptr) { throw std::range_error("Invalid iterator"); } Node<T> *nextNode = node->next_; Node<T> *newNode = new Node(value, node, nextNode); node->next_ = newNode; if (nextNode != nullptr) { nextNode->prev_ = newNode; } else { tail_ = newNode; } return Iterator(newNode, head_, tail_); } /** * Add a new element to the front of our list. */
  • 4. void push_front(const T &value) { Node<T> *new_node = new Node<T>(value, nullptr, head_); if (head_ == nullptr) { tail_ = new_node; } else { head_->setPrevious(new_node); } head_ = new_node; ++size_; } /** * Add an element to the end of this list. * * Should return an iterator pointing to the newly created node. */ Iterator push_back(const T &value) { Node<T> *new_node = new Node<T>(value, nullptr, tail_); if (tail_ == nullptr) { head_ = new_node; } else { tail_ = new_node; } tail_ = new_node; ++size_; return Iterator(new_node, head_, tail_); }