Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/02/2010, 16:29
devshared
 
Fecha de Ingreso: enero-2010
Mensajes: 25
Antigüedad: 14 años, 4 meses
Puntos: 0
Respuesta: Ordenación de datos vinculados

Código C:
Ver original
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct ciclista{
  5.         char nombre[30];
  6.         float tiempo;
  7. }Ciclista;
  8.  
  9. Ciclista *ciclistas; //lista de ciclistas
  10. int n; //longitud de ciclistas
  11.  
  12. void ordenar(){
  13.     int i, j, salto;
  14.     Ciclista tmp;
  15.     for(i = 0; i < n; i++){
  16.         for(j = 0; j < n - i; j++){
  17.             if(ciclistas[j].tiempo > ciclistas[j + 1].tiempo){
  18.                 tmp = ciclistas[j];
  19.                 ciclistas[j] = ciclistas[j+1];
  20.                 ciclistas[j+1] = tmp;
  21.             }
  22.         }
  23.     }                      
  24. }
  25.  
  26. int main(){
  27.     int i;
  28.     printf("\nIngrese la cantidad de ciclistas: ");
  29.     scanf("%d", &n);
  30.     while(n < 0){
  31.         printf("\nIngrese una cantidad valida.");
  32.         printf("\nIngrese la cantidad de ciclistas: ");
  33.         scanf("%d", &n);    
  34.    }
  35.    ciclistas = (Ciclista *)malloc(sizeof(Ciclista) * n);
  36.    printf("\nCarga los datos de los ciclistas");
  37.    for(i = 0; i < n; i++){
  38.        printf("\nIngrese nombre: ");
  39.        scanf("\n%[^\n]s", ciclistas[i].nombre);
  40.        printf("\nIngrese tiempo: ");
  41.        scanf("%f", &ciclistas[i].tiempo);
  42.    }
  43.    ordenar();
  44.    for(i = 0; i < n; i++){
  45.        printf("\n%d- %s %.2f", i, ciclistas[i].nombre, ciclistas[i].tiempo);
  46.    }
  47.    printf("\n");
  48.    system("PAUSE");
  49.    return 0;
  50. }