Created
January 7, 2014 20:07
-
-
Save sjchmiela/8305999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include "../libraries/min-chain.cpp" | |
using namespace std; | |
bool member(node* list, int val) | |
{ | |
while(list != NULL) | |
{ | |
if(list->w == val) | |
{ | |
return true; | |
} | |
list = list->next; | |
} | |
return false; | |
} | |
int remove_all(node *& list, int val) | |
{ | |
int count = 0; | |
while(list != NULL && list->w == val) | |
{ | |
list = list->next; | |
count++; | |
} | |
if(list == NULL) { return count; } | |
node* previous = list; | |
node* current = list->next; | |
while(current != NULL) | |
{ | |
if(current->w == val) | |
{ | |
count++; | |
previous->next = current->next; | |
current = current->next; | |
} | |
else | |
{ | |
previous = previous->next; | |
current = current->next; | |
} | |
} | |
return count; | |
} | |
int remove_repeating(node *& list) | |
{ | |
int count = 0; | |
node* current = list; | |
while(current != NULL) | |
{ | |
if(member(current->next, current->w)) | |
{ | |
count += remove_all(list, current->w); | |
} | |
current = current->next; | |
} | |
return count; | |
} | |
int main() | |
{ | |
node* chain = NULL; | |
append(chain, 2); | |
append(chain, 3); | |
append(chain, 5); | |
append(chain, 2); | |
append(chain, 7); | |
append(chain, 2); | |
append(chain, 5); | |
append(chain, 11); | |
print(chain); | |
printf("Removed: %d\n",remove_repeating(chain)); | |
print(chain); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment