Error with sort order with dynamic allocation

0

I'm doing a C program in dynamic allocation and I'm having some questions at the sorting part.

I did the code, and it even runs, but when I type the option ordena the screen just blinks, and does not perform the process ... What am I doing wrong?

#include <stdlib.h>

#include <stdio.h>
#include <string.h>

struct pasta {
       char nome[80];
       int codigo;
       struct pasta *proximo;
       struct pasta *anterior;
};

struct pasta *inicio, *ultimo, dados[1000];
int q=0;
int menu();

void listaligada (struct pasta *);
void cadastro();
void ordena();
void grava();
void abre();
void exclui();
void altera();
void lista();

main()
{
      int i;
      inicio = ultimo = NULL;

      for (;;){
          i=menu();
          switch(i) {
                    case 1: cadastro();
                    break;
                    case 2: grava(); break;
                    case 3: abre(); break;
                    case 4: ordena(); break;
                    case 5: lista(); break;
                    case 6: exclui(); break;
                    case 7: altera(); break;
                    case 8: exit(1); break;
                    }
          }
}

int menu()
{
    int i;
    system("cls");
    printf("\t\tMenu\n\n");
    printf("\t1. Cadastro\n");
    printf("\t2. Grava\n");
    printf("\t3. Abre\n");
    printf("\t4. Ordena\n");
    printf("\t5. Lista\n");
    printf("\t6. Exclui\n");
    printf("\t7. Altera\n");
    printf("\t8. Sai\n");
    printf("\nEntre com a opcao: ");
    scanf("%d",&i);

    return i;
}

void listaligada (struct pasta *p) {
     if (inicio == NULL) { 
                inicio = ultimo = p;
                p->proximo = NULL;  
                p->anterior = NULL;
                return; 
     }
     ultimo->proximo = p; 
     p->anterior = ultimo; 
     p->proximo = NULL;
     ultimo = p; 


}

void cadastro() { 
    struct pasta *p;
     static int i=0;
     system("cls");
     printf("\t\tCadastro\n\n");
     printf("\tEntre com os dados. \nTecle apenas ENTER sobre o campo 'nome' sem digitar nada para sair:");

     for(;;) {
             printf("\nNome %d:", i+1);
             p = (struct pasta *) malloc (sizeof(struct pasta)); 
             fflush(stdin);
             gets(p->nome);
             if(!p->nome[0])
                            break;
             p->codigo = ++i; 
             listaligada(p);
         }
}

void ordena() {
    int i,j;
    struct pasta temp;
    system ("cls");
    printf("Nomes ordenados:\n\n");
    for(i=0 ; i<q ; i++)
        for(j=i+1 ; j<q ; j++)

             if (dados[i].nome>dados[j].nome)
             {
             temp=dados[i];
             dados[i]=dados[j];
             dados[j]=temp;
             }

}
void grava() {}
void abre() {}
void exclui() {}
void altera() {}
void lista() {
     struct pasta *p;
     system ("cls");
     printf("\n\t\tLista\n\n");
     p = inicio;
     while (p!= NULL) {
           printf("\n%s", p->nome);
           printf("\nCodigo: %d", p->codigo);           
           p = p->proximo;
           printf("\n---------\n");       
     }
     system("pause");
}
    
asked by anonymous 16.11.2015 / 15:28

1 answer

0

In ordena , you use for(i=0 ; i<q ; i++) but you never increase q .

q should be incremented by cadastro and decremented by exclui .

    
16.11.2015 / 16:50