Last active
June 20, 2023 23:54
-
-
Save codepainkiller/3cc906797788cb10ab17 to your computer and use it in GitHub Desktop.
Ordenamiento Burbuja con listas enlazadas - C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* C++ - Ordenar numeros en una lista enlazada simple | |
* | |
* Copyright 2014 Martin Cruz Otiniano | |
* | |
* Description: Ordena mediante el criterio de burbuja una lista | |
* | |
* Site: martincruz.me | |
*/ | |
#include <iostream> | |
#include <stdlib.h> | |
using namespace std; | |
struct nodo{ | |
int nro; // en este caso es un numero entero | |
struct nodo *sgte; | |
}; | |
typedef struct nodo *Tlista; | |
Tlista inicio, fin; | |
void generarLista( Tlista &inicio, Tlista &fin, int n ) | |
{ | |
Tlista q, t; | |
for(int i=0; i<n; i++) | |
{ | |
q = new(struct nodo); | |
q->nro = rand()%51; | |
if(inicio==NULL) | |
{ | |
q->sgte = inicio; | |
inicio = q; | |
fin = q; | |
} | |
else | |
{ | |
q->sgte = fin->sgte; | |
fin->sgte = q; | |
fin = q; | |
} | |
} | |
cout<<"\n\n\tLista de numeros generados... "<<endl; | |
} | |
void reportarLista(Tlista inicio) | |
{ | |
while(inicio != NULL) | |
{ | |
cout <<" " << inicio->nro ; | |
inicio = inicio->sgte; | |
} | |
} | |
void ordenarLista(Tlista lista) | |
{ | |
Tlista actual , siguiente; | |
int t; | |
actual = lista; | |
while(actual->sgte != NULL) | |
{ | |
siguiente = actual->sgte; | |
while(siguiente!=NULL) | |
{ | |
if(actual->nro > siguiente->nro) | |
{ | |
t = siguiente->nro; | |
siguiente->nro = actual->nro; | |
actual->nro = t; | |
} | |
siguiente = siguiente->sgte; | |
} | |
actual = actual->sgte; | |
siguiente = actual->sgte; | |
} | |
cout<<"\n\n\tLista ordenada..."<<endl; | |
} | |
void menu() | |
{ | |
cout<<"\n\t\tORDENAMIENTO DE UNA LISTA ENLAZADA SIMPLE\n\n"; | |
cout<<" 1. GENERAR NUMEROS "<<endl; | |
cout<<" 2. MOSTRAR NUMEROS "<<endl; | |
cout<<" 3. ORDENAR NUMEROS "<<endl; | |
cout<<" 4. SALIR "<<endl; | |
cout<<"\n INGRESE OPCION: "; | |
} | |
/* Funcion Principal | |
------------------------------------------------------------------*/ | |
int main() | |
{ | |
inicio = NULL; | |
fin = NULL; | |
int op; // opcion del menu | |
int num; // elemenento a ingresar | |
system("color 0b"); | |
do | |
{ | |
menu(); cin>> op; | |
switch(op) | |
{ | |
case 1: | |
cout<< "\n Cantidad de numeros: "; cin>> num; | |
generarLista( inicio, fin, num ); | |
break; | |
case 2: | |
cout<<"\n\n LISTA:\n\n"; | |
reportarLista( inicio ); | |
break; | |
case 3: | |
ordenarLista( inicio ); | |
break; | |
} | |
cout<<endl<<endl; | |
system("pause"); system("cls"); | |
}while(op!=4); | |
system("pause"); | |
return 0; | |
} |
Gracias por el aporte
@Nico1996 Podrías usar una lista doblemente enlazada para ordenar por número o por caracter, por ejemplo:
struct nodo { int dato; char caracter: struct nodo *sigNumero; struct nodo *sigCaracter; };
Sólo tendrías que usar doble apuntador para los dos inicios que tendrás (uno para ordenar por numero y otro para ordenar por caracter).
Excelente aporte, nada mas que creo que la linea 84 es innecesaria, ya que haces la asignación otra vez en la 71, Gracias
Gracias amigo, me salvaste un par de veces.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola tengo una duda, como hacer si por ejemplo, los nodos estan compuestos por un caracter y un entero (int a,char b por ejemplo) y queremos ordenar la lista de mayor a menor pero segun los numeros (por ejemplo el nodo A (tiene 5 y b) el nodo B (tene un 100 y un letra z) el nodo C (tiene un 101 y una letra x). como quedaria ordenado seria C-B-A.