Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned long* insertLinear(unsigned long* tab, int size, unsigned long value){
- int stableIndex = getIndex(value);
- int index = stableIndex;
- int j = 0;
- while (index < size){
- if (tab[index] == NULL){
- tab[index] = value;
- break;
- }
- else index++;
- j++;
- }
- if (tab[index] == NULL){
- int i = 0;
- while (i < stableIndex){
- if (tab[i] == NULL){
- tab[i] = value;
- break;
- }
- else i++;
- j++;
- }
- }
- cout << "linear probing insert (complexity): " << j << endl;
- return tab;
- }
- int searchLinear(unsigned long* tab, int size, unsigned long toSearch){
- int index = getIndex(toSearch);
- int stableIndex = index;
- int j = 0;
- while (index < size){
- if (tab[index] == toSearch){ cout << "linear probing search (complexity): " << j << endl; return index; }
- else index++;
- j++;
- }
- if (tab[index] != toSearch){
- int i = 0;
- while (i < stableIndex){
- if (tab[i] == toSearch){ cout << "linear probing search (complexity): " << j << endl; return i; }
- else i++;
- j++;
- }
- }
- cout << "linear probing search (complexity): " << j << endl;
- return -1;
- }
- unsigned long* removeLinear(unsigned long* tab, int size, unsigned long toRemove){
- int index = getIndex(toRemove);
- int stableIndex = index;
- int j = 0;
- while (index < size){
- if (tab[index] == toRemove){
- tab[index] = NULL;
- while (index < size){
- insertLinear(tab, size, tab[index + 1]);
- }
- }
- else index++;
- j++;
- }
- if (tab[index] != toRemove){
- int i = 0;
- while (i < stableIndex){
- if (tab[i] == toRemove){
- tab[index] = NULL;
- while (index < size){
- insertLinear(tab, size, tab[index + 1]);
- }
- }
- else i++;
- j++;
- }
- }
- cout << "linear probing remove (complexity): " << j << endl;
- return tab;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement