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

IMPLEMENT THE LinkedList.cpp class LinkedList.h template class LinkedList { publ

ID: 3920031 • Letter: I

Question

IMPLEMENT THE LinkedList.cpp class

LinkedList.h

template

class LinkedList

{

public:

LinkedList();

~LinkedList();

void insert(const Type &item, int);

void remove();

Type retrieve() const;

int gotoPrior();

int gotoNext();

int gotoBeginning();

void clear();

int empty() const;

LinkedList(const LinkedList &src);

protected:

class ListElement

{

public:

ListElement(const Type &item, ListElement* nextP) :

element(item), next(nextP) {}

Type element;

ListElement* next;

};

ListElement *head;

ListElement *cursor;

};

#endif

main.cpp

// HW4Q4.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include

#include "linkedList.h"

#include "LinkedList.cpp"

using namespace std;

// HW4 PartIV - HW4PartIV.cpp

/* NOTE for using the LinkedList Test Program

This program allows users testing their implementation interactively.

To properly use this code, you should add the following logics to your

LinkedList implementation.

Testing Insert Method:

The test program will check the functionality of insert method without the second parameter.

By defualt, insert the element after the cursor pointing element.

If you want to insert an element at different location (before or replace), you should properly

provide the second argument, then activate the insert method.

*/

// Outputs the elements in a list. If the list is empty, outputs

// "Empty list". This operation is intended for testing and

// debugging purposes only.

template

void outList(LinkedList list)

{

char tmp;

if (list.empty())

cout << "Empty list" << endl;

else

{

list.gotoBeginning();

do

{

tmp = list.retrieve();

cout << tmp << " -> ";

} while (list.gotoNext());

cout << endl;

}

}

void main()

{

LinkedList testList; // Test list

char testElement; // List element

char cmd; // Input command

cout << endl << "Commands:" << endl;

cout << " +x : Insert x after the current element" << endl;

cout << " - : Remove the current element" << endl;

cout << " @ : Display the current element" << endl;

cout << " < : Go to the beginning of the list" << endl;

cout << " N : Go to the next element" << endl;

cout << " P : Go to the prior element" << endl;

cout << " C : Clear the list" << endl;

cout << " E : Empty list?" << endl;

cout << " Q : Quit the program" << endl;

cout << endl;

do

{

outList(testList); // Output list

cout << endl << "Command: "; // Read command

cin >> cmd;

if ((cmd == '+') || (cmd == '=') || (cmd == '#'))

cin >> testElement;

switch (cmd)

{

case '+':

cout << "Insert " << testElement << endl;

testList.insert(testElement, 0);

break;

case '-':

cout << "Remove the current element" << endl;

testList.remove();

break;

case '@':

cout << "Current element is "

<< testList.retrieve() << endl;

break;

case '<':

if (testList.gotoBeginning())

cout << "Go to the beginning of the list" << endl;

else

cout << "Failed -- list is empty" << endl;

break;

case 'N': case 'n':

if (testList.gotoNext())

cout << "Go to the next element" << endl;

else

cout << "Failed -- either at the end of the list "

<< "or the list is empty" << endl;

break;

case 'P': case 'p':

if (testList.gotoPrior())

cout << "Go to the prior element" << endl;

else

cout << "Failed -- either at the beginning of the "

<< "list or the list is empty" << endl;

break;

case 'C': case 'c':

cout << "Clear the list" << endl;

testList.clear();

break;

case 'E': case 'e':

if (testList.empty())

cout << "List is empty" << endl;

else

cout << "List is NOT empty" << endl;

break;

case 'Q': case 'q':

break;

default:

cout << "Inactive or invalid command" << endl;

}

} while ((cmd != 'Q') && (cmd != 'q'));

}

PLEASE HELP ME TO IMPLEMENT THE LinkedList.cpp class

Explanation / Answer

Linkedlist.cpp

#include "LinkedList.h"

template <class T>

LinkedList<T>::LinkedList()

{

first = NULL;

last = NULL;

}

template <class T>

LinkedList<T>::~LinkedList()

{

Node<T>* temp = first;

while(temp != NULL)

{

temp = temp->next;

delete(first);

first = temp;

}

}

template <class T>

void LinkedList<T>::insertAtBack(T valueToInsert)

{

Node<T>* newNode;

newNode->val = valueToInsert;

newNode->next = NULL;

Node<T>* temp = first;

if (temp != NULL)

{

while (temp->next != NULL)

{

temp = temp->next;

}

temp->next = newNode;

}

else

{

first = newNode;

}

}

template <class T>

bool LinkedList<T>::removeFromBack()

{

if (first == NULL && last == NULL) {return false;}

if (first == last)

{

cout<<"First is equal to Last."<<endl;

delete(first);

first = last = NULL;

return true;

}

else

{

Node<T>* temp = first;

int nodeCount = 0;

while (temp != NULL)

{

nodeCount = nodeCount + 1;

temp = temp->next;

}

Node<T>* temp2 = first;

for(int i = 1; i < (nodeCount - 1); i++)

{

temp2 = temp2->next;

}

cout << temp2->val<<endl;

delete(temp2->next);

last = temp2;

last->next = NULL;

return true;

}

}

template <class T>

void LinkedList<T>::print()

{

Node<T>* temp = first;

if (temp == NULL)

{

cout<<"";

}

if (temp->next == NULL)

{

cout<<temp->val;

}

else

{

while (temp != NULL)

{

cout<< temp->val;

temp = temp->next;

cout<< ",";

}

}

}

template <class T>

bool LinkedList<T>::isEmpty()

{

if (first == NULL && last == NULL) {return true;}

else {return false;}

}

template <class T>

int LinkedList<T>::size()

{

if (first == NULL && last == NULL) {return 0;}

Node<T>* temp = first;

int nodeCounter = 0;

while (temp != NULL)

{

nodeCounter = nodeCounter + 1;

temp = temp->next;

}

return nodeCounter;

}

template <class T>

void LinkedList<T>::clear()

{

Node<T>* temp = first;

while(temp != NULL)

{

temp = temp->next;

first = temp;

delete(temp);

}

}

template <class T>

void LinkedList<T>::insertAtFront(T valueToInsert)

{

Node<T>* newNode;

newNode->val = valueToInsert;

if(first == NULL)

{

first = newNode;

}

else

{

newNode->next = first;

first = newNode;

}

}

template <class T>

bool LinkedList<T>::removeFromFront()

{

if (first == NULL && last == NULL) {return false;}

else

{

Node<T>* temp;

temp = first;

first = first->next;

delete(temp);

return true;

}

}

template <class T>

T& LinkedList<T>::firstNum()

{

return first->val;

}

Linkedlist.h

using namespace std;

#include <iostream>

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

template <class T>

struct Node

{

T val;

Node<T> *next;

};

template <class T>

class LinkedList

{

public:

LinkedList();

~LinkedList();

void insertAtBack(T valueToInsert);

bool removeFromBack();

void print();

bool isEmpty();

int size();

void clear();

void insertAtFront(T valueToInsert);

bool removeFromFront();

T& firstNum();

private:

Node<T> *first;

Node<T> *last;

};

#endif