void List::AddNode(int addData)
{
// definition of the AddNode function
nodePtr n = new node;
n->next = NULL;// last element in the list
n->data = addData;
if (head != NULL) // last value always set to null
// if there is already an element in the list.
{
curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = n;
}
else // we do not have an element in the list
{
head = n;
}
}
;
void List::AddNode(int addData)
{
// definition of the AddNode function
nodePtr n = new node;
n->next = NULL;// last element in the list
n->data = addData;
if (head == NULL) // last value always set to null
// if there is already an element in the list.
{
head = n;
}
else
{
nodePtr it = head;
while (it->next != NULL)
{
it = it->next;
}
it->next = n;
}
}
It does not. If you really intend to make a SortedList class, remove List::AddNode() and only keep the List::AddOrdered() function. By the way List::AddOrdered() does not look like it does the job, whether the function is actually correct or not.
void List::AddOrdered(int data)
{
nodePtr *headOrNext; // points to head, or some node's next pointer
nodePtr cur; // current node we're looking at
// point headOrNext at the nodePtr where the new node should go.
for (headOrNext = &head; (cur = *headOrNext); headOrNext = &cur->next) {
if (cur->data > data) break;
}
// headOrNext now points to the pointer to the next item.
nodePtr n = new Node;
n->data = data;
n->next = *headOrNext;
*headOrNext = n;
if (cur == nullptr) {
// Inserting at the end. Update tail
tail = n;
}
}