Skip to content

Instantly share code, notes, and snippets.

@joaquinvelez23
Last active November 18, 2021 01:14
Show Gist options
  • Select an option

  • Save joaquinvelez23/962b2268b11986a10c28616b15f22ac3 to your computer and use it in GitHub Desktop.

Select an option

Save joaquinvelez23/962b2268b11986a10c28616b15f22ac3 to your computer and use it in GitHub Desktop.
Lista simple en C++ con función de agregar lista a ordenada y mostrar.
/*JOAQUIN VELEZ LISTA EN C SIN CLASES*/
#include <iostream>
using namespace std;
struct Nodo{
int dato;
Nodo *siguiente;
};
void insertarLista(Nodo *&lista, int n);
void mostrarLista(Nodo *lista);
int main(){
//creamos nodo
Nodo* lista=NULL;
int nmain, opt;
char caracter;
do{
cout << "MENU" << endl;
cout << "1. Ingresar lista" << endl;
cout << "2. Mostrar lista" << endl;
cin >> opt;
if (opt == 1){
cout << "Ingrese el dato entero: ";
cin >> nmain;
insertarLista(lista, nmain);
}else if (opt == 2){
mostrarLista(lista);
}else{
cout << "Opcion invalida." << endl;
}
system("pause");
system("cls");
cout << "Desea seguir en el programa? (s) o (n) ";
cin.ignore();
cin >> caracter;
}while(caracter == 's' || caracter == 'S');
return 0;
}
void insertarLista(Nodo *&lista, int n){
Nodo *nuevo_nodo = new Nodo();
nuevo_nodo->dato=n;
Nodo *aux1 = lista;
Nodo *aux2; //sirve para el while
nuevo_nodo->siguiente=aux1;
//while que ordena
while ((aux1 != NULL) && (aux1->dato < n)){
aux2 = aux1;
aux1 = aux1->siguiente;
}
if (aux1 == lista){
lista=nuevo_nodo;
}else{
aux2->siguiente=nuevo_nodo;
}
nuevo_nodo->siguiente=aux1;
cout << "Dato " << n << ", ingresado correctamente\n" << endl;
}
void mostrarLista(Nodo *lista){
Nodo *actual = new Nodo();
actual = lista;
cout << "MUESTRA DE DATOS:" << endl;
while(actual != NULL){
cout << ". " << actual->dato << endl;
actual = actual->siguiente;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment