Advertisement
Vesker

Untitled

Sep 8th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.68 KB | None | 0 0
  1. // Copyright [2015] <Salvatore Junior Amore>
  2. #include "Elemento.hpp"
  3.  
  4. template<typename T>
  5. class ListaEnc {
  6.  private:
  7.     Elemento<T> *head;
  8.     int size;
  9.  
  10.  public:
  11.     ListaEnc() {
  12.         head = NULL;
  13.         size = 0;
  14.     }
  15.     ~ListaEnc() {
  16.         size = 0;
  17.         delete head;
  18.     }
  19.     // inicio
  20.     void adicionaNoInicio(const T& dado) {
  21.         Elemento<T> *novo = new Elemento<T>(dado, head);
  22.             if (novo == NULL) {
  23.                 throw 0;
  24.             } else {
  25.             head = novo;
  26.             size++;
  27.             }
  28.      }
  29.     T retiraDoInicio() {
  30.         if (listaVazia()) {
  31.             throw 0;
  32.         } else {
  33.         Elemento<T> *refRemovida = head;
  34.             if (refRemovida == NULL) {
  35.                 throw 0;
  36.             } else {
  37.                 head->setProximo(head->getProximo()->getProximo());
  38.                 size--;
  39.                 return refRemovida->getInfo();
  40.             }
  41.         }
  42.     }
  43.     void eliminaDoInicio() {
  44.         if(listaVazia()) {
  45.             throw 0;
  46.         } else {
  47.             Elemento<T> *novo = new Elemento<T>(NULL, head);
  48.             if(novo == NULL) {
  49.                 throw 0;
  50.             } else {
  51.                 head->setProximo(novo->getProximo());
  52.                 size--;
  53.                 novo->~Elemento();
  54.             }
  55.         }
  56.     }
  57.     // posicao
  58.     void adicionaNaPosicao(const T& dado, int pos) {
  59.         if(pos > size|| pos < 0) {
  60.             throw 0;
  61.         }  else {
  62.         Elemento<T> *novo = new Elemento<T>(dado, head);
  63.         Elemento<T> *anterior;
  64.             if(novo == NULL) {
  65.                 throw 0;
  66.             } else {
  67.                     if(pos == 0) {
  68.                         adicionaNoInicio(dado);
  69.                     } else {
  70.                         anterior = head;
  71.                         int cont = 0;
  72.                             while(cont < pos-1) {
  73.                                 anterior = anterior->getProximo();
  74.                                 cont++;
  75.                         }
  76.                         novo->setProximo(anterior->getProximo());
  77.                         anterior->setProximo(novo);
  78.                         size++;
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     int posicao(const T& dado) const {
  84.         Elemento<T> *novo;
  85.             if(listaVazia()) {
  86.                 throw 0;
  87.             } else {
  88.                 novo = head;
  89.                    
  90.                     int cont = 0;
  91.                     while(cont <= size) {
  92.                         if(novo->getInfo() == dado) {
  93.                             return cont;
  94.                             } else {
  95.                             novo = novo->getProximo();
  96.                             cont++;
  97.                             }
  98.                         }
  99.                     if (cont > size) { throw 0;}
  100.                     }
  101.                     }
  102.     T* posicaoMem(const T& dado) const {
  103.     }
  104.     bool contem(const T& dado) {
  105.         int cont = 1;
  106.         if(listaVazia()) {
  107.             throw 0;
  108.         } else {
  109.         Elemento<T> *novo = new Elemento<T>(dado, head);
  110.             if(novo == NULL) {
  111.             throw 0;
  112.             } else {
  113.                 while (cont <= size &&
  114.                 novo->getInfo()!= novo->getProximo()->getInfo()) {
  115.                 novo->setProximo(novo->getProximo());
  116.                 cont++;
  117.                 }
  118.             if(cont > size) {
  119.                 return false;
  120.             } else {
  121.                 return true;
  122.               }
  123.             }
  124.         }
  125.     }
  126.     T retiraDaPosicao(int pos) {
  127.      Elemento<T> *auxiliar;
  128.     if (listaVazia()) {
  129.         throw 0;
  130.     } else {
  131.         if (pos > size || pos < 0) {
  132.         throw 0;
  133.     }  else {
  134.         if (pos == 0) {
  135.             return retiraDoInicio();
  136.         } else {
  137.             Elemento<T> *anterior = head;
  138.             for (int n = 0; n < pos-2; n++) {
  139.                 anterior = anterior->getProximo();
  140.             }
  141.             auxiliar = anterior->getProximo();
  142.             anterior->setProximo(auxiliar->getProximo());
  143.             size--;
  144.             return auxiliar->getInfo();
  145.         }
  146.     }}
  147.     }
  148.     // fim
  149.     void adiciona(const T& dado) {
  150.      adicionaNaPosicao(dado, size);
  151.     }
  152.     T retira() {
  153.         return retiraDaPosicao(size);
  154.     }
  155.     // especifico
  156.     T retiraEspecifico(const T& dado) {
  157.     return retiraDaPosicao(posicao(dado)+1);
  158.     }
  159.     void adicionaEmOrdem(const T& data) {
  160.     if (listaVazia()) {
  161.         adicionaNoInicio(data);
  162.     } else {
  163.         Elemento<T> *auxiliar = head;
  164.         int cont = 0;
  165.             while(auxiliar->getInfo()< data && cont <= size) {
  166.                 auxiliar = auxiliar->getProximo();
  167.                 cont++;
  168.             }
  169.             if (cont <= size) {
  170.                 adicionaNaPosicao(data, cont);
  171.             } else {
  172.                 adiciona(data);
  173.             }
  174.         }
  175.     }
  176.     bool listaVazia() const {
  177.         return(size == 0);
  178.     }
  179.     bool igual(T dado1, T dado2) {
  180.         return(dado1 == dado2);
  181.     }
  182.     bool maior(T dado1, T dado2) {
  183.         return(dado1 > dado2);
  184.     }
  185.     bool menor(T dado1, T dado2) {
  186.         return(dado1 < dado2);
  187.     }
  188.     void destroiLista() {
  189.     if (listaVazia()) {
  190.         throw 0;
  191.     } else {
  192.         Elemento<T> *atual = new Elemento<T>(0, head->getProximo());
  193.         Elemento<T> *anterior = new Elemento<T>(0, head);
  194.             if(atual == NULL || anterior == NULL) {
  195.             throw 0;
  196.             } else {
  197.              int cont = 0;
  198.                 while(cont <= size) {
  199.                  anterior->~Elemento();
  200.                  anterior = atual;
  201.                  atual = atual->getProximo();
  202.                  cont++;
  203.              }
  204.                 size = 0;
  205.             }
  206.             }
  207.     }
  208. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement