RADIX Ordina l'assegnazione C ++
-
12-12-2019 - |
Domanda
Ho un problema con il mio compito a casa e io non so dove sono andato storto con esso.Devo progettare una funzione per radix ordinabile con un bucket e un giro k.Ho bisogno di preservare la sequenza degli elementi dell'elenco nel secchio e quindi ho bisogno di mantenere due punti per ogni secchio - anteriore e posteriore. Tuttavia, quando compilo il codice e eseguo il mio codice di prova con 10 numeri che devono essere ordinati, la mia output contiene solo 3 numeri.Se i suoi 20 numeri, stampa solo 2. Puoi aiutarmi per favore?Questo è il mio codice e grazie per il tuo tempo.Modifica: di Leastsigdig intendo una cifra significativa Devo cambiarlo perché è un brutto nome
#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);
}
. Soluzione
C'è almeno un problema qui:
//Linking
int y= 0;
for (int y; y< a-1; y++){
.
La variabile y
nel loop for
Shadows The y
nella portata esterna.Ciò significa che il y
all'interno del loop è non ètilizzato e tu sei fuori nelle erbacce.
Dovresti alzare il livello di avviso del compilatore e prestare attenzione a ciò che dice.