Advertisement
wilk_maciej

MAPA

May 21st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5.  
  6. template<class T, class E>
  7. class el{
  8. public:
  9.   T first;
  10.   E second;
  11.   el<T,E> *nast;
  12.   el<T,E> *poprz;
  13.  
  14. };
  15. template<class T, class E>
  16. class MAPA{
  17. public:
  18.   el<T,E> *pier;
  19.  
  20.   MAPA(){
  21.     pier=NULL;
  22.   }
  23.   ~MAPA(){
  24.     while (pier!=NULL){
  25.       if (pier->nast != NULL){
  26.         class el<T,E> *nowy=pier->nast;
  27.         delete pier;
  28.         pier = nowy;
  29.       }
  30.       else{
  31.         delete pier;
  32.         pier = NULL;
  33.       }
  34.     }
  35.   }
  36.  
  37.   E & operator [](const T &o1){
  38.     el<T,E> *w, *wp, *wn;
  39.     for(w=pier, wp=0; w!=0; w=w->nast){
  40.       if(w->first == o1) return w->second;
  41.       wp=w;
  42.     }
  43.     wn=new el<T,E>;
  44.     wn->first=o1;
  45.     wn->nast=0;
  46.     if(wp==0) pier=wn;
  47.     else {
  48.       wp->nast=wn;
  49.       wn->poprz=wp;
  50.     }
  51.     return wn->second;
  52.   }
  53.  
  54.   class iterator{
  55. public:
  56.     el<T,E> *ws;
  57.     int operator !=(const iterator &o1){
  58.       if(ws!=o1.ws) return 1;
  59.       return 0;
  60.     }
  61.  
  62.     iterator & operator ++(int){
  63.       ws=ws->nast;
  64. //      return *ws;
  65.     }
  66.  
  67.     el<T,E> * operator ->(void){
  68.       return ws;
  69.     }
  70.   };
  71.  
  72.  
  73.   iterator begin(void){
  74.     iterator i1;
  75.     i1.ws=pier;
  76.     return i1;
  77.   }
  78.  
  79.  
  80.   iterator end(void){
  81.     iterator i1;
  82.     i1.ws=0;
  83.     return i1;
  84.   }
  85.  
  86.   void swap(el<T,E> *jeden, el<T,E> *dwa){
  87.     el<T,E> *prev1_temp;
  88.     prev1_temp=jeden.poprz;
  89.     el<T,E> *next1_temp;
  90.     next1_temp=jeden.nast;
  91.     el<T,E> *prev2_temp;
  92.     prev2_temp=dwa.poprz;
  93.     el<T,E> *next2_temp;
  94.     next2_temp=dwa.nast;
  95.     if (prev1_temp){
  96.       prev1_temp.nast=dwa;
  97.       dwa.poprz=prev1_temp;
  98.     }
  99.     else dwa.poprz=NULL;
  100.    
  101.     if (next1_temp){
  102.       next1_temp.poprz=dwa;
  103.       dwa.nast=next1_temp;
  104.     }
  105.     else dwa.nast=NULL;
  106.     if (prev2_temp){
  107.       prev2_temp.nast=jeden;
  108.       jeden.poprz=prev2_temp;
  109.     }
  110.     else jeden.poprz=NULL;
  111.    
  112.     if (next2_temp){
  113.       next2_temp.poprz=jeden;
  114.       jeden.nast=next2_temp;
  115.     }
  116.     else jeden.nast=NULL;
  117.   }
  118.  
  119.   void sort_first(){
  120.     for(el<T,E> *p=pier; p!=NULL; p=p->nast){
  121.       for(el<T,E> *n=pier.nast; n!=NULL; n=n->nast){
  122.         if (p->first < n->first) swap(p,n);        
  123.       }
  124.     }
  125.   }
  126.   void sort_second(){
  127.     for(el<T,E> *p=pier; p!=NULL; p=p->nast){
  128.       for(el<T,E> *n=pier->nast; n!=NULL; n=n->nast){
  129.         if (p->second < n->second) swap(p,n);
  130.       }
  131.     }
  132.   }
  133.  
  134.  
  135.  
  136.  
  137. };
  138.  
  139. int main(){
  140.   MAPA<string, string> m;
  141.   m["AA:"]="66";
  142.   m["BB"]="656";
  143.   m["CC"]="6634";
  144.   string a;
  145.   m.sort_second();
  146.   a = m["AA:"];
  147.   cout <<a <<endl;
  148.   for(MAPA<string, string>:: iterator i=m.begin();i!=m.end();i++){
  149.     cout <<i->first <<" " <<i->second <<endl;
  150.   }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement