Pergunta

Tenho um problema com meu dever de casa e não sei onde errei.Eu tenho que projetar uma função para classificação de raiz com baldes e k rodadas.Preciso preservar a sequência dos itens da lista no balde e, portanto, preciso manter dois pontos para cada balde - na frente e atrás.No entanto, quando compilo meu código e executo meu código de teste com 10 números que precisam ser classificados, minha saída contém apenas 3 números.Se forem 20 números, imprimirá apenas 2.Você pode me ajudar por favor?Este é o meu código e obrigado pelo seu tempo.Editar:por pelo menosSigDig quero dizer Dígito Significativo, tenho que mudar isso porque é um nome ruim

#include <cstdlib> // Provides size_t and NULL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

struct listnode { struct listnode * next;
                  unsigned long     value; } ;

struct listnode *radixsort (struct listnode *data, int a, int k){
    struct listnode *front [a], *rear [a], *cursor;
    int i=0 , j = 0, leastSigDig,base10num;
    if (data == NULL) {return data;}

    for (i;i<k;i++){
        base10num= pow(a,i);
        cursor = data;
        for (j=0; j<a; j++){
            front [j] = NULL;
            rear [j] = NULL;
        }
        while (cursor != NULL){
            leastSigDig = ((cursor->value)/base10num)%a;
            if (rear [leastSigDig]!= NULL){
                rear[leastSigDig]->next= cursor;
                rear [leastSigDig]= cursor;
            }
            else if (cursor == NULL) {
                rear [leastSigDig] = cursor;
            }
            cursor = cursor->next;
        }

        //Linking
            cursor = NULL;
for (int y=0; y< a-1; y++){
    int z= y+1;
    if (front [y] == NULL)
        continue;
    else if (cursor == NULL){
            cursor = front [y];
            rear [y]->next = front [z];
        }
    else if (cursor != NULL) 
            rear [y]->next = front [z];


    data = cursor;
    }
        }
    }
    return data;
}   


int main(void)
{  
    long i, length=10;
    long a = 10; // working with base 10
    long k = log10(length*a);
    struct listnode *node, *space;
    space =  (struct listnode *) malloc(length*sizeof(struct listnode));
    for( i=0; i< length; i++ ) {
        (space + i)->value = 2*((17*i)%length);
        (space + i)->next = space + (i+1);
    }
    (space+(length-1))->next = NULL;
    node = space;
    struct listnode * temp =node;
    cout<<endl<<"List before radixsort\n" <<endl ;
    while(temp!=NULL)
    {
        cout << temp->value << "\t";
        temp = temp->next;
    }

    node = radixsort(node,a,k);

    listnode *check = node;
    cout << "\n\nList after radixsort \n\n";
    while (check)
    {
        cout << check->value << "\t";
        check = check->next;
    }
    cout << "\n\n";
    exit(0);
}
Foi útil?

Solução

Há pelo menos um problema aqui:

//Linking
int y= 0;

for (int y; y< a-1; y++){

A variável y no for laço sombras o y no âmbito externo.Isto significa que y dentro do loop não foi inicializado e você está no meio do mato.

Você deve aumentar o nível de aviso do compilador e prestar atenção ao que ele diz.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top