rod1231234

this is sparsa

May 7th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.57 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cstdio>
  4. using namespace std;
  5. template <class X>
  6. class no;
  7. template <class X>
  8. class matriz;
  9. template <class X>
  10. class no
  11. {
  12.     int lin;
  13.     int col;
  14.     X dado;
  15.     no<X>* prox;
  16.     no<X>* ant;
  17.     no<X>* cima;
  18.     no<X>* baixo;
  19.     no(){ prox=NULL;ant=NULL;cima=NULL;baixo=NULL;dado=0;lin= -1;col= -1;}
  20.     no(X d){prox=NULL;ant=NULL;cima=NULL;baixo=NULL;dado = d;}
  21. public:
  22.     friend class matriz<X>;
  23. };
  24. template <class X>
  25. class matriz
  26. {
  27.     int maiorl;
  28.     int maiorc;
  29.     no<X> *starter;
  30. public:
  31.     matriz()
  32.     {
  33.         starter=NULL;
  34.         maiorl = (-1);
  35.         maiorc = (-1);
  36.     }
  37.     bool vazio()
  38.     {
  39.         if(starter==NULL)
  40.             return true;
  41.         else
  42.             return false;
  43.        
  44.        
  45.     }
  46.     ~matriz();
  47.     void inserir (int,int,X);
  48.     void excluir (int,int);
  49.     void imprimir();
  50. };
  51. template <class X>
  52. void matriz<X>:: inserir(int linha, int coluna, X d)
  53. {
  54.     if(vazio())
  55.     {
  56.         starter= new no<X>;
  57.     }
  58.     no<X>* ptr;
  59.     no<X>* aux;
  60.     no<X>* elem;
  61.     no<X>* auxc;
  62.     ptr = new no<X>;
  63.     elem = new no<X>;
  64.     elem->dado = d;
  65.     elem->lin = linha;
  66.     elem->col = coluna;
  67.     auxc = starter;
  68.     while(auxc->prox!=NULL){
  69.         if(coluna<auxc->prox->col && coluna>auxc->col)
  70.         {
  71.             ptr->col = coluna;
  72.             ptr->prox=auxc->prox;
  73.             ptr->ant=auxc;
  74.             auxc->prox->ant=ptr;
  75.             auxc->prox= ptr;
  76.             ptr->baixo=elem;
  77.             break;
  78.         }
  79.         if(auxc->col==coluna)
  80.         {
  81.             aux=auxc;
  82.             while(aux->baixo!=NULL)
  83.             {
  84.                 if(aux->lin>linha)
  85.                 {
  86.                     elem->cima=aux->cima;
  87.                     elem->baixo=aux;
  88.                     aux->cima=elem;
  89.                 }
  90.                 if(aux->lin<linha && aux->baixo->lin>linha)
  91.                 {
  92.                     elem->cima=aux;
  93.                     elem->baixo=aux->baixo;
  94.                     aux->baixo->cima=elem;
  95.                     aux->baixo=elem;
  96.                 }
  97.                 if(aux->lin==linha)
  98.                 {
  99.                     aux->dado=d;
  100.                 }
  101.                 aux=aux->baixo;
  102.                
  103.             };
  104.             if(aux->col==coluna && aux->baixo==NULL)
  105.             {
  106.                 if(aux->lin==linha)
  107.                 {
  108.                     aux->dado=d;
  109.                 }
  110.             }
  111.            
  112.             if(auxc->baixo==NULL)
  113.             {
  114.                 auxc->baixo=elem;
  115.                 elem->cima=auxc;
  116.             }
  117.             auxc=auxc->prox;
  118.             break;
  119.         }
  120.         auxc=auxc->prox;
  121.     };
  122.     if(auxc->prox==NULL)
  123.     {
  124.         ptr->col = coluna;
  125.         ptr->baixo=elem;
  126.         ptr->ant=auxc;
  127.         auxc->prox=ptr;
  128.         elem->cima=ptr;
  129.     }
  130.     no<X> *auxl;
  131.     auxl=starter;
  132.     while(auxl->baixo!=NULL){
  133.         if(linha<auxl->lin && linha>auxl->cima->lin)
  134.         {
  135.             ptr->lin = linha;
  136.             ptr->baixo=auxc->baixo;
  137.             ptr->cima=auxc;
  138.             auxc->baixo->cima=ptr;
  139.             auxc->baixo= ptr;
  140.             ptr->prox=elem;
  141.             break;
  142.         }
  143.         if(auxl->lin==linha)
  144.         {
  145.             elem->lin = linha;
  146.             aux=auxl;
  147.             while(aux->prox!=NULL)
  148.             {
  149.                 if(aux->col>coluna)
  150.                 {
  151.                     elem->prox=aux;
  152.                     elem->ant=aux->ant;
  153.                     aux->ant=elem;
  154.                    
  155.                 }
  156.                 if(aux->col<coluna && aux->prox->col>coluna)
  157.                 {
  158.                     elem->ant=aux;
  159.                     elem->prox=aux->prox;
  160.                     aux->prox->ant=elem;
  161.                     aux->prox=elem;
  162.                 }
  163.                 if(aux->col==coluna)
  164.                 {
  165.                     aux->dado=d;
  166.                 }
  167.                 aux=aux->prox;
  168.                
  169.             };
  170.            
  171.             if(aux->lin==linha && aux->prox==NULL)
  172.             {
  173.                 if(aux->col==coluna)
  174.                 {
  175.                     aux->dado=d;
  176.                 }
  177.             }
  178.             if(aux->prox==NULL)
  179.             {
  180.                 aux->prox=elem;
  181.                 elem->ant=aux;
  182.             }
  183.             auxl=auxl->baixo;
  184.             break;
  185.            
  186.         }
  187.         auxl=auxl->baixo;
  188.     };
  189.     if(auxl->baixo==NULL)
  190.     {
  191.         ptr->lin = linha;
  192.         ptr->prox=elem;
  193.         ptr->cima=auxl;
  194.         auxl->baixo=ptr;
  195.         elem->ant =ptr;
  196.     }
  197.     if(linha>maiorl)
  198.     {
  199.         maiorl=linha;
  200.     }
  201.     if(coluna>maiorc)
  202.     {
  203.         maiorc=coluna;
  204.     }
  205. };
  206. template <class X>
  207. void matriz<X> :: excluir(int linha,int coluna)
  208. {
  209.     no<X>* aux;
  210.     no<X>* ptr;
  211.     aux= new no<X>;
  212.     aux = starter;
  213.     while(aux!=NULL)
  214.     {
  215.         if(linha==aux->lin)
  216.         {
  217.             ptr=aux;
  218.             while(ptr!=NULL)
  219.             {
  220.                 if(ptr->col==coluna)
  221.                 {
  222.                     ptr->dado=0;
  223.                     cout<<"\n";
  224.                     break;
  225.                 }
  226.                 ptr=ptr->prox;
  227.             };
  228.         }
  229.         aux=aux->baixo;
  230.     };                                            
  231. };
  232. template <class X>
  233. void matriz<X> :: imprimir()
  234. {
  235.     no <X>* auxl;
  236.     no <X>* aux;
  237.     auxl=starter->baixo;
  238.     for(int i=0;i<=maiorl;i++)
  239.     {
  240.         if(auxl->lin==i)
  241.         {
  242.             aux=auxl->prox;
  243.             for(int j=0;j<=maiorc;j++)
  244.             {
  245.                 if(aux->col==j)
  246.                 {
  247.                     cout<<aux->dado<<" ";
  248.                     if(aux->prox!=NULL)
  249.                     {
  250.                         aux=aux->prox;
  251.                     }
  252.                 }
  253.                 else
  254.                 {
  255.                     cout<<"0 ";
  256.                 }
  257.             }
  258.             if(auxl->baixo->baixo!=NULL)
  259.             {
  260.                 auxl=auxl->baixo->baixo;
  261.             }
  262.         }
  263.         else
  264.         {
  265.             for(int j=0;j<=maiorc;j++)
  266.             {
  267.                 cout<<"0 ";
  268.             }
  269.         }
  270.         cout<<"\n";
  271.     }
  272.    
  273. };
  274. template <class X>
  275. matriz<X> :: ~matriz()
  276. {
  277.     no<X>* auxl;
  278.     no<X>* aux;
  279.     no<X>* ptr;
  280.     //cout<<"dest\n";
  281.     auxl=starter->baixo;
  282.     //cout<<"dest1\n";
  283.     while(auxl != NULL)
  284.     {
  285.         //cout<<"while auxl\n";
  286.         while(auxl->prox != NULL)
  287.         {
  288.             aux = auxl;
  289.             while(aux->prox != NULL)
  290.             {
  291.                 //cout<<"while aux\n";
  292.                 ptr = aux;
  293.                 aux = aux->prox;
  294.             }
  295.             delete aux;
  296.             ptr->prox = NULL;
  297.         }
  298.         auxl = auxl->baixo;
  299.     }
  300.     auxl=aux=starter;
  301.     while(auxl->baixo!=NULL)
  302.         auxl=auxl->baixo;
  303.     while(aux->prox!=NULL)
  304.         aux=aux->prox;
  305.     while(auxl!=starter)
  306.     {
  307.         auxl=auxl->cima;
  308.         delete auxl->baixo;
  309.     }
  310.     while(aux!=starter)
  311.     {
  312.         aux=aux->ant;
  313.         delete aux->prox;
  314.     }
  315.     delete starter;
  316. };
  317. int main(int argc, char *argv[])
  318. {
  319.     matriz<int> teste;
  320.     teste.inserir(1,4,9);
  321.     cout<<"inserir1ok\n";
  322.     teste.inserir(2,2,1);
  323.     cout<<"inserir2ok\n";
  324.     teste.inserir(3,0,2);
  325.     cout<<"inserir3ok\n";
  326.     teste.inserir(4,2,5);
  327.     teste.inserir(2,4,6);
  328.     cout<<"inserir4ok\n";
  329.     teste.imprimir();
  330.     cout<<"\n";
  331.     teste.inserir(3,0,7);
  332.     teste.imprimir();
  333.     teste.excluir(3,0);
  334.     teste.imprimir();
  335.     system("PAUSE");
  336.     return EXIT_SUCCESS;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment