Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7
then the result should be 2 7 1 12
here is what I have:

#include <iostream>
using namespace std;

class NumberList
{
private:
// Declare a structure for the list
struct ListNode
{
double value; // The value in this node
struct ListNode *next; // To point to the next node
}; 

ListNode *head; // List head pointer

public:
// Constructor
NumberList()
{ head = NULL; }

// Destructor
~NumberList();

// Linked list operations
void appendNode(double);
void deleteDuplicates(double);
void displayList() const;
};

void NumberList::appendNode(double num)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list

// Allocate a new node and store num there.
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;

// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;

// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;

// Insert newNode as the last node.
nodePtr->next = newNode;
}
}

void NumberList::displayList() const
{
ListNode *nodePtr; // To move through the list

// Position nodePtr at the head of the list.
nodePtr = head;

// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;

// Move to the next node.
nodePtr = nodePtr->next;
}
}

// here is where i need help this function doesn't delete the repeated numbers

void NumberList::deleteDuplicates(double num)
{
ListNode* nodePtr=head;
int count=0;
while(nodePtr !=NULL)
{
count++;
if(nodePtr->value==num)
{
delete nodePtr;
}
else 
nodePtr=nodePtr->next;
}}int main()
{
// Define a NumberList object.
NumberList list;

// Build the list with some values.
list.appendNode(2);
list.appendNode(7);
list.appendNode(1);
list.appendNode(7);
list.appendNode(12);
list.appendNode(7);

// Display the list.
cout << "Here are the initial values:\n";
list.displayList();
cout << endl;
//delete duplicates
list.deleteDuplicates(7);
cout<<"here is the new list:\n";
list.displayList();
cout<<endl;
return 0;
}

You still have time to edit your post. Hit "Edit This Post", then "Go Advanced", and highlight your code, then press the "#" icon on the editing bar, then "Save Changes" and you should be in business.

if(nodePtr->value==num)
{
delete nodePtr;
}

Here you are directly freeing up the the node, deleting it. So if it's in the middle or at the beginning of the linked list, your list is corrupted, broke.

head->2 -> 7 -> 1 -> 7 -> 12 -> 7

You are deleting node (with value 7) and you get following picture.

head->2
somewhere :1 -> 7 -> 12 -> 7

What you want is to get next pointer of the previous node and make it to point next pointer of the current node (so linked list is not corrupted ), then delete the node. Also check your counter to see whether value is duplicated or not.

if (!head) return;
while (head->next){
if (head->next->value==num){
counter++;
if (counter>1){
tmp = head->next;
head->next = tmp->next;
delete tmp;
}
}
}

I'm not giving you full code, and this is general sketch of what you should do. If you need further help, let me know.

The logic of your delete function is just the first step towards where I think you want to go. That is, once you can delete a given node no matter where it is in the list, then you have to develop a method to retain a single occurrence of each value in the list, and not delete them all, which is what the logic of you delete function would do if it didn't destroy the list. Then you may well want to be able to delete multiple duplicated values such as changing this: 2->7->2->7 to 2->7. Each of those steps can be enhancements to your current delete function, once you get the first step completed.

You can just throw them all into an std::set and then read them out into a vector. It will take care of the duplicates for you.

Here you are directly freeing up the the node, deleting it. So if it's in the middle or at the beginning of the linked list, your list is corrupted, broke.

head->2 -> 7 -> 1 -> 7 -> 12 -> 7

You are deleting node (with value 7) and you get following picture.

head->2
somewhere :1 -> 7 -> 12 -> 7

What you want is to get next pointer of the previous node and make it to point next pointer of the current node (so linked list is not corrupted ), then delete the node. Also check your counter to see whether value is duplicated or not.


I'm not giving you full code, and this is general sketch of what you should do. If you need further help, let me know.

Hi thanks for the help but i am getting 3 errors from the code that u wrote it said something like '=' cannot convert listnode to int. it is coming from tmp= head->next;
then there is another one that says ->next must point to a class/struct/union is coming from head->next= tmp->next;
then third delete tmp says cannot delete objects that are not pointers. also what else do i have to add? thanks

Hi thanks for the help but i am getting 3 errors from the code that u wrote it said something like '=' cannot convert listnode to int. it is coming from tmp= head->next;
then there is another one that says ->next must point to a class/struct/union is coming from head->next= tmp->next;
then third delete tmp says cannot delete objects that are not pointers. also what else do i have to add? thanks

Hi.
I think you've declared tmp to be an integer, it should be a pointer to ListNode.
ListNode * tmp ;

Hi.
I think you've declared tmp to be an integer, it should be a pointer to ListNode.
ListNode * tmp ;

yes thanks it compiles now but still doesn't give nothing in my output the function is

void NumberList::deleteDuplicates(double num)
{
	ListNode *tmp;
	int count=0;
	if(!head) 
		return;
	while(head->next)
	{
		if(head->next->value==num)
		{
			count++;
			if(count>1)
			{
				tmp= head->next;
				head->next= tmp->next;
				delete tmp;
			}
		}
	}
}

what else am I missing, thanks for the help

what else am I missing, thanks for the help

Code tags --AGAIN!!!!

It seems that with the warning above, and the information posted all over this site about CODE tags, like
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used, even in responses to your posts
6) Even on the background of the box you actually typed your message in

you'd think one would get the message...

Comments
Great!

Could you please copy-n-paste what you are getting as an output after deleting 7's. Have you validated your appendNode function? have you validated your print function?
Could you please post all your code (within valid code tag).

This article has been dead for over six months. Start a new discussion instead.