Singly Linked List - Delete duplicate nodes

I don't know why my code is not working. Can someone tell me what I did wrong? It's from line 249 - 266



void SLL::deleteDuplicate(){
Node* temp = head;
Node* p = temp;
Node* q = temp->next;
while(temp != NULL){

while(q != NULL && p != tail){

if(q == temp){p->next = q->next; delete q; q = p->next;}

else if(q == tail && q == temp){p = tail; tail = NULL; delete q;}

else{q = q->next; p = p->next;}

}

temp = temp->next;
}

/* Node* temp = head;


while(temp != NULL){

Node* p = head;
Node* q = head->next;

while(q != NULL && p->next != NULL){
cout << "q: " << q->letter << endl;
cout << "p: " << p->letter << endl;
system("pause");
if(q->letter == temp->letter){
if(q == temp){
continue;
}
else{
p->next = q->next;
delete q;
q = p->next;
}
}
else if(q == tail && q->letter == temp->letter) //if it's the last node
{
p->next = NULL;
delete tail;
tail = p;
// q = tail;
}

/* else{
p->next = q->next;
delete q;
q = p->next;
} */

/* q = q->next;
p = p->next;




}

temp = temp->next;

}
*/


}



int main(){

SLL list;
SLL list2;

list.insertToFront('C');
list.insertToFront('B');
list.insertToFront('A');

list.printAll(); //PRINTS OUT "A->B->C->"
cout << "Insert 'Z' to front \n\n";
list.insertToFront('Z'); //INSERT 'Z' TO THE FRONT OF 'A'

list.printAll(); //PRINTS OUT Z->A->B->C->
cout << "Delete 'Z' from the front \n\n";
list.deleteFront(); //DELETES 'Z' FROM FRONT





list.printAll(); //PRINTS OUT "A->B->C->"
cout << "Insert 'Z' to the back \n\n";
list.insertToBack('Z'); //INSERT 'Z' AT THE END

list.printAll(); //PRINTS OUT "A->B->C->Z->"
cout << "Delete 'Z' from the back \n\n";
list.deleteBack(); //DELETES LAST 'Z' FROM THE LIST

list.printAll(); //PRINTS OUT "A->B->C->"


list.insertToBack('D');
list.insertToBack('E');
list.insertToBack('F');
list.printAll();
cout << "Reverse list \n\n";
list.reverse();
cout << "Delete 3rd Node from back 'D'";
list.deleteKthNodeFromBack(3);
list.printAll();

cout << "Delete extra letters \n\n";

list2.insertToBack('A'); /*A->A->E->B->B->C->D->D->C->E will become A->E->B->C->D*/
list2.insertToBack('A');
list2.insertToBack('E');
list2.insertToBack('B');
list2.insertToBack('B');
list2.insertToBack('C');
list2.insertToBack('D');
list2.insertToBack('D');
list2.insertToBack('C');
list2.insertToBack('E');



list2.printAll();

list2.deleteDuplicate();

list2.printAll();















system("pause");

return 0;


}[/code]
That algorithm is pretty confusing. I just don't understand why all those pointers start from pointing to head.

I suggest you write out your RemoveDuplicate algorithm in pseudo-code first, just to be clear on what you intend, then implement your algorithm once you've had time to think about it.
Topic archived. No new replies allowed.