Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright [2015] <Salvatore Junior Amore>
- #include "Elemento.hpp"
- template<typename T>
- class ListaEnc {
- private:
- Elemento<T> *head;
- int size;
- public:
- ListaEnc() {
- head = NULL;
- size = 0;
- }
- ~ListaEnc() {
- size = 0;
- delete head;
- }
- // inicio
- void adicionaNoInicio(const T& dado) {
- Elemento<T> *novo = new Elemento<T>(dado, head);
- if (novo == NULL) {
- throw 0;
- } else {
- head = novo;
- size++;
- }
- }
- T retiraDoInicio() {
- if (listaVazia()) {
- throw 0;
- } else {
- Elemento<T> *refRemovida = head;
- if (refRemovida == NULL) {
- throw 0;
- } else {
- head->setProximo(head->getProximo()->getProximo());
- size--;
- return refRemovida->getInfo();
- }
- }
- }
- void eliminaDoInicio() {
- if(listaVazia()) {
- throw 0;
- } else {
- Elemento<T> *novo = new Elemento<T>(NULL, head);
- if(novo == NULL) {
- throw 0;
- } else {
- head->setProximo(novo->getProximo());
- size--;
- novo->~Elemento();
- }
- }
- }
- // posicao
- void adicionaNaPosicao(const T& dado, int pos) {
- if(pos > size|| pos < 0) {
- throw 0;
- } else {
- Elemento<T> *novo = new Elemento<T>(dado, head);
- Elemento<T> *anterior;
- if(novo == NULL) {
- throw 0;
- } else {
- if(pos == 0) {
- adicionaNoInicio(dado);
- } else {
- anterior = head;
- int cont = 0;
- while(cont < pos-1) {
- anterior = anterior->getProximo();
- cont++;
- }
- novo->setProximo(anterior->getProximo());
- anterior->setProximo(novo);
- size++;
- }
- }
- }
- }
- int posicao(const T& dado) const {
- Elemento<T> *novo;
- if(listaVazia()) {
- throw 0;
- } else {
- novo = head;
- int cont = 0;
- while(cont <= size) {
- if(novo->getInfo() == dado) {
- return cont;
- } else {
- novo = novo->getProximo();
- cont++;
- }
- }
- if (cont > size) { throw 0;}
- }
- }
- T* posicaoMem(const T& dado) const {
- }
- bool contem(const T& dado) {
- int cont = 1;
- if(listaVazia()) {
- throw 0;
- } else {
- Elemento<T> *novo = new Elemento<T>(dado, head);
- if(novo == NULL) {
- throw 0;
- } else {
- while (cont <= size &&
- novo->getInfo()!= novo->getProximo()->getInfo()) {
- novo->setProximo(novo->getProximo());
- cont++;
- }
- if(cont > size) {
- return false;
- } else {
- return true;
- }
- }
- }
- }
- T retiraDaPosicao(int pos) {
- Elemento<T> *auxiliar;
- if (listaVazia()) {
- throw 0;
- } else {
- if (pos > size || pos < 0) {
- throw 0;
- } else {
- if (pos == 0) {
- return retiraDoInicio();
- } else {
- Elemento<T> *anterior = head;
- for (int n = 0; n < pos-2; n++) {
- anterior = anterior->getProximo();
- }
- auxiliar = anterior->getProximo();
- anterior->setProximo(auxiliar->getProximo());
- size--;
- return auxiliar->getInfo();
- }
- }}
- }
- // fim
- void adiciona(const T& dado) {
- adicionaNaPosicao(dado, size);
- }
- T retira() {
- return retiraDaPosicao(size);
- }
- // especifico
- T retiraEspecifico(const T& dado) {
- return retiraDaPosicao(posicao(dado)+1);
- }
- void adicionaEmOrdem(const T& data) {
- if (listaVazia()) {
- adicionaNoInicio(data);
- } else {
- Elemento<T> *auxiliar = head;
- int cont = 0;
- while(auxiliar->getInfo()< data && cont <= size) {
- auxiliar = auxiliar->getProximo();
- cont++;
- }
- if (cont <= size) {
- adicionaNaPosicao(data, cont);
- } else {
- adiciona(data);
- }
- }
- }
- bool listaVazia() const {
- return(size == 0);
- }
- bool igual(T dado1, T dado2) {
- return(dado1 == dado2);
- }
- bool maior(T dado1, T dado2) {
- return(dado1 > dado2);
- }
- bool menor(T dado1, T dado2) {
- return(dado1 < dado2);
- }
- void destroiLista() {
- if (listaVazia()) {
- throw 0;
- } else {
- Elemento<T> *atual = new Elemento<T>(0, head->getProximo());
- Elemento<T> *anterior = new Elemento<T>(0, head);
- if(atual == NULL || anterior == NULL) {
- throw 0;
- } else {
- int cont = 0;
- while(cont <= size) {
- anterior->~Elemento();
- anterior = atual;
- atual = atual->getProximo();
- cont++;
- }
- size = 0;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement