ANTES QUE NADA GRACIAS POR LEERLO.
PROBLEMA:
ME HE ATORADO EN QUE NO SE COMO DIVIDIR MI CODIGO EN FUNCIONES.
NOTA: EN EL SEGUNDO CODIGO INTENTE HACER UN MENU.
CODIGO QUE SI FUNCIONA:
Código
#include<bits/stdc++.h> using namespace std; typedef struct Nodo{ int dato; struct Nodo *siguiente; }nodo; int main(){ cin.tie(0); ios_base::sync_with_stdio(0); system("color 0a"); int numero_De_elementos; cout<<"Introduce el numero de elementos:"<<endl; cin>>numero_De_elementos; if(numero_De_elementos>0){ int dato; nodo *n = NULL; nodo *aux = NULL; nodo *inicio = NULL; /*CREACION DE PRIMER NODO*/ n = (nodo*)malloc(sizeof(nodo)); cout<<"Introduce el nodo"<<endl; cin>>dato; n -> dato = dato; aux = n; inicio = n; n -> siguiente = NULL; /*CREACION DE LOS NODOS RESTANTES*/ for(int i=1; i<numero_De_elementos; i++){ cout<<"Introduce el nodo"<<endl; cin>>dato; n = (nodo*)malloc(sizeof(nodo)); n -> dato = dato; aux -> siguiente = n; aux = aux -> siguiente; n -> siguiente = NULL; } /*RECORRIDO*/ aux = inicio; while(aux != NULL){ cout<<aux->dato<<" -> "; aux = aux ->siguiente; } } return 0; }
CODIGO CON FUNCIONES QUE NO SIRVE:
Código
#include<bits/stdc++.h> using namespace std; struct Nodo{ int dato; struct Nodo *siguiente; }; typedef struct Nodo *Lista; void primer_nodo(nodo n, nodo aux, nodo inicio)){ int valor; cout<<endl<<"INTRODUCE EL VALOR DEL NODO "; cin>>valor; n = (nodo*)malloc(sizeof(nodo)); n -> dato = valor; aux = n; inicio = n; n -> siguiente = NULL; } void agregar_nodo(nodo n, nodo aux){ cout<<endl<<"INTRODUCE EL VALOR DEL NODO "; cin>>valor; n = (nodo*)malloc(sizeof(nodo)); n -> dato = valor; aux -> siguiente = n; aux = n; } void imprimir(nodo aux, nodo inicio){ aux = inicio; while(aux != NULL){ cout<<aux -> dato<<" -> "; aux = aux -> siguiente; } } int main(){ system("color 0a"); int respuesta; int contador=0; nod *n = NULL; nodo *aux = NULL; nodo *inicio = NULL; cout<<"LISTAS ENLAZADAS"<<endl<<endl; cout<<"0) MOSTRAR EL NUMERO DE ELEMENTOS DE LA LISTA"<<endl; cout<<"1) AGREGAR ELEMENTO AL FINAL"<<endl; cout<<"2) ELIMINAR ELEMENTO AL FINAL (AUN NO SIRVE xD)"<<endl; cout<<"3) MOSTAR LISTA"<<endl; cout<<"4) SALIR"<<endl; cout<<endl<<"INTRODUCE EL NUMERO DE LA OPCION DESEADA "; while(respuesta != 4){ cin>>respuesta; if(respuesta == 0) cout<<contador; if(respuesta == 1){ if(contador == 0) primer_nodo(n,aux,inicio); else agregar_nodo(n,aux); } if(respuesta == 2) if(respuesta == 3) imprimir(aux,inicio); } return 0; }