I have a list with names of employers such as:
Node 1: Jill, Matt, Joe, Bob, Matt
Node 2: Jeff, James, John, J
I changed your variable names to be more readable, but kept the forward-declaration in case you wanted it like that. The issues I found were that you were always inserting the node at the end of the list, regardless of if you found it was a duplicate. In addition, your commented lines appeared close, but only if alone. With the pp=p
stuff, it would cause issues. Try the following and see if it works. You'll still be leaking memory, but it will get you started:
void list::put(int i) { //Where i is a random number
node *current =head;
node *added =new node(employers[i]);
node *tail =head;
node *prev = NULL;
bool foundRepeat = false;
while (current!=NULL)
{
if (current->data == added->data)
{
if (prev)
prev->next = current->next;
current->next=head;
head=current;
foundRepeat = true;
break;
}
prev = current;
current=current->next;
}
if (!foundRepeat)
{
while (tail->next)
{
tail=tail->next;
}
tail->next=added;
}
N++;
}