Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"Elemento.hpp"
- // "Copyright [2015] <Salvatore Amore>"
- template<typename T>
- class ListaEnc {
- private:
- Elemento<T> *head;
- int size; public:
- ListaEnc() {
- size = 0;
- head = NULL;
- }
- ~ListaEnc() {
- size = 0;
- delete head;
- }
- // inicio
- void adicionaNoInicio(const T& dado) {
- Elemento<T> *add = new Elemento<T>(dado, head);
- if (add == NULL) {
- throw 1;
- } else {
- head = add;
- size++;
- }
- }
- T retiraDoInicio() {
- Elemento<T> *reti = head;
- if (reti == NULL) {
- throw 0;
- } else {
- if (listaVazia()) {
- throw 1;
- } else {
- if (size == 1) {
- throw 0;
- } else {
- head->setProximo(head->getProximo()->getProximo());
- size--;
- }
- }
- return reti->getInfo();
- }
- }
- void eliminaDoInicio() {
- if (listaVazia()) {
- throw 1;
- }
- Elemento<T>* saiu = head;
- head = saiu->getProximo();
- size--;
- delete saiu;
- }
- // posicao
- void adicionaNaPosicao(const T& dado, int pos) {
- Elemento<T> *add = new Elemento<T>(dado, head);
- Elemento<T> *temp;
- if (pos > size || pos < 0) {
- throw 1;
- } else {
- if (add == NULL) {
- throw 1;
- } else {
- if (pos == 0) {
- adicionaNoInicio(dado);
- } else {
- temp = head;
- int ctrl = 0;
- while (ctrl < pos - 1) {
- temp = temp->getProximo();
- ctrl++;
- }
- add->setProximo(temp->getProximo());
- temp->setProximo(add);
- size++;
- }
- }
- }
- }
- int posicao(const T& dado) const {
- Elemento<T> *anterior = head;
- int i = 0;
- int ctrl = 0;
- while (ctrl < size) {
- if (anterior->getInfo() == dado) {
- ctrl = size;
- } else {
- anterior = anterior->getProximo();
- i++;
- ctrl++;
- }
- }
- if (i == size) {
- throw 1;
- } else {
- return i;
- }
- }
- T* posicaoMem(const T& dado) const {
- }
- bool contem(const T& dado) {
- Elemento<T> *temp = new Elemento<T>(dado, head);
- int ctrl = 1;
- if (listaVazia()) {
- throw 2;
- } else {
- if (temp == NULL) {
- throw 1;
- } else {
- while (ctrl <= size
- && temp->getInfo() != temp->getProximo()->getInfo()) {
- temp->setProximo(temp->getProximo());
- ctrl++;
- }
- }
- }
- if (ctrl > size) {
- return false;
- } else {
- return true;
- }
- }
- T retiraDaPosicao(int pos) {
- Elemento<T> *temporario;
- Elemento<T> *anterior = head;
- int ctrl = 0;
- if (listaVazia()) {
- throw 1;
- } else {
- if (pos > size || pos < 0) {
- throw 2;
- } else {
- if (pos == 0) {
- return retiraDoInicio();
- } else {
- while (ctrl < pos - 2) {
- anterior = anterior->getProximo();
- ctrl++;
- }
- temporario = anterior->getProximo();
- anterior->setProximo(temporario->getProximo());
- size--;
- }
- }
- }
- return temporario->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) {
- Elemento<T> *temp = head;
- int ctrl = 0;
- if (listaVazia()) {
- adicionaNoInicio(data);
- } else {
- while (temp->getInfo() < data && ctrl <= size) {
- temp = temp->getProximo();
- ctrl++;
- }
- if (ctrl <= size) {
- adicionaNaPosicao(data, ctrl);
- } else {
- adiciona(data);
- }
- }
- }
- bool listaSohUm() const {
- if(size == 1){
- return true;
- } else {
- return false;
- }
- }
- bool listaVazia() const {
- if (size == 0) {
- return true;
- } else {
- return false;
- }
- }
- 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() {
- Elemento<T>* atual = head;
- while (atual != NULL) {
- atual = atual->getProximo();
- eliminaDoInicio();
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment