Radix排序c ++实现

时间:2014-02-05 06:48:58

标签: c++ pointers radix-sort

我无法尝试实施基数排序。这是一项任务,我的教授甚至不愿意指出我正确的方向。我一直在盯着这段代码。有人可以向我解释为什么我的指针不起作用吗?我做了一些检查,看到我的指针甚至没有指向下一个指针,我很困惑为什么。我不是要求任何人做我的任务,只是某个方向。

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define COLUMN 10

using namespace std;


struct List{
    int data;
    List *p;
};
struct node{
    int len = 0;
    List *p = NULL;
};

void printList(int list[], int len){
    for (int i = 0;i<len;i++){
         cout <<list[i] << " ";
    }
    cout << "\n";
}

void deleteList(node columns[], int len){
    for (int j =0; j<COLUMN;j++){
        List *curr;
        List *prev;
        curr = columns[j].p;
        for (int i = 0; i<len;i++){
            prev = curr;
            curr = curr->p;
            delete prev;
        }
        delete curr;
    }
}

void overrideArray(node columns[], int a[], int len){
    int k = 0; List *curr;
    for (int i = 0;i<COLUMN,k<len;i++){
        int len = columns[i].len;
        cout << len << " length"<< endl;
        curr =columns[i].p;
        for (int j = 0;j<len;j++){
            cout << "plue\n";
            if (len == 0) continue;
            cout << k <<" plue2\n";
            a[k]=curr->data;
            cout << "plue3\n";
            cout << a[k] << " value";
            curr = curr->p;
            k++;
        }
    }
    printList(a,len);
}
int main(){
    srand(time(NULL));
    int length = 10, m,n,i;
    node *columns = new node[COLUMN];
    List * curr;
    int *a = new int [length];
    for (int i = 0;i<length;i++){
        a[i] = 2;
    }
    printList(a,length);
    m = 10; n = 1; i =0;
    while(columns[0].len!=length){
        int loc = (a[i]%m)/n;
        //cout << loc << endl;
        curr = columns[loc].p;
        while(curr != NULL){
            //cout << "here" << endl;
            curr = curr->p;
            //cout << curr->data << endl;
        }
        curr = new List;
        cout << curr->p << endl;
        curr->data = a[i];
        cout << curr->p << endl;
        columns[loc].len++;
        i++;
        if (i == length-1){
            overrideArray(columns,a,length);
            cout << "here5\n";
            deleteList(columns,length);
            cout << "here6\n";
            i = 0;
            m *= 10;
            n*=10;
        }

}
overrideArray(columns,a,length);
printList(a,length);
return 0;
}

0 个答案:

没有答案