Skip to content

Instantly share code, notes, and snippets.

@codepainkiller
Last active June 20, 2023 23:54
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codepainkiller/3cc906797788cb10ab17 to your computer and use it in GitHub Desktop.
Save codepainkiller/3cc906797788cb10ab17 to your computer and use it in GitHub Desktop.
Ordenamiento Burbuja con listas enlazadas - C++
/*
* 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;
}
@nicogrep
Copy link

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.

@EdgardoDth
Copy link

Gracias por el aporte

@PerviousNebula
Copy link

@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).

@FernandoEsquivelArce
Copy link

Excelente aporte, nada mas que creo que la linea 84 es innecesaria, ya que haces la asignación otra vez en la 71, Gracias

@jsotoca
Copy link

jsotoca commented Sep 4, 2020

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