Radix сортировка с использованием структуры очереди - PullRequest
0 голосов
/ 23 марта 2020

Я пытаюсь реализовать сортировку по основанию, используя структуру очереди, но она выдает ошибку. Может ли кто-нибудь помочь мне с этим, пожалуйста. спасибо

с использованием очереди библиотеки c ++, программа работает отлично, поэтому что-то не так с моей структурой очереди или с тем, как я инициализирую массив из 10 очередей или, может быть, с помощью pointers.btw я новичок , спасибо

#include<iostream>
#include<cstdlib>
#include<ctime>
//#include<queue>
#include<cmath>
using namespace std;

struct node{
int value;
node* next;
};

struct que{
node* frontt;
node* rear;
};

void enqueue(que* &queu,int value)
{
    node* newNode = new node;
    newNode->value = value;
    newNode->next = nullptr;
    if(queu->rear !=nullptr)
    {
        queu->rear->next = newNode;
        queu->rear = newNode;
    } else{
        queu->frontt = queu->rear = newNode;
    }
}

int dequeue(que* &queu)
{
    node* temp = new node;
    int returnValue;

    if(queu->frontt){
        temp = queu->frontt;
        returnValue = temp->value;
        queu->frontt = queu->frontt->next;
        delete temp;
        if(!queu->frontt){
            queu->rear = nullptr;
        }
    }
    return returnValue;
}
void radixSort(int q[], int n);

int main() {

const int MAX = 9;
int radix[MAX] = {2, 543, 23, 123, 900, 4, 3, 2, 223};

for (int j = 0; j < MAX; j++)
      cout << radix[j] << " ";
cout << endl << endl;
radixSort(radix, MAX);
for (int j = 0; j < MAX; j++)
      cout << radix[j] << " ";
cout << endl << endl;
return 0;
}

void radixSort(int arr[], int n) {

    que* arrayOfQues[10];
    //queue<int> arrayOfQues[10]; //one array per possible digit
    int maxDigits = 3; //holds amount of digits in largest number
    int currentDigit = 0; //starting base for decimal digit
    while (currentDigit < maxDigits) {
        for(int i = 0; i < n; i++){ //loop through whole array
            int divisor = pow(10, currentDigit);
            int num = arr[i]; //set to current value of array position
            int digitValue = static_cast<int>((num / divisor) % 10); //get the decimal digit at current digit
            enqueue(arrayOfQues[digitValue],num);
            //arrayOfQues[digitValue].push(num); //put digits in corresponding arrayOfQues
        }
        int i = 0;
        for(int k = 0; k < 10; k++){ //loop through all arrayOfQues
            while(arrayOfQues[k]->frontt != nullptr){
            //while(!arrayOfQues[k].empty()){ //push all elements in bin[k] until empty to a
                /*int temp = arrayOfQues[k].front();
                arr[i] = temp;
                arrayOfQues[k].pop();
                i++;*/
              arr[i] = dequeue(arrayOfQues[k]);
            }
        }
        currentDigit++;
    }
}
...