I am trying to create a template LinkedList class that will allow Tag objects to
ID: 3630018 • Letter: I
Question
I am trying to create a template LinkedList class that will allow Tag objects to be appended to the list. Everything seems to go reasonably, just that I am having problems instantiating the objects correctly. I would appreciate it if you could look at the following code:code:
//LinkedList.h - defines a linked list
#include <iostream>
using namespace std;
template<class T>
struct nodeType
{
T info; //some arb data - eg to contain a pointer to a class
nodeType<T> *link;
};
template <class T>
class linkedListType
{
public:
void initializeList();
//function to initialize first element in list to zero
//postcondition: first=NULL last=NULL
bool isEmpty();
//function to see if list contains elements
int Length();
//function to return the number of elements initialized in the list
T firstItem();
//function to return the first item in the list
T lastItem();
//function to return the last item in the list
void appendItem(const T& newItem);
//function to append an item to the list
linkedListType();
//constructor
~linkedListType();
//destructor
protected:
nodeType<T> *first; //pointer to first node
nodeType<T> *last; //pointer to last node
int count; //stores number of items in the list
};
template<class T>
bool linkedListType<T>::isEmpty()
{
return(first==NULL);
}
template<class T>
linkedListType<T>::linkedListType()
{
first=NULL;
last=NULL;
count=0;
}
template<class T>
linkedListType<T>::~linkedListType()
{
nodeType<T> *temp;
while(first!=NULL)
{
temp=first;
first=first->link;
delete temp;
}
last=NULL;
count=0;
}
template<class T>
linkedListType<T>::Length()
{
return count;
}
template<class T>
T linkedListType<T>::firstItem()
{
if(!isEmpty())
{
return first->info;
//NB: return the first node's data member eg: return any object
}
else
{
return NULL;
}
}
template<class T>
T linkedListType<T>::lastItem()
{
if(!isEmpty())
{
return last->info;
//NB: return the first node's data member eg: return any object
}
else
{
return NULL;
}
}
template<class T>
void linkedListType<T>::appendItem(const T& newItem)
{
nodeType<T> newNode;
newNode->info=newItem;
last->link=newNode;
last=newNode;
count++;
}
and this also
The kanban.h file: code
using namespace std;
class Tag
{
public:
Tag(int pTagNumber = 0,int pLastScannedWCID = 0,bool pHasDemand=true);
//contructor
//postcondition: tagnumber=0, workcentre last scanned=0; HasDemand = true
int tagNumber();
//function to return the tag number
private:
int TagNum;
int LastScannedWCID;
bool HasDemand;
};
Tag::Tag(int pTagNumber,int pLastScannedWCID,bool pHasDemand)
{
TagNum=pTagNumber;
LastScannedWCID=pLastScannedWCID;
HasDemand=pHasDemand;
}
int Tag::tagNumber()
{
return TagNum;
}
Explanation / Answer
#include "LinkedList.h" #include "kanban.h" #include using namespace std; void main() { //Tag *tagptr = new Tag(1,1); //create new tag object //coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.