问题描述
在用Bucket sort的时候,试了一下用链表数组的结构。
编译通过了,但是不知道为什么next指针用不了。
代码
1 2 3 4 5 6 7 8
| typedef double ElemType; typedef ElemType* Array; typedef struct CTNode{ ElemType data; struct CTNode *next; }*ChildPtr; typedef ChildPtr CTBox; typedef CTBox* CTree;
|
1 2 3 4 5 6 7 8 9 10
| ChildPtr getRear(CTBox B){ ChildPtr p = B; if(B){ } return p; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| void SortCTNode(CTBox &Box){ ChildPtr p, q, head; head = p = Box; while(head){ head = head->next; q = Box; while(p->data > q->next->data && q->next){ q = q->next; } if(p != q){ p->next = q->next; q->next = p; } p = head; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| void BUCKET_SORT(Array &A, int n){ CTree B = (CTBox*)malloc(n * sizeof(CTBox)); CTBox newBox; ChildPtr newChild, p; for(int i = 0; i < n; i++){ B[i] = NULL; } for(i = 0; i < n; i++){ if(!B[(int)ceil(n * A[i])]){ newBox = (CTNode*)malloc(sizeof(CTNode)); newBox->data = A[i]; newBox->next = NULL; B[(int)ceil(n * A[i])] = newBox; } else{ newChild = (CTNode*)malloc(sizeof(CTNode)); newChild->data = A[i]; newChild->next = NULL; p = getRear(B[(int)ceil(n * A[i])]); p->next = newChild; } } for(i = 0; i < n; i++){ if(B[i]){ SortCTNode(B[i]); } } CTBox Final = (CTNode*)malloc(sizeof(CTNode)); p = Final; for(i = 0; i < n; i++){ if(B[i]){ p->next = B[i]; p = getRear(B[i]); } } p = Final->next; for(i = 0; i < n; i++){ A[i] = p->data; p = p->next; } }
|