Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void SortedMultiMap::add(TKey c, TValue v) {
- //TODO - Implementation
- //adds a new key value pair to the sorted multi map
- int i; //indexul cu care plec de la head
- i = head; //primul nod din lista
- //creez elem nou pe poz firstEmpty
- //fac legaturile intre elem respectiv si cel de pe poz i
- //setez noul firstEmpty
- if (isEmpty() == true) //nu am niciun element, adaug primul element
- {
- head = firstEmpty;
- tail = firstEmpty;
- nodes[firstEmpty].info.first = c;
- nodes[firstEmpty].info.second = v;
- nodes[firstEmpty].next = -1;
- nodes[firstEmpty].prev = -1;
- nr_pairs++;
- firstEmpty = 1;
- if (nr_pairs == kapazitat)
- {
- //marire capacitate/realocare memorie
- int aux_kapazitat = kapazitat * 2;
- DLLANode* newElems = new DLLANode[aux_kapazitat];
- for (int i = 0; i < kapazitat; i++)
- newElems[i] = nodes[i];
- kapazitat = kapazitat * 2;
- delete[] nodes;
- nodes = newElems;
- }
- }
- else
- {
- //daca am elem in array si vreau sa adaug perechi
- if (nr_pairs == kapazitat)
- {
- //marire capacitate/realocare memorie
- int aux_kapazitat = kapazitat*2;
- DLLANode* newElems = new DLLANode[aux_kapazitat];
- for (int i = 0; i < kapazitat; i++)
- newElems[i] = nodes[i];
- kapazitat = kapazitat * 2;
- delete[] nodes;
- nodes = newElems;
- }
- while (rel(nodes[i].info.first, c) == true && nodes[i].next!=-1) //parcurg cat timp relatia e adevarata
- {
- i = nodes[i].next; //trece pe urmatorul
- }
- if (nodes[i].next == -1)
- {
- //introduc la final
- nodes[firstEmpty].info.first = c;
- nodes[firstEmpty].info.second = v;
- nodes[i].next = firstEmpty;
- nodes[firstEmpty].prev = i;
- nodes[firstEmpty].next = -1;
- nr_pairs++;
- firstEmpty++;
- }
- else
- {
- //introduc undeva la mijloc
- nodes[firstEmpty].info.first = c;
- nodes[firstEmpty].info.second = v;
- nodes[firstEmpty].prev = i;
- nodes[firstEmpty].next = nodes[i].next;
- nodes[i].next = firstEmpty;
- nr_pairs++;
- firstEmpty++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement