Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <cstdio>
- using namespace std;
- template <class X>
- class no;
- template <class X>
- class matriz;
- template <class X>
- class no
- {
- int lin;
- int col;
- X dado;
- no<X>* prox;
- no<X>* ant;
- no<X>* cima;
- no<X>* baixo;
- no(){ prox=NULL;ant=NULL;cima=NULL;baixo=NULL;dado=0;lin= -1;col= -1;}
- no(X d){prox=NULL;ant=NULL;cima=NULL;baixo=NULL;dado = d;}
- public:
- friend class matriz<X>;
- };
- template <class X>
- class matriz
- {
- int maiorl;
- int maiorc;
- no<X> *starter;
- public:
- matriz()
- {
- starter=NULL;
- maiorl = (-1);
- maiorc = (-1);
- }
- bool vazio()
- {
- if(starter==NULL)
- return true;
- else
- return false;
- }
- ~matriz();
- void inserir (int,int,X);
- void excluir (int,int);
- void imprimir();
- };
- template <class X>
- void matriz<X>:: inserir(int linha, int coluna, X d)
- {
- if(vazio())
- {
- starter= new no<X>;
- }
- no<X>* ptr;
- no<X>* aux;
- no<X>* elem;
- no<X>* auxc;
- ptr = new no<X>;
- elem = new no<X>;
- elem->dado = d;
- elem->lin = linha;
- elem->col = coluna;
- auxc = starter;
- while(auxc->prox!=NULL){
- if(coluna<auxc->prox->col && coluna>auxc->col)
- {
- ptr->col = coluna;
- ptr->prox=auxc->prox;
- ptr->ant=auxc;
- auxc->prox->ant=ptr;
- auxc->prox= ptr;
- ptr->baixo=elem;
- break;
- }
- if(auxc->col==coluna)
- {
- aux=auxc;
- while(aux->baixo!=NULL)
- {
- if(aux->lin>linha)
- {
- elem->cima=aux->cima;
- elem->baixo=aux;
- aux->cima=elem;
- }
- if(aux->lin<linha && aux->baixo->lin>linha)
- {
- elem->cima=aux;
- elem->baixo=aux->baixo;
- aux->baixo->cima=elem;
- aux->baixo=elem;
- }
- if(aux->lin==linha)
- {
- aux->dado=d;
- }
- aux=aux->baixo;
- };
- if(aux->col==coluna && aux->baixo==NULL)
- {
- if(aux->lin==linha)
- {
- aux->dado=d;
- }
- }
- if(auxc->baixo==NULL)
- {
- auxc->baixo=elem;
- elem->cima=auxc;
- }
- auxc=auxc->prox;
- break;
- }
- auxc=auxc->prox;
- };
- if(auxc->prox==NULL)
- {
- ptr->col = coluna;
- ptr->baixo=elem;
- ptr->ant=auxc;
- auxc->prox=ptr;
- elem->cima=ptr;
- }
- no<X> *auxl;
- auxl=starter;
- while(auxl->baixo!=NULL){
- if(linha<auxl->lin && linha>auxl->cima->lin)
- {
- ptr->lin = linha;
- ptr->baixo=auxc->baixo;
- ptr->cima=auxc;
- auxc->baixo->cima=ptr;
- auxc->baixo= ptr;
- ptr->prox=elem;
- break;
- }
- if(auxl->lin==linha)
- {
- elem->lin = linha;
- aux=auxl;
- while(aux->prox!=NULL)
- {
- if(aux->col>coluna)
- {
- elem->prox=aux;
- elem->ant=aux->ant;
- aux->ant=elem;
- }
- if(aux->col<coluna && aux->prox->col>coluna)
- {
- elem->ant=aux;
- elem->prox=aux->prox;
- aux->prox->ant=elem;
- aux->prox=elem;
- }
- if(aux->col==coluna)
- {
- aux->dado=d;
- }
- aux=aux->prox;
- };
- if(aux->lin==linha && aux->prox==NULL)
- {
- if(aux->col==coluna)
- {
- aux->dado=d;
- }
- }
- if(aux->prox==NULL)
- {
- aux->prox=elem;
- elem->ant=aux;
- }
- auxl=auxl->baixo;
- break;
- }
- auxl=auxl->baixo;
- };
- if(auxl->baixo==NULL)
- {
- ptr->lin = linha;
- ptr->prox=elem;
- ptr->cima=auxl;
- auxl->baixo=ptr;
- elem->ant =ptr;
- }
- if(linha>maiorl)
- {
- maiorl=linha;
- }
- if(coluna>maiorc)
- {
- maiorc=coluna;
- }
- };
- template <class X>
- void matriz<X> :: excluir(int linha,int coluna)
- {
- no<X>* aux;
- no<X>* ptr;
- aux= new no<X>;
- aux = starter;
- while(aux!=NULL)
- {
- if(linha==aux->lin)
- {
- ptr=aux;
- while(ptr!=NULL)
- {
- if(ptr->col==coluna)
- {
- ptr->dado=0;
- cout<<"\n";
- break;
- }
- ptr=ptr->prox;
- };
- }
- aux=aux->baixo;
- };
- };
- template <class X>
- void matriz<X> :: imprimir()
- {
- no <X>* auxl;
- no <X>* aux;
- auxl=starter->baixo;
- for(int i=0;i<=maiorl;i++)
- {
- if(auxl->lin==i)
- {
- aux=auxl->prox;
- for(int j=0;j<=maiorc;j++)
- {
- if(aux->col==j)
- {
- cout<<aux->dado<<" ";
- if(aux->prox!=NULL)
- {
- aux=aux->prox;
- }
- }
- else
- {
- cout<<"0 ";
- }
- }
- if(auxl->baixo->baixo!=NULL)
- {
- auxl=auxl->baixo->baixo;
- }
- }
- else
- {
- for(int j=0;j<=maiorc;j++)
- {
- cout<<"0 ";
- }
- }
- cout<<"\n";
- }
- };
- template <class X>
- matriz<X> :: ~matriz()
- {
- no<X>* auxl;
- no<X>* aux;
- no<X>* ptr;
- //cout<<"dest\n";
- auxl=starter->baixo;
- //cout<<"dest1\n";
- while(auxl != NULL)
- {
- //cout<<"while auxl\n";
- while(auxl->prox != NULL)
- {
- aux = auxl;
- while(aux->prox != NULL)
- {
- //cout<<"while aux\n";
- ptr = aux;
- aux = aux->prox;
- }
- delete aux;
- ptr->prox = NULL;
- }
- auxl = auxl->baixo;
- }
- auxl=aux=starter;
- while(auxl->baixo!=NULL)
- auxl=auxl->baixo;
- while(aux->prox!=NULL)
- aux=aux->prox;
- while(auxl!=starter)
- {
- auxl=auxl->cima;
- delete auxl->baixo;
- }
- while(aux!=starter)
- {
- aux=aux->ant;
- delete aux->prox;
- }
- delete starter;
- };
- int main(int argc, char *argv[])
- {
- matriz<int> teste;
- teste.inserir(1,4,9);
- cout<<"inserir1ok\n";
- teste.inserir(2,2,1);
- cout<<"inserir2ok\n";
- teste.inserir(3,0,2);
- cout<<"inserir3ok\n";
- teste.inserir(4,2,5);
- teste.inserir(2,4,6);
- cout<<"inserir4ok\n";
- teste.imprimir();
- cout<<"\n";
- teste.inserir(3,0,7);
- teste.imprimir();
- teste.excluir(3,0);
- teste.imprimir();
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment