Here is the code I have written
Code: Select all
template <class T> class sList{
private:
T data;
sList *next;
public:
sList()
{
this->data = 0;
this->next = 0;
}
sList(const T data)
{
this->data = data;
this->next = NULL;
}
~sList()
{
cout<<"Destructing Node: "<<this->data<<endl;
delete this->next;
}
void add(const T data)
{
sList *travPtr = this;
while(travPtr->next != 0)
{
travPtr = travPtr->next;
}
travPtr->next = new sList(data);
this->data++;
}
void remove(const T data)
{
sList *travPtr = this;
sList *delPtr = 0;
while(travPtr->next != 0)
{
if(data == travPtr->next->data)
{
delPtr = travPtr->next;
travPtr->next = delPtr->next;
cout<<"Deleting Node : "<<delPtr->data<<endl;
delete delPtr;
this->data--;
return;
}
travPtr = travPtr->next;
}
}
void display()
{
sList *travPtr = this->next;
cout<<"Total No. of nodes : "<<this->data<<endl;
while(travPtr != NULL) {
cout << travPtr->data << " --> ";
travPtr = travPtr->next;
}
cout << endl;
}
};
int main()
{
sList<int> myList;
myList.add(10);
myList.add(12);
myList.add(14);
myList.add(20);
myList.display();
myList.remove(12);
myList.display();
}
So first of all is this the way sLists are implemented in C++ and second how do I get this to work?
TIA