Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. void SortedMultiMap::add(TKey c, TValue v) {
  2.     //TODO - Implementation
  3.  
  4.     //adds a new key value pair to the sorted multi map
  5.  
  6.     int i; //indexul cu care plec de la head
  7.     i = head; //primul nod din lista
  8.    
  9.  
  10.     //creez elem nou pe poz firstEmpty
  11.     //fac legaturile intre elem respectiv si cel de pe poz i
  12.     //setez noul firstEmpty
  13.  
  14.     if (isEmpty() == true) //nu am niciun element, adaug primul element
  15.     {
  16.         head = firstEmpty;
  17.         tail = firstEmpty;
  18.         nodes[firstEmpty].info.first = c;
  19.         nodes[firstEmpty].info.second = v;
  20.         nodes[firstEmpty].next = -1;
  21.         nodes[firstEmpty].prev = -1;
  22.         nr_pairs++;
  23.         firstEmpty = 1;
  24.         if (nr_pairs == kapazitat)
  25.         {
  26.             //marire capacitate/realocare memorie
  27.             int aux_kapazitat = kapazitat * 2;
  28.             DLLANode* newElems = new DLLANode[aux_kapazitat];
  29.             for (int i = 0; i < kapazitat; i++)
  30.                 newElems[i] = nodes[i];
  31.             kapazitat = kapazitat * 2;
  32.             delete[] nodes;
  33.             nodes = newElems;
  34.         }
  35.  
  36.     }
  37.     else
  38.     {
  39.         //daca am elem in array si vreau sa adaug perechi
  40.  
  41.         if (nr_pairs == kapazitat)
  42.         {
  43.             //marire capacitate/realocare memorie
  44.             int aux_kapazitat = kapazitat*2;
  45.             DLLANode* newElems = new DLLANode[aux_kapazitat];
  46.             for (int i = 0; i < kapazitat; i++)
  47.                 newElems[i] = nodes[i];
  48.             kapazitat = kapazitat * 2;
  49.             delete[] nodes;
  50.             nodes = newElems;
  51.         }
  52.  
  53.         while (rel(nodes[i].info.first, c) == true && nodes[i].next!=-1) //parcurg cat timp relatia e adevarata
  54.         {
  55.             i = nodes[i].next; //trece pe urmatorul
  56.         }
  57.  
  58.         if (nodes[i].next == -1)
  59.         {
  60.             //introduc la final
  61.  
  62.             nodes[firstEmpty].info.first = c;
  63.             nodes[firstEmpty].info.second = v;
  64.             nodes[i].next = firstEmpty;
  65.             nodes[firstEmpty].prev = i;
  66.             nodes[firstEmpty].next = -1;
  67.             nr_pairs++;
  68.             firstEmpty++;
  69.  
  70.         }
  71.         else
  72.         {
  73.             //introduc undeva la mijloc
  74.  
  75.             nodes[firstEmpty].info.first = c;
  76.             nodes[firstEmpty].info.second = v;
  77.            
  78.             nodes[firstEmpty].prev = i;
  79.             nodes[firstEmpty].next = nodes[i].next;
  80.             nodes[i].next = firstEmpty;
  81.  
  82.             nr_pairs++;
  83.             firstEmpty++;
  84.         }
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement