Initial commit of code for dopelib, and ch 1, 2, and 12

This commit is contained in:
2024-10-29 16:04:54 -04:00
parent 11601352d3
commit afbf4c2f7e
25 changed files with 745 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#include "chapter2.hpp"
#include <unordered_map>
/*
Remove Dups!
Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
*/
void remove_dups(DopeLinkedList* ll){
DopeNode* prev = ll->GetHead();
DopeNode* node = ll->GetHead();
DopeNode* tmp = NULL;
std::unordered_map<int, int> dup_tracker;
int iter_cnt = 0;
while(node->GetNext() != NULL){
if(dup_tracker.count(node->GetData())){
// remove node
prev->SetNext(node->GetNext());
tmp = node;
node = node->GetNext();
tmp->Reset();
} else {
dup_tracker[node->GetData()] += 1;
node = node->GetNext();
if (iter_cnt != 0){
prev = prev->GetNext();
}
}
iter_cnt++;
}
}