Vesker

bg

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