A remove_duplicates() function in my linked list class: Is this right?

AI Thread Summary
The discussion centers on the implementation of a generic linked list class, specifically focusing on a function to remove duplicate elements. The current approach utilizes a set to track unique values but has been criticized for its inefficiency, as it deletes nodes without properly unlinking them from the list. This leads to the need for rescanning the list each time a node is removed, which is not optimal. Suggestions for improvement include maintaining two pointers per node (Next and Previous) to facilitate easier node removal, or creating a "remove_next_node" function that takes a pointer to the preceding node for deletion. The importance of managing pointers correctly to avoid memory leaks and ensure efficient operations is emphasized, with acknowledgment that the current implementation requires significant fixes to enhance performance.
Jamin2112
Messages
973
Reaction score
12
I'm making a generic linked list class. For reference, firstNodePtr and lastNodePtr are pointers to the nodes at the beginning and end of the list. I'm trying to make a function that removes duplicate elements, which in turn calls a function for deleting a particular node. I think it does what I want (or does it?) but I also feel like this is overly complicated and I could simplify it somehow. Thoughts?

Code:
template <class T> void List<T>::remove_duplicates() {
	std::set<T> thisSet;
	node* curNodePtr = firstNodePtr;
	while (curNodePtr) {
		if (!thisSet.count(curNodePtr->val)) {
			thisSet.insert(curNodePtr->val);
			curNodePtr = curNodePtr->next;
		} else { 
			node* curNodePtrCopy = curNodePtr;
			curNodePtr = curNodePtr->next;
			delete curNodePtrCopy;
		}
	}
	
}

template <class T> void List<T>::remove_node(node* thisNodePtr) {
	if (thisNodePtr == firstNodePtr) {  
		firstNodePtr = firstNodePtr->next;
	} else { 
		node* prevNodePtr = firstNodePtr;
		while (prevNodePtr->next != thisNodePtr)
			prevNodePtr = prevNodePtr->next;
		prevNodePtr->next = thisNodePtr->next;
		if (thisNodePtr == lastNodePtr)
			lastNodePtr = prevNodePtr;
	}
	delete thisNodePtr;	
}
 
Technology news on Phys.org
The obvious problem is that you have no efficient way of backing up through the list. When you code to remove "thisNodePtr", you're stuck having to return to the start of the list and search forward through each preceding node.

There are basically two ways of doing what you're looking to do. The most obvious is to create and maintain two node pointer per node - not just Next, but Next and Previous.

The other way is to code a "remove_next_node" instead of a "remove_node". The parameter would be a pointer to the node before the node to be deleted. If you do that, use a null value for your input pointer when you want to remove the first node. Also, check for the last node being passed - since the "Next" pointer on that node is null because there's no "Next" after the last node.
 
Just for the record, the current implementation of remove_duplicates() does not call remove_node(). Instead it does something very bad: it deletes nodes without unlinking them from the list.

If you intend to call remove_node() from that place, it will be quite inefficient, because for each removed node the list is rescanned from the beginning.

One way to deal with that is by maintaining a pointer to the previous node - not in the list, but in remove_duplicates().
 
Thanks, guys. I think I've made all the necessary fixes.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
9
Views
3K
Replies
1
Views
1K
Replies
2
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
3
Views
8K
Back
Top