SlideShare a Scribd company logo
1 of 13
Download to read offline
#ifndef LINKED_LIST_
#define LINKED_LIST_
template
class LinkedList {
private:
std::shared_ptr> headPtr; // Pointer to first node in the chain;
// (contains the first entry in the list)
int itemCount; // Current count of list items
std::shared_ptr> getNodeAt(int position) const;
public:
LinkedList();
LinkedList(const LinkedList& aList);
virtual ~LinkedList();
bool isEmpty() const;
int getLength() const;
bool insert(int newPosition, const ItemType& newEntry);
bool remove(int position);
void clear();
/** throw PrecondViolatedExcept if position < 1 or
position > getLength(). */
ItemType getEntry(int position) const throw(PrecondViolatedExcept);
/** throw PrecondViolatedExcept if position < 1 or
position > getLength(). */
void replace(int position, const ItemType& newEntry)
throw(PrecondViolatedExcept);
// Operator Overloading
void operator = (const LinkedList& arg);
friend std::ostream& operator << (std::ostream& os, const LinkedList& arg)
{
os << "There are " << arg.getLength() << " values in the list:" << std::endl;
for (int i{ 1 }; i <= arg.getLength(); i++) {
os << arg.getEntry(i) << std::endl;
}
os << std::endl << std::endl << std::endl << std::endl;
return os;
}
};
// Returns a pointer to the Node at the given position.
template
std::shared_ptr> LinkedList::getNodeAt (int position) const {
std::shared_ptr> currPtr{ headPtr };
for (int i{ 1 }; i < position; i++)
currPtr = currPtr->getNext();
return currPtr;
}
// Default constructor.
template
LinkedList::LinkedList() {
headPtr = nullptr;
itemCount = 0;
}
// Copy constructor.
template
LinkedList::LinkedList(const LinkedList& aList) {
itemCount = aList.getLength();
auto currPtr{ aList.headPtr };
auto newNodePtr{ headPtr = std::make_shared>(currPtr->getItem()) };
auto prevNodePtr{ newNodePtr };
currPtr = currPtr->getNext();
for (int i{ 2 }; i <= itemCount; i++) {
newNodePtr = std::make_shared>(currPtr->getItem());
prevNodePtr->setNext(newNodePtr);
prevNodePtr = newNodePtr;
currPtr = currPtr->getNext();
}
}
// Destructor.
template
LinkedList::~LinkedList() {
headPtr = nullptr;
itemCount = 0;
}
// Accessor/Info Functions.
template
bool LinkedList::isEmpty() const {
return (itemCount > 0)?0:1;
}
template
int LinkedList::getLength() const {
return itemCount;
}
// Places a Node at a given position (element at the same position is now pos+1).
template
bool LinkedList::insert(const int newPosition,
const ItemType& newEntry)
{
bool ableToInsert{ (newPosition >= 1) &&
(newPosition <= itemCount + 1) };
if (ableToInsert)
{
// Create a new node containing the new entry
auto newNodePtr{ std::make_shared>(newEntry) };
// Attach new node to chain
if (newPosition == 1)
{
// Insert new node at beginning of chain
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
}
else
{
// Find node that will be before new node
auto prevPtr{ getNodeAt(newPosition - 1) };
// Insert new node after node to which prevPtr points
newNodePtr->setNext(prevPtr->getNext());
prevPtr->setNext(newNodePtr);
} // end if
itemCount++; // Increase count of entries
} // end if
return ableToInsert;
} // end insert
// Removes the Node at the given position.
template
bool LinkedList::remove(const int position)
{
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
if (position == 1)
{
// Remove the first node in the chain
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
auto prevPtr{ getNodeAt(position - 1) };
// Point to node to delete
auto curPtr{ prevPtr->getNext() };
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(curPtr->getNext());
} // end if
itemCount--; // Decrease count of entries
} // end if
return ableToRemove;
} // end remove
// Replaces the Entry in the given Node with the new Entry.
template
void LinkedList::replace(const int position, const ItemType& newEntry)
throw(PrecondViolatedExcept)
{
if (position > this->getLength() || position < 1)
throw PrecondViolatedExcept("replace() called with a position out of bounds.");
else
getNodeAt(position)->setItem(newEntry);
}
template
void LinkedList::clear()
{
headPtr = nullptr;
itemCount = 0;
} // end clear
template
ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcept)
{
if (position > this->getLength() || position < 1)
throw PrecondViolatedExcept("getEntry() called with a position out of bounds.");
else
return getNodeAt(position)->getItem();
}
// Makes the calling Linked List's entries the same as the given Linked List.
template
void LinkedList::operator = (const LinkedList& arg) {
if (arg.isEmpty())
return;
// First section copies the given list into the calling list's existing nodes.
bool isThisLarger{ this->itemCount >= arg.itemCount };
auto currThisPtr{ this->headPtr };
auto currArgPtr{ arg.headPtr };
if (!this->isEmpty())
{
currThisPtr->setItem(currArgPtr->getItem());
for (int i = 2; i <= ((isThisLarger) ? arg.itemCount : this->itemCount); i++) {
currThisPtr = currThisPtr->getNext();
currArgPtr = currArgPtr->getNext();
currThisPtr->setItem(currArgPtr->getItem());
}
// If the calling list is larger then tidy up the end.
if (isThisLarger) {
this->itemCount = arg.itemCount;
currThisPtr->setNext(nullptr);
}
}
// Create new nodes and/or finish copying the entries.
if (!isThisLarger) {
currArgPtr = currArgPtr->getNext();
auto newNodePtr{ std::make_shared>(currArgPtr->getItem()) };
auto prevNodePtr{ currThisPtr };
prevNodePtr->setNext(newNodePtr);
if (this->isEmpty())
this->headPtr = newNodePtr;
for (int i = this->itemCount+1; i <= arg.itemCount; i++) {
newNodePtr = std::make_shared>(currArgPtr->getItem());
prevNodePtr->setNext(newNodePtr);
prevNodePtr = newNodePtr;
currArgPtr = currArgPtr->getNext();
}
this->itemCount = arg.itemCount;
}
}
#endif
Solution
#ifndef LINKED_LIST_
#define LINKED_LIST_
template
class LinkedList {
private:
std::shared_ptr> headPtr; // Pointer to first node in the chain;
// (contains the first entry in the list)
int itemCount; // Current count of list items
std::shared_ptr> getNodeAt(int position) const;
public:
LinkedList();
LinkedList(const LinkedList& aList);
virtual ~LinkedList();
bool isEmpty() const;
int getLength() const;
bool insert(int newPosition, const ItemType& newEntry);
bool remove(int position);
void clear();
/** throw PrecondViolatedExcept if position < 1 or
position > getLength(). */
ItemType getEntry(int position) const throw(PrecondViolatedExcept);
/** throw PrecondViolatedExcept if position < 1 or
position > getLength(). */
void replace(int position, const ItemType& newEntry)
throw(PrecondViolatedExcept);
// Operator Overloading
void operator = (const LinkedList& arg);
friend std::ostream& operator << (std::ostream& os, const LinkedList& arg)
{
os << "There are " << arg.getLength() << " values in the list:" << std::endl;
for (int i{ 1 }; i <= arg.getLength(); i++) {
os << arg.getEntry(i) << std::endl;
}
os << std::endl << std::endl << std::endl << std::endl;
return os;
}
};
// Returns a pointer to the Node at the given position.
template
std::shared_ptr> LinkedList::getNodeAt (int position) const {
std::shared_ptr> currPtr{ headPtr };
for (int i{ 1 }; i < position; i++)
currPtr = currPtr->getNext();
return currPtr;
}
// Default constructor.
template
LinkedList::LinkedList() {
headPtr = nullptr;
itemCount = 0;
}
// Copy constructor.
template
LinkedList::LinkedList(const LinkedList& aList) {
itemCount = aList.getLength();
auto currPtr{ aList.headPtr };
auto newNodePtr{ headPtr = std::make_shared>(currPtr->getItem()) };
auto prevNodePtr{ newNodePtr };
currPtr = currPtr->getNext();
for (int i{ 2 }; i <= itemCount; i++) {
newNodePtr = std::make_shared>(currPtr->getItem());
prevNodePtr->setNext(newNodePtr);
prevNodePtr = newNodePtr;
currPtr = currPtr->getNext();
}
}
// Destructor.
template
LinkedList::~LinkedList() {
headPtr = nullptr;
itemCount = 0;
}
// Accessor/Info Functions.
template
bool LinkedList::isEmpty() const {
return (itemCount > 0)?0:1;
}
template
int LinkedList::getLength() const {
return itemCount;
}
// Places a Node at a given position (element at the same position is now pos+1).
template
bool LinkedList::insert(const int newPosition,
const ItemType& newEntry)
{
bool ableToInsert{ (newPosition >= 1) &&
(newPosition <= itemCount + 1) };
if (ableToInsert)
{
// Create a new node containing the new entry
auto newNodePtr{ std::make_shared>(newEntry) };
// Attach new node to chain
if (newPosition == 1)
{
// Insert new node at beginning of chain
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
}
else
{
// Find node that will be before new node
auto prevPtr{ getNodeAt(newPosition - 1) };
// Insert new node after node to which prevPtr points
newNodePtr->setNext(prevPtr->getNext());
prevPtr->setNext(newNodePtr);
} // end if
itemCount++; // Increase count of entries
} // end if
return ableToInsert;
} // end insert
// Removes the Node at the given position.
template
bool LinkedList::remove(const int position)
{
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
if (position == 1)
{
// Remove the first node in the chain
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
auto prevPtr{ getNodeAt(position - 1) };
// Point to node to delete
auto curPtr{ prevPtr->getNext() };
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(curPtr->getNext());
} // end if
itemCount--; // Decrease count of entries
} // end if
return ableToRemove;
} // end remove
// Replaces the Entry in the given Node with the new Entry.
template
void LinkedList::replace(const int position, const ItemType& newEntry)
throw(PrecondViolatedExcept)
{
if (position > this->getLength() || position < 1)
throw PrecondViolatedExcept("replace() called with a position out of bounds.");
else
getNodeAt(position)->setItem(newEntry);
}
template
void LinkedList::clear()
{
headPtr = nullptr;
itemCount = 0;
} // end clear
template
ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcept)
{
if (position > this->getLength() || position < 1)
throw PrecondViolatedExcept("getEntry() called with a position out of bounds.");
else
return getNodeAt(position)->getItem();
}
// Makes the calling Linked List's entries the same as the given Linked List.
template
void LinkedList::operator = (const LinkedList& arg) {
if (arg.isEmpty())
return;
// First section copies the given list into the calling list's existing nodes.
bool isThisLarger{ this->itemCount >= arg.itemCount };
auto currThisPtr{ this->headPtr };
auto currArgPtr{ arg.headPtr };
if (!this->isEmpty())
{
currThisPtr->setItem(currArgPtr->getItem());
for (int i = 2; i <= ((isThisLarger) ? arg.itemCount : this->itemCount); i++) {
currThisPtr = currThisPtr->getNext();
currArgPtr = currArgPtr->getNext();
currThisPtr->setItem(currArgPtr->getItem());
}
// If the calling list is larger then tidy up the end.
if (isThisLarger) {
this->itemCount = arg.itemCount;
currThisPtr->setNext(nullptr);
}
}
// Create new nodes and/or finish copying the entries.
if (!isThisLarger) {
currArgPtr = currArgPtr->getNext();
auto newNodePtr{ std::make_shared>(currArgPtr->getItem()) };
auto prevNodePtr{ currThisPtr };
prevNodePtr->setNext(newNodePtr);
if (this->isEmpty())
this->headPtr = newNodePtr;
for (int i = this->itemCount+1; i <= arg.itemCount; i++) {
newNodePtr = std::make_shared>(currArgPtr->getItem());
prevNodePtr->setNext(newNodePtr);
prevNodePtr = newNodePtr;
currArgPtr = currArgPtr->getNext();
}
this->itemCount = arg.itemCount;
}
}
#endif

More Related Content

Similar to #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf

5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdframbagra74
Β 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfFashionBoutiquedelhi
Β 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
Β 
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.pdfmail931892
Β 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.pptssuser0be977
Β 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
Β 
κ°•μ˜μžλ£Œ8
κ°•μ˜μžλ£Œ8κ°•μ˜μžλ£Œ8
κ°•μ˜μžλ£Œ8Young Wook Kim
Β 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfclimatecontrolsv
Β 
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.pdffeelinggift
Β 
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.pdffantoosh1
Β 
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 .pdfbermanbeancolungak45
Β 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfamarndsons
Β 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
Β 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfflashfashioncasualwe
Β 
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.pdfmail931892
Β 
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.pdffmac5
Β 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03Nasir Mehmood
Β 

Similar to #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf (20)

5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
Β 
Can you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdfCan you please debug this Thank you in advance! This program is sup.pdf
Can you please debug this Thank you in advance! This program is sup.pdf
Β 
Linked lists
Linked listsLinked lists
Linked lists
Β 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.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
Β 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
Β 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
Β 
κ°•μ˜μžλ£Œ8
κ°•μ˜μžλ£Œ8κ°•μ˜μžλ£Œ8
κ°•μ˜μžλ£Œ8
Β 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.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
Β 
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
Β 
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
Β 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
Β 
Sorter
SorterSorter
Sorter
Β 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
Β 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
Β 
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
Β 
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
Β 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
Β 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
Β 

More from angelsfashion1

The density of water. The shape of protein molecu.pdf
                     The density of water. The shape of protein molecu.pdf                     The density of water. The shape of protein molecu.pdf
The density of water. The shape of protein molecu.pdfangelsfashion1
Β 
Silica gel is a very polar absorbent, and so ho.pdf
                     Silica gel is a very polar absorbent, and so ho.pdf                     Silica gel is a very polar absorbent, and so ho.pdf
Silica gel is a very polar absorbent, and so ho.pdfangelsfashion1
Β 
PbI2-Pb2+ and 2I- Molar Concentration of Iodide.pdf
                     PbI2-Pb2+ and 2I-  Molar Concentration of Iodide.pdf                     PbI2-Pb2+ and 2I-  Molar Concentration of Iodide.pdf
PbI2-Pb2+ and 2I- Molar Concentration of Iodide.pdfangelsfashion1
Β 
NiSO4 as its a weak salt and will not dissociates.pdf
                     NiSO4 as its a weak salt and will not dissociates.pdf                     NiSO4 as its a weak salt and will not dissociates.pdf
NiSO4 as its a weak salt and will not dissociates.pdfangelsfashion1
Β 
NaCl is ionic compound and polar where as benzene.pdf
                     NaCl is ionic compound and polar where as benzene.pdf                     NaCl is ionic compound and polar where as benzene.pdf
NaCl is ionic compound and polar where as benzene.pdfangelsfashion1
Β 
No. Sodium lauryl sulfate will not form insoluble.pdf
                     No. Sodium lauryl sulfate will not form insoluble.pdf                     No. Sodium lauryl sulfate will not form insoluble.pdf
No. Sodium lauryl sulfate will not form insoluble.pdfangelsfashion1
Β 
Moles of H2O = 4.564=6.75 moles .pdf
                     Moles of H2O = 4.564=6.75 moles                .pdf                     Moles of H2O = 4.564=6.75 moles                .pdf
Moles of H2O = 4.564=6.75 moles .pdfangelsfashion1
Β 
it is ribose and the other is deoxyribose .pdf
                     it is ribose and the other is deoxyribose        .pdf                     it is ribose and the other is deoxyribose        .pdf
it is ribose and the other is deoxyribose .pdfangelsfashion1
Β 
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdfTrue.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdfangelsfashion1
Β 
These are two of the three major perspectives on sociology. Each of .pdf
These are two of the three major perspectives on sociology. Each of .pdfThese are two of the three major perspectives on sociology. Each of .pdf
These are two of the three major perspectives on sociology. Each of .pdfangelsfashion1
Β 
I have no Idea sorry. .pdf
                     I have no Idea sorry.                            .pdf                     I have no Idea sorry.                            .pdf
I have no Idea sorry. .pdfangelsfashion1
Β 
The main body of the new policy The employees can write blogs to .pdf
The main body of the new policy The employees can write blogs to .pdfThe main body of the new policy The employees can write blogs to .pdf
The main body of the new policy The employees can write blogs to .pdfangelsfashion1
Β 
The emotional and psychological effects the HIVAIDS epidemic on the.pdf
The emotional and psychological effects the HIVAIDS epidemic on the.pdfThe emotional and psychological effects the HIVAIDS epidemic on the.pdf
The emotional and psychological effects the HIVAIDS epidemic on the.pdfangelsfashion1
Β 
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdfSpecific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdfangelsfashion1
Β 
Solution is simple.Comments added start with calling a funct.pdf
Solution is simple.Comments added start with calling a funct.pdfSolution is simple.Comments added start with calling a funct.pdf
Solution is simple.Comments added start with calling a funct.pdfangelsfashion1
Β 
Since the gene responsible for the color of pea and shape of seed of.pdf
Since the gene responsible for the color of pea and shape of seed of.pdfSince the gene responsible for the color of pea and shape of seed of.pdf
Since the gene responsible for the color of pea and shape of seed of.pdfangelsfashion1
Β 
HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
                     HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf                     HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdfangelsfashion1
Β 
Questionaccording to this rule of solubility rules most sulfat.pdf
Questionaccording to this rule of solubility rules most sulfat.pdfQuestionaccording to this rule of solubility rules most sulfat.pdf
Questionaccording to this rule of solubility rules most sulfat.pdfangelsfashion1
Β 
Option (d) is correct. This is because dominant mutation involves si.pdf
Option (d) is correct. This is because dominant mutation involves si.pdfOption (d) is correct. This is because dominant mutation involves si.pdf
Option (d) is correct. This is because dominant mutation involves si.pdfangelsfashion1
Β 

More from angelsfashion1 (20)

The density of water. The shape of protein molecu.pdf
                     The density of water. The shape of protein molecu.pdf                     The density of water. The shape of protein molecu.pdf
The density of water. The shape of protein molecu.pdf
Β 
SrCl2 So.pdf
                     SrCl2                                      So.pdf                     SrCl2                                      So.pdf
SrCl2 So.pdf
Β 
Silica gel is a very polar absorbent, and so ho.pdf
                     Silica gel is a very polar absorbent, and so ho.pdf                     Silica gel is a very polar absorbent, and so ho.pdf
Silica gel is a very polar absorbent, and so ho.pdf
Β 
PbI2-Pb2+ and 2I- Molar Concentration of Iodide.pdf
                     PbI2-Pb2+ and 2I-  Molar Concentration of Iodide.pdf                     PbI2-Pb2+ and 2I-  Molar Concentration of Iodide.pdf
PbI2-Pb2+ and 2I- Molar Concentration of Iodide.pdf
Β 
NiSO4 as its a weak salt and will not dissociates.pdf
                     NiSO4 as its a weak salt and will not dissociates.pdf                     NiSO4 as its a weak salt and will not dissociates.pdf
NiSO4 as its a weak salt and will not dissociates.pdf
Β 
NaCl is ionic compound and polar where as benzene.pdf
                     NaCl is ionic compound and polar where as benzene.pdf                     NaCl is ionic compound and polar where as benzene.pdf
NaCl is ionic compound and polar where as benzene.pdf
Β 
No. Sodium lauryl sulfate will not form insoluble.pdf
                     No. Sodium lauryl sulfate will not form insoluble.pdf                     No. Sodium lauryl sulfate will not form insoluble.pdf
No. Sodium lauryl sulfate will not form insoluble.pdf
Β 
Moles of H2O = 4.564=6.75 moles .pdf
                     Moles of H2O = 4.564=6.75 moles                .pdf                     Moles of H2O = 4.564=6.75 moles                .pdf
Moles of H2O = 4.564=6.75 moles .pdf
Β 
it is ribose and the other is deoxyribose .pdf
                     it is ribose and the other is deoxyribose        .pdf                     it is ribose and the other is deoxyribose        .pdf
it is ribose and the other is deoxyribose .pdf
Β 
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdfTrue.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
True.It is a confirmal mapping transforming imaginary axis of s-pl.pdf
Β 
These are two of the three major perspectives on sociology. Each of .pdf
These are two of the three major perspectives on sociology. Each of .pdfThese are two of the three major perspectives on sociology. Each of .pdf
These are two of the three major perspectives on sociology. Each of .pdf
Β 
I have no Idea sorry. .pdf
                     I have no Idea sorry.                            .pdf                     I have no Idea sorry.                            .pdf
I have no Idea sorry. .pdf
Β 
The main body of the new policy The employees can write blogs to .pdf
The main body of the new policy The employees can write blogs to .pdfThe main body of the new policy The employees can write blogs to .pdf
The main body of the new policy The employees can write blogs to .pdf
Β 
The emotional and psychological effects the HIVAIDS epidemic on the.pdf
The emotional and psychological effects the HIVAIDS epidemic on the.pdfThe emotional and psychological effects the HIVAIDS epidemic on the.pdf
The emotional and psychological effects the HIVAIDS epidemic on the.pdf
Β 
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdfSpecific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
Specific heat of water = 4.184 Jg.oCHeat released by dissolution .pdf
Β 
Solution is simple.Comments added start with calling a funct.pdf
Solution is simple.Comments added start with calling a funct.pdfSolution is simple.Comments added start with calling a funct.pdf
Solution is simple.Comments added start with calling a funct.pdf
Β 
Since the gene responsible for the color of pea and shape of seed of.pdf
Since the gene responsible for the color of pea and shape of seed of.pdfSince the gene responsible for the color of pea and shape of seed of.pdf
Since the gene responsible for the color of pea and shape of seed of.pdf
Β 
HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
                     HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf                     HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
HClO4(aq) appear as H+ (aq) and ClO4 - (aq)Na2SO4.pdf
Β 
Questionaccording to this rule of solubility rules most sulfat.pdf
Questionaccording to this rule of solubility rules most sulfat.pdfQuestionaccording to this rule of solubility rules most sulfat.pdf
Questionaccording to this rule of solubility rules most sulfat.pdf
Β 
Option (d) is correct. This is because dominant mutation involves si.pdf
Option (d) is correct. This is because dominant mutation involves si.pdfOption (d) is correct. This is because dominant mutation involves si.pdf
Option (d) is correct. This is because dominant mutation involves si.pdf
Β 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
Β 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ9953056974 Low Rate Call Girls In Saket, Delhi NCR
Β 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
Β 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
Β 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
Β 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
Β 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
Β 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
Β 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
Β 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
Β 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
Β 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)Dr. Mazin Mohamed alkathiri
Β 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
Β 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
Β 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
Β 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
Β 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
Β 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
Β 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
Β 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
Β 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
Β 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
Β 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
Β 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Β 
Model Call Girl in Bikash Puri Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Bikash Puri  Delhi reach out to us at πŸ”9953056974πŸ”Model Call Girl in Bikash Puri  Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Bikash Puri Delhi reach out to us at πŸ”9953056974πŸ”
Β 
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Β 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
Β 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
Β 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
Β 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
Β 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
Β 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
Β 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
Β 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
Β 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
Β 

#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf

  • 1. #ifndef LINKED_LIST_ #define LINKED_LIST_ template class LinkedList { private: std::shared_ptr> headPtr; // Pointer to first node in the chain; // (contains the first entry in the list) int itemCount; // Current count of list items std::shared_ptr> getNodeAt(int position) const; public: LinkedList(); LinkedList(const LinkedList& aList); virtual ~LinkedList(); bool isEmpty() const; int getLength() const; bool insert(int newPosition, const ItemType& newEntry); bool remove(int position); void clear(); /** throw PrecondViolatedExcept if position < 1 or position > getLength(). */ ItemType getEntry(int position) const throw(PrecondViolatedExcept); /** throw PrecondViolatedExcept if position < 1 or position > getLength(). */ void replace(int position, const ItemType& newEntry) throw(PrecondViolatedExcept); // Operator Overloading void operator = (const LinkedList& arg); friend std::ostream& operator << (std::ostream& os, const LinkedList& arg) { os << "There are " << arg.getLength() << " values in the list:" << std::endl; for (int i{ 1 }; i <= arg.getLength(); i++) {
  • 2. os << arg.getEntry(i) << std::endl; } os << std::endl << std::endl << std::endl << std::endl; return os; } }; // Returns a pointer to the Node at the given position. template std::shared_ptr> LinkedList::getNodeAt (int position) const { std::shared_ptr> currPtr{ headPtr }; for (int i{ 1 }; i < position; i++) currPtr = currPtr->getNext(); return currPtr; } // Default constructor. template LinkedList::LinkedList() { headPtr = nullptr; itemCount = 0; } // Copy constructor. template LinkedList::LinkedList(const LinkedList& aList) { itemCount = aList.getLength(); auto currPtr{ aList.headPtr }; auto newNodePtr{ headPtr = std::make_shared>(currPtr->getItem()) }; auto prevNodePtr{ newNodePtr }; currPtr = currPtr->getNext(); for (int i{ 2 }; i <= itemCount; i++) { newNodePtr = std::make_shared>(currPtr->getItem()); prevNodePtr->setNext(newNodePtr); prevNodePtr = newNodePtr; currPtr = currPtr->getNext(); } } // Destructor.
  • 3. template LinkedList::~LinkedList() { headPtr = nullptr; itemCount = 0; } // Accessor/Info Functions. template bool LinkedList::isEmpty() const { return (itemCount > 0)?0:1; } template int LinkedList::getLength() const { return itemCount; } // Places a Node at a given position (element at the same position is now pos+1). template bool LinkedList::insert(const int newPosition, const ItemType& newEntry) { bool ableToInsert{ (newPosition >= 1) && (newPosition <= itemCount + 1) }; if (ableToInsert) { // Create a new node containing the new entry auto newNodePtr{ std::make_shared>(newEntry) }; // Attach new node to chain if (newPosition == 1) { // Insert new node at beginning of chain newNodePtr->setNext(headPtr); headPtr = newNodePtr; } else { // Find node that will be before new node
  • 4. auto prevPtr{ getNodeAt(newPosition - 1) }; // Insert new node after node to which prevPtr points newNodePtr->setNext(prevPtr->getNext()); prevPtr->setNext(newNodePtr); } // end if itemCount++; // Increase count of entries } // end if return ableToInsert; } // end insert // Removes the Node at the given position. template bool LinkedList::remove(const int position) { bool ableToRemove = (position >= 1) && (position <= itemCount); if (ableToRemove) { if (position == 1) { // Remove the first node in the chain headPtr = headPtr->getNext(); } else { // Find node that is before the one to delete auto prevPtr{ getNodeAt(position - 1) }; // Point to node to delete auto curPtr{ prevPtr->getNext() }; // Disconnect indicated node from chain by connecting the // prior node with the one after prevPtr->setNext(curPtr->getNext()); } // end if
  • 5. itemCount--; // Decrease count of entries } // end if return ableToRemove; } // end remove // Replaces the Entry in the given Node with the new Entry. template void LinkedList::replace(const int position, const ItemType& newEntry) throw(PrecondViolatedExcept) { if (position > this->getLength() || position < 1) throw PrecondViolatedExcept("replace() called with a position out of bounds."); else getNodeAt(position)->setItem(newEntry); } template void LinkedList::clear() { headPtr = nullptr; itemCount = 0; } // end clear template ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcept) { if (position > this->getLength() || position < 1) throw PrecondViolatedExcept("getEntry() called with a position out of bounds."); else return getNodeAt(position)->getItem(); } // Makes the calling Linked List's entries the same as the given Linked List. template void LinkedList::operator = (const LinkedList& arg) { if (arg.isEmpty()) return; // First section copies the given list into the calling list's existing nodes.
  • 6. bool isThisLarger{ this->itemCount >= arg.itemCount }; auto currThisPtr{ this->headPtr }; auto currArgPtr{ arg.headPtr }; if (!this->isEmpty()) { currThisPtr->setItem(currArgPtr->getItem()); for (int i = 2; i <= ((isThisLarger) ? arg.itemCount : this->itemCount); i++) { currThisPtr = currThisPtr->getNext(); currArgPtr = currArgPtr->getNext(); currThisPtr->setItem(currArgPtr->getItem()); } // If the calling list is larger then tidy up the end. if (isThisLarger) { this->itemCount = arg.itemCount; currThisPtr->setNext(nullptr); } } // Create new nodes and/or finish copying the entries. if (!isThisLarger) { currArgPtr = currArgPtr->getNext(); auto newNodePtr{ std::make_shared>(currArgPtr->getItem()) }; auto prevNodePtr{ currThisPtr }; prevNodePtr->setNext(newNodePtr); if (this->isEmpty()) this->headPtr = newNodePtr; for (int i = this->itemCount+1; i <= arg.itemCount; i++) { newNodePtr = std::make_shared>(currArgPtr->getItem()); prevNodePtr->setNext(newNodePtr); prevNodePtr = newNodePtr; currArgPtr = currArgPtr->getNext(); } this->itemCount = arg.itemCount;
  • 7. } } #endif Solution #ifndef LINKED_LIST_ #define LINKED_LIST_ template class LinkedList { private: std::shared_ptr> headPtr; // Pointer to first node in the chain; // (contains the first entry in the list) int itemCount; // Current count of list items std::shared_ptr> getNodeAt(int position) const; public: LinkedList(); LinkedList(const LinkedList& aList); virtual ~LinkedList(); bool isEmpty() const; int getLength() const; bool insert(int newPosition, const ItemType& newEntry); bool remove(int position); void clear(); /** throw PrecondViolatedExcept if position < 1 or position > getLength(). */ ItemType getEntry(int position) const throw(PrecondViolatedExcept); /** throw PrecondViolatedExcept if position < 1 or position > getLength(). */ void replace(int position, const ItemType& newEntry) throw(PrecondViolatedExcept); // Operator Overloading
  • 8. void operator = (const LinkedList& arg); friend std::ostream& operator << (std::ostream& os, const LinkedList& arg) { os << "There are " << arg.getLength() << " values in the list:" << std::endl; for (int i{ 1 }; i <= arg.getLength(); i++) { os << arg.getEntry(i) << std::endl; } os << std::endl << std::endl << std::endl << std::endl; return os; } }; // Returns a pointer to the Node at the given position. template std::shared_ptr> LinkedList::getNodeAt (int position) const { std::shared_ptr> currPtr{ headPtr }; for (int i{ 1 }; i < position; i++) currPtr = currPtr->getNext(); return currPtr; } // Default constructor. template LinkedList::LinkedList() { headPtr = nullptr; itemCount = 0; } // Copy constructor. template LinkedList::LinkedList(const LinkedList& aList) { itemCount = aList.getLength(); auto currPtr{ aList.headPtr }; auto newNodePtr{ headPtr = std::make_shared>(currPtr->getItem()) }; auto prevNodePtr{ newNodePtr }; currPtr = currPtr->getNext(); for (int i{ 2 }; i <= itemCount; i++) { newNodePtr = std::make_shared>(currPtr->getItem()); prevNodePtr->setNext(newNodePtr);
  • 9. prevNodePtr = newNodePtr; currPtr = currPtr->getNext(); } } // Destructor. template LinkedList::~LinkedList() { headPtr = nullptr; itemCount = 0; } // Accessor/Info Functions. template bool LinkedList::isEmpty() const { return (itemCount > 0)?0:1; } template int LinkedList::getLength() const { return itemCount; } // Places a Node at a given position (element at the same position is now pos+1). template bool LinkedList::insert(const int newPosition, const ItemType& newEntry) { bool ableToInsert{ (newPosition >= 1) && (newPosition <= itemCount + 1) }; if (ableToInsert) { // Create a new node containing the new entry auto newNodePtr{ std::make_shared>(newEntry) }; // Attach new node to chain if (newPosition == 1) { // Insert new node at beginning of chain newNodePtr->setNext(headPtr);
  • 10. headPtr = newNodePtr; } else { // Find node that will be before new node auto prevPtr{ getNodeAt(newPosition - 1) }; // Insert new node after node to which prevPtr points newNodePtr->setNext(prevPtr->getNext()); prevPtr->setNext(newNodePtr); } // end if itemCount++; // Increase count of entries } // end if return ableToInsert; } // end insert // Removes the Node at the given position. template bool LinkedList::remove(const int position) { bool ableToRemove = (position >= 1) && (position <= itemCount); if (ableToRemove) { if (position == 1) { // Remove the first node in the chain headPtr = headPtr->getNext(); } else { // Find node that is before the one to delete auto prevPtr{ getNodeAt(position - 1) }; // Point to node to delete auto curPtr{ prevPtr->getNext() };
  • 11. // Disconnect indicated node from chain by connecting the // prior node with the one after prevPtr->setNext(curPtr->getNext()); } // end if itemCount--; // Decrease count of entries } // end if return ableToRemove; } // end remove // Replaces the Entry in the given Node with the new Entry. template void LinkedList::replace(const int position, const ItemType& newEntry) throw(PrecondViolatedExcept) { if (position > this->getLength() || position < 1) throw PrecondViolatedExcept("replace() called with a position out of bounds."); else getNodeAt(position)->setItem(newEntry); } template void LinkedList::clear() { headPtr = nullptr; itemCount = 0; } // end clear template ItemType LinkedList::getEntry(int position) const throw(PrecondViolatedExcept) { if (position > this->getLength() || position < 1) throw PrecondViolatedExcept("getEntry() called with a position out of bounds."); else return getNodeAt(position)->getItem(); } // Makes the calling Linked List's entries the same as the given Linked List.
  • 12. template void LinkedList::operator = (const LinkedList& arg) { if (arg.isEmpty()) return; // First section copies the given list into the calling list's existing nodes. bool isThisLarger{ this->itemCount >= arg.itemCount }; auto currThisPtr{ this->headPtr }; auto currArgPtr{ arg.headPtr }; if (!this->isEmpty()) { currThisPtr->setItem(currArgPtr->getItem()); for (int i = 2; i <= ((isThisLarger) ? arg.itemCount : this->itemCount); i++) { currThisPtr = currThisPtr->getNext(); currArgPtr = currArgPtr->getNext(); currThisPtr->setItem(currArgPtr->getItem()); } // If the calling list is larger then tidy up the end. if (isThisLarger) { this->itemCount = arg.itemCount; currThisPtr->setNext(nullptr); } } // Create new nodes and/or finish copying the entries. if (!isThisLarger) { currArgPtr = currArgPtr->getNext(); auto newNodePtr{ std::make_shared>(currArgPtr->getItem()) }; auto prevNodePtr{ currThisPtr }; prevNodePtr->setNext(newNodePtr); if (this->isEmpty()) this->headPtr = newNodePtr; for (int i = this->itemCount+1; i <= arg.itemCount; i++) { newNodePtr = std::make_shared>(currArgPtr->getItem());
  • 13. prevNodePtr->setNext(newNodePtr); prevNodePtr = newNodePtr; currArgPtr = currArgPtr->getNext(); } this->itemCount = arg.itemCount; } } #endif