Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Receba 10 numeros , remova somente os numeros pares e imprimir de trás para frente " */
- #include <iostream>
- using namespace std;
- #define tam 10
- struct no
- {
- int info;
- struct no*prox;
- };
- typedef struct no*noPtr;
- // funcoes
- bool listaVazia(noPtr p);
- int inserir(noPtr *start, int x );
- void listar(noPtr start);
- void retirar(noPtr *start, int x);
- void listarPares(noPtr *start);
- int menu();
- // fim do espaco
- int main(int argc, char** argv)
- {
- int op;
- noPtr inicio = NULL;
- int x;
- int qtde = 0;
- do
- {
- op = menu();
- switch(op)
- {
- case 1:
- cout << "Digite um numero para inserir " << endl;
- cin >> x;
- qtde += inserir(&inicio, x);
- case 2:
- cout << "Digite um elemento que queira remover " << endl;
- cin >> x;
- retirar(&inicio, x);
- cout << "Elemento removido " << endl;
- break;
- case 3:
- listar(inicio);
- break;
- case 4:
- listarPares(&inicio);
- case 0:
- break;
- }
- }
- while(op != 0);
- return 0;
- }
- bool listaVazia(noPtr p)
- {
- if(p == NULL)
- return true;
- else
- {
- return false;
- }
- }
- int inserir(noPtr *start, int x )
- {
- noPtr p = new no;
- p->info = x;
- if(listaVazia(*start))
- {
- *start = p;
- p->prox = NULL;
- }
- else
- {
- noPtr aux = *start;
- *start = p;
- p->prox = aux;
- }
- cout << "Elemento adicionado " << endl;
- return 1;
- }
- void listar(noPtr start)
- {
- noPtr p = start;
- while(p != NULL)
- {
- cout << p->info << endl;
- p = p->prox;
- }
- }
- void listarPares(noPtr *start)
- {
- int contador = 0;
- noPtr p = *start;
- while(p != NULL)
- {
- if((p->info % 2) == 0)
- {
- contador += 1;
- }
- p = p->prox;
- } // Quantos elementos pares tem
- int vetor[contador];
- for(int i = 0; i < tam ; i++)
- {
- if(p->info % 2 == 0)
- {
- vetor[i] = p->info;
- }
- p = p->prox;
- } // Adicionar os valores pares a array
- for(int i = 0; i < tam ; i++)
- {
- if(p->info % 2 == 0)
- {
- retirar(start, vetor[i]);
- }
- p = p->prox;
- } // remover os elementos pares da lista encadeada
- }
- void retirar(noPtr *start, int x)
- {
- noPtr p = *start;
- if(listaVazia(*start))
- {
- cout << "Lista Vazia " << endl;
- }
- else
- {
- while(p != NULL)
- {
- if( p->info == x)
- {
- *start = p->prox;
- delete(p);
- }
- p = p->prox;
- }
- }
- }
- int menu()
- {
- int op;
- cout << "1 INSERIR" << endl;
- cout << "2 REMOVER" << endl;
- cout << "3 LISTAR" << endl;
- cout << "4 LISTAR PARES " << endl;
- cout << "0 SAIR" << endl;
- cin >> op;
- return op; // 1, 2 , 3 , 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement