Error sorting with dynamic allocation

Asked

Viewed 102 times

0

I’m doing a C program on dynamic allocation and I’m having doubts on the ordering part.

I made the code, and it even runs, but when I type the option ordena the screen just flashes, and doesn’t accomplish the process... What I’m 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");
}

1 answer

0

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

q should be increased by cadastro and decreasing in exclui.

  • Right, but if you don’t use q, which variable will I put?

  • According to your code, q is the amount of items in the list, only increment it when adding an item in the list (q++).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.