基数排序C++赋值
Radix Sort C++ Assignment
我的家庭作业有问题,我不知道哪里出了问题。我必须设计一个用a桶和k轮进行基数排序的函数。我需要保留bucket中列表项的顺序,因此我需要为每个bucket保留两个点——前后。然而,当我编译代码并用10个需要排序的数字运行测试代码时,我的输出只包含3个数字。如果是20个数字,则只打印2个。你能帮我吗?这是我的代码,感谢您抽出时间。编辑:至少SigDig我的意思是有效数字,我必须改变它,因为它是一个坏名字
#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 radixsortn" <<endl ;
while(temp!=NULL)
{
cout << temp->value << "t";
temp = temp->next;
}
node = radixsort(node,a,k);
listnode *check = node;
cout << "nnList after radixsort nn";
while (check)
{
cout << check->value << "t";
check = check->next;
}
cout << "nn";
exit(0);
}
这里至少有一个问题:
//Linking
int y= 0;
for (int y; y< a-1; y++){
for
循环中的变量y
阴影外部范围中的y
。这意味着循环中的y
未初始化,您将陷入困境。
您应该打开编译器警告级别,并注意它所说的内容。
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 用C++中的sscanf赋值
- 为std::string的某个索引赋值
- 重载Singly Linked List中的赋值运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- gtest_使用setargpointee在函数中赋值
- 非常量变量只读位置的赋值
- 使用赋值运算符重载从类中返回jobject
- C++数据文件、数组和计算赋值
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 标准库类型的赋值运算符的引用限定符
- 关于 c++ 函数中指针赋值的简单问题
- 复制构造函数、赋值运算符C++
- 标准::变体的赋值运算符
- cin >> int 给定一个字符串将 int 赋值为 0
- if 子句中的赋值不起作用
- 复制包含C++所有元素的对象!(构造函数和赋值,最佳实践?