En contraimpares tengo el error no me da el total de los números impares que tengo
Código
#include "stdafx.h" #include <iostream> using namespace std; struct nodo { int num; nodo *siguiente; }; typedef nodo *LISTA; LISTA n = NULL; void insertar(LISTA &puntero, int numero) { LISTA puntero1,puntero2; puntero1 = puntero; if (puntero1==NULL) { puntero1 = new nodo; puntero1->num = numero; puntero1->siguiente=NULL; puntero = puntero1; } else { while (puntero1->siguiente!=NULL) { puntero1 = puntero1->siguiente; } puntero2 = new nodo; puntero2->num = numero; puntero2->siguiente = NULL; puntero1->siguiente = puntero2; } } void mostrar_lista(LISTA puntero) { if (puntero == NULL) { cout << "La lista esta vacia"; } while (puntero!=NULL) { cout << puntero->num << endl; puntero = puntero->siguiente; } } // aqui ya no pude solucionarlo int contraimpares(LISTA &puntero) { int suma=0; if (puntero == NULL) { cout << "La lista esta vacia"; } while (puntero->num % 2 != 0) { puntero = puntero->siguiente; suma++; } return suma; } void main() { int num_elem,numero,i; cout << "Dame el numero de elementos de la lista" << endl; cin >> num_elem; for (i = 0; i < num_elem; i++) { cout << "Dame el numero: " << endl; cin >> numero; insertar(n,numero); } cout << "Lista capturada" << endl; mostrar_lista(n); cout << "Los numeros impares en la lista son: " << contraimpares(n) << endl; system("pause"); }
· Los códigos deben ir en etiquetas GeSHi
>aquí las reglas del foro
-Engel Lex