Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

It\'s a C++ program of singly linked list class template. Bellow is the header f

ID: 3721729 • Letter: I

Question

It's a C++ program of singly linked list class template. Bellow is the header file provided. It should hold an object from other class that I already have. The comments are the functions that need to be made in the SLList

SLList.hpp:

// It's a template class

class SLList {

public:

// Default Constructor

// Constructor that takes first item for list

// It shall not be possible to create copies or assign directly to the linked list

// Destructor

// begin()

// end()

// A function called empty that lets you know if the list is empty or not

// A function called size that tells you how big the list is

// A function called front that returns the key value of the front node (return by value only)

// A function called push_back that adds a key to the end of the list

// A function called erase that takes as a parameter an iterator that indicates the Node to be erased.

            // It returns an iterator that indicates the position after the node that was erased

            // See the list class function of the same name at cplusplus.com

// A function called clear that erases the list, but leaves the list object in a viable state

private:

// Declare your Node class privately here

// A pointer to the first node in the list

};

// Probably a good spot for the iterator class declaration

// It is contained within a template class

class SLList::iterator {

public:

// Constructor that takes a Node pointer

// Overloaded postfix and prefix increment operators

// No decrements, this will be a forward-only iterator

// Overloaded de-reference operator, returns by value only

// Overloaded equality operator

// Overloaded inequality operator

private:

// Node pointer (resource being managed by the iterator)

};

// Implementations of classes go here

Explanation / Answer

// It's a template class

template <typename T>

class SLList {

class node;

public:

SLList<T>() noexcept {

head = nullptr;

}

¬SLList<T>() {}

class iterator;

iterator begin()

{

return iterator(head);

}

iterator end()

{

return iterator(nullptr);

}

bool isEmpty() {

return

}

int size() {

if(head == nullptr) {

return 0;

}

node *temp = head;

int count = 0;

while(temp != nullptr) {

count++;

temp = temp->next;

}

return count;

}

int front() {

if(head != nullptr) {

return head->value;

}

else {

return 0;

}

}

void push_back(T n) {

node *temp = initialiseNode(n);

if (getHead() == nullptr) {

getHead() = temp;

}

else {

node *temphead = head;

while(temphead->next != nullptr) {

temphead = temphead->next;

}

temphead->next = temp;

}

}

int erase(int n) {

int index = 1; // calculates the index values

node *temphead = head;

if(temphead->value == n) { // checks if value matches with n

head = head->next; // if it matches then head points to second node

delete temphead; // delete the first node

return 0;

} else {

while(temphead->next != nullptr) { // if the next node is not nullptr the continue the loop

if(temphead->next->value == n) { // if value matches

node *temp = temphead->next; // the next node is saved

temphead->next = temphead->next->next; // the next node is skipped and next to next node is pointed to by current node

delete temp; // delete the next node

return index; // return index

}

temphead = temphead->next; // temphead points to next node

index++;

}

}

}

void erase() {

node *temphead = head;

while(temphead != null) { // we keep deleting all nodes of the list until it is nullptr

node *temp = temphead; // we save the current node to delete it later

temphead = temphead->next; // temphead now points to the next node

delete temp; // the current node is deleted

}

head = temphead; // head points to nullptr again

}


private:

class node

{

T value;

Node* next;

friend class SLList; // since SLList will use this class functions and objects

};

node* initialiseNode(T value)

{

Node* newNode = new node;

newNode->value = value;

newNode->next = nullptr;

return newNode;

}

node*& getHead() // returns reference to the head node

{

return head;

}

static node* head;

};

class SLList::iterator {

public:

iterator() noexcept :

mCurrentNode (head) { }

iterator(const node* newNode) noexcept :

mCurrentNode (newNode) { }

iterator& operator=(node* newNode)

{

this->mCurrentNode = newNode;

return *this;

}

// Prefix ++ overload

iterator& operator++()

{

if (mCurrentNode)

mCurrentNode = mCurrentNode->next;

return *this;

}

// Postfix ++ overload

iterator operator++(int)

{

iterator iterator = *this;

++*this;

return iterator;

}

bool operator!=(const iterator& it)

{

return mCurrentNode != it.mCurrentNode;

}

bool operator==(const iterator& it)

{

return mCurrentNode == it.mCurrentNode;

}

int operator*()

{

return mCurrentNode->data;

}

private:

const node* mCurrentNode;

};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote