"algorithm" in the book, in algorithm 3.1 refers to the implementation of the map, here according to the idea of the book, with a one-way list of simple writing.
#ifndef Sequential_h#defineSequential_hTemplate<classKclassV>classlink{Private: classNode { public: K Key=0; V value=0; Node*next=nullptr; public: Node (K, V, node*); Node () {}; };Private: Node*first ; int_length; int_capicty; public: Link (); V&Get(K key); voidput (K key, V value); intSize ()Const; intCapicty ()Const; BOOLDelete (K key); ~Link ();}; Template<classKclassV>Link<k, v>::node::node (K key, V value, node*Node) { this->key =key; this->value =value; Next=node;} Template<classKclassV>V& link<k, v>::Get(K Key) {Node*x =first ; for(inti =0; I < _length&& x! = nullptr; i++) { if(x->key = = Key)returnX->value; X= x->next; }}//If there is no k, a new Key-value pair is added at the end of the listtemplate<classKclassV>voidlink<k, v>::p ut (K key, V value) {Node*x =first ; BOOLFlag =false; for(inti =0; I < _length; i++) { if(x->key = = Key) {x->value = value; flag =true; break; } X= x->next; } if(!Flag) { if(_length+1>=_capicty) {Node*m =first ; _capicty*=2; Node**n =Newnode*[_capicty]; for(inti =0; I < _capicty; i++) {n[i]=NewNode (); } for(inti =0; I < _capicty-1; i++) {n[i]->next = N[i +1]; } for(inti =0; I < _capicty && m!=nullptr; i++) {n[i]=m; M= m->next; } Delete[] first; first= n[0]; } intL =_length; Node*y =first ; while(l) {y= y->next; L--; } y->value =value; Y->key =key; Y->next = y->next; _length++; }}template<classKclassV>Link<k, v>::~Link () {if(first! = Nullptr)Delete[] first;} Template<classKclassV>Link<k, v>:: Link () {Node* * n =Newnode*[Ten]; _capicty=Ten; _length=0; for(inti =0; I <Ten; i++) {n[i]=NewNode (); } for(inti =0; I <Ten-1; i++) {n[i]->next = n[i+1]; } first= n[0];} Template<classKclassV>intlink<k, v>::size ()Const{ return_length;}//current linked list capacitytemplate<classKclassV>intlink<k, v>::capicty ()Const{ return_capicty;} Template<classKclassV>BOOLlink<k, v>::D elete (K key) {BOOLFlag =false; Node* N = first->next; Node* B =first ; if(b->key = =Key) {_length--; first= first->next; return true; } for(inti =1; I < _length; i++) { if(n->key = =Key) {b->next = n->next; _length--; Flag=true; break; } b= b->next; N= n->next; } returnflag;}#endif
C + + linked list and key-value pairs