Vesker

Lista Encadeada

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