문제

숙제에 문제가 있는데 어디서 문제가 발생했는지 모르겠습니다.a 버킷과 k 라운드를 사용하여 기수 정렬 기능을 디자인해야 합니다.버킷에 있는 목록 항목의 순서를 유지해야 하므로 각 버킷에 대해 앞과 뒤의 두 지점을 유지해야 합니다.그러나 코드를 컴파일하고 정렬해야 하는 10개의 숫자가 포함된 테스트 코드를 실행하면 출력에는 3개의 숫자만 포함됩니다.숫자가 20개이면 2개만 인쇄됩니다.도와 줄수있으세요?이것은 제 코드입니다. 시간을 내주셔서 감사합니다.편집하다:LeastSigDig는 유효 숫자를 의미합니다. 이름이 좋지 않기 때문에 변경해야 합니다.

#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);
}
도움이 되었습니까?

해결책

여기에는 적어도 하나의 문제가 있습니다.

//Linking
int y= 0;

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

변수 y 에서 for 고리 그림자 그만큼 y 외부 범위에서.이는 다음을 의미합니다. y 루프 내부는 초기화되지 않았으며 잡초에 빠졌습니다.

컴파일러 경고 수준을 높이고 그 내용에 주의를 기울여야 합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top