ずっといろいろなサイト様を参考にしたり、参考書など目を通しましたがしっくりきません。
バケットの基数ソートなんですが、
スタックからキューへ直したいです。
どこをいじればいいかと。
1から別のものでならスタック操作のものキュー操作のものそれぞれ作れたのですがこのプログラムだと上手く完成までもっていけません。
よろしくお願いします。
#include <stdio.h>
#include <stdlib.h>
#define N 100 /* 配列Aの最大サイズ */
#define m 10 /* 値の範囲は[0,1,...,m-1] */
#define K 3 /* 語長 */
struct word /* 構造体wordの宣言 */
{
int letter[K];
};
struct cell /* 構造体cellの宣言 */
{
int index;
struct cell *next;
};
/* 関数の宣言 */
void radixsort(struct word *A, int *idx, int n);
void bucketsort(struct word *A, int *idx, int n, int k);
void insert(struct word *A, int idx, struct cell **B, int k);
int main()
{
struct word A[N];
A[0].letter[0]=2; A[0].letter[1]= 2; A[0].letter[2]= 1;
A[1].letter[0]=6; A[1].letter[1]= 5; A[1].letter[2]= 0;
A[2].letter[0]=0; A[2].letter[1]= 2; A[2].letter[2]= 3;
A[3].letter[0]=0; A[3].letter[1]= 0; A[3].letter[2]= 2;
A[4].letter[0]=1; A[4].letter[1]= 0; A[4].letter[2]= 6;
A[5].letter[0]=2; A[5].letter[1]= 2; A[5].letter[2]= 6;
A[6].letter[0]=2; A[6].letter[1]= 5; A[6].letter[2]= 0;
A[7].letter[0]=1; A[7].letter[1]= 2; A[7].letter[2]= 6;
A[8].letter[0]=3; A[8].letter[1]= 7; A[8].letter[2]= 2;
A[9].letter[0]=0; A[9].letter[1]= 4; A[9].letter[2]= 7;
A[10].letter[0]=2; A[10].letter[1]= 1; A[10].letter[2]= 5;
A[11].letter[0]=0; A[11].letter[1]= 3; A[11].letter[2]= 3;
int idx[N];
int i, h, n;
n=12;
for(i=0; i<n; i++) idx[i]=i;
radixsort(A, idx, n);
return(0);
}
void radixsort (struct word *A, int *idx, int n)
{
int k;
for(k=K-1; k>=0; k--) bucketsort(A, idx, n, k);
return;
}
void bucketsort(struct word *A, int *idx, int n, int k)
{
struct cell *B[m];
struct cell *p, *q;
int i, j;
for(j=0; j<m; j++) B[j]=NULL;
for(i=0; i<n; i++) /* 文(1) */
insert(A, idx[i], B, k); /* 文(2) */
i=n-1;
for(j=m-1; j>=0; j--)
{
p=B[j];
while(p != NULL)
{idx[i] = p->index; i=i-1;
q = p->next; free(p); p=q;}
}
return;
}
void insert(struct word *A, int ix, struct cell **B, int k)
{
struct cell *p;
p=(struct cell *)malloc(sizeof(struct cell));
p->index = ix;
p->next = B[A[ix].letter[k]];
B[A[ix].letter[k]] = p;
return;
}