I've been stuck trying to figure out how I can implement a NODE based link list that can hold anything (via templates).
I've got the Node class ready and working(tested with my Stack class).
I was curious where I could be going wrong when making the function called insertAtIndex where I take in the data to store and the index at where it should be stored.
My Node class
template <class T>
class Node
{
public:
T *data; //the object information
Node<T> *next; //pointer to the next node element
Node()
{
next = 0;
data = 0;
}
};
Here's my Linked List class so far
template <class T>
class LinkedList
{
private:
Node<T> *head;
int count;
public:
LinkedList()
{
head = 0;
count = 0;
}
int getCount()
{
return count;
}
void insertAtIndex(T* dat, int index)
{
Node<T> * temp = new Node<T>(dat);
if(index == 0)
{
temp = head;
temp->data = dat;
temp->next = temp->next;
temp->next = temp;
delete temp;
count++;
}
else if(index <= count)
{
Node<T> *cursor = new Node<T>();
for(int i = 0; i < index - 1; i++)
{
cursor = cursor->next;
}
Node<T> * temp = new Node<T>();
temp->data = dat;
temp->next = cursor->next;
cursor->next = temp;
count++;
}
}
void Print()
{
// Temp pointer
Node<T> *tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->next == NULL ) {
cout << *tmp->data;
cout << " --> ";
cout << "NULL" << endl;
}
else
{
// Parse and print the list
do
{
cout << *tmp->data;
cout << " --> ";
tmp = tmp->next;
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
};
You should add Node(T*) constructor to your Node class:
template <class T>
class Node
{
public:
T *data; //the object information
Node<T> *next; //pointer to the next node element
Node()
{
next = 0;
data = 0;
}
Node(T* newData) { // <<---------- Here it is
next = 0;
data = newData;
}
};
You need to rethink how you are adding nodes to the list. Here is your code with comments on what it is actually doing.
Node<T> * temp = new Node<T>(dat); // Create new node, good
if(index == 0)
{
temp = head; // Throw away pointer to new node, bad. temp is now head.
temp->data = dat; // Overwrite head's data, bad
temp->next = temp->next; // Set a variable to itself, does nothing.
temp->next = temp; // Change head's next to point to itself, bad
delete temp; // delete the head node, bad
count++;
}
What you actually want to do is:
That's all - don't delete anything.
Once you have that working, move on to the more complicated part of adding a node to the middle or the list (your else part).
A debugger (or at least output statements) is essential for learning where your code is going wrong. You can look at the state of your variables at each point.
You might want to consider a base node class that only has a next pointer as a member, then an inherited class that adds a template data member. Common list functions could be used for the base class.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.