Reputation:
i have finished code for bucket sorting,here is full code
#include <iostream>
#include<iomanip>
using namespace std;
#define narray 8// array size;
#define nbucket 5// bucket size;
#define interval 10// bucket range
struct node
{
int data;
struct node *next;
};
void BucketSort(int arr[]);
struct node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
void BucketSort(int arr[])
{
int i,j;
struct node **buckets;
buckets = (struct node **)malloc(sizeof(struct node*) * nbucket);
for (i=0;i<nbucket;i++){
buckets[i]=NULL;
}
for (int i=0;i<narray;i++){
struct node *current;
int pos=getBucketIndex(arr[i]);
current=(struct node *)malloc(sizeof(struct node));
current->data=arr[i];
current->next=buckets[pos];
buckets[pos]=current;
}
for (i=0;i<nbucket;i++){
cout << "Bucket[" << i << "] : ";
printBuckets(buckets[i]);
cout<<endl;
}
for ( i=0;i<nbucket;i++){
buckets[i]=InsertionSort(buckets[i]);
}
for (i=0;i<nbucket;i++){
printBuckets(buckets[i]);
cout<<endl;
}
//put item back to original array
for (j=0,i=0;i<nbucket;i++){
struct node * node;
node=buckets[i];
while(node){
arr[j++]=node->data;
node=node->next;
}
}
//free memory
for (i=0;i<nbucket;i++){
struct node *node;
node=buckets[i];
while(node){
struct node *temp;
temp=node;
node=node->next;
free(temp);
}
}
free(buckets);
return ;
}
struct node *InsertionSort(struct node *list){
struct node *k,*nodeList;
if (list==0 || list->next==0){
return list;
}
nodeList=list;
k=list->next;
nodeList->next;
while(k!=0){
struct node *ptr;
if(nodeList->data>k->data){
struct node *tmp;
tmp=k;
k=k->next;
tmp->next=nodeList;
nodeList=tmp;
continue;
}
for (ptr=nodeList;ptr->next!=0;ptr=ptr->next){
if (ptr->next->data>k->data)break;
}
if (ptr->next!=0){
struct node *temp;
temp=k;
k=k->next;
temp->next=ptr->next;
ptr->next=temp;
continue;
}
else{
ptr->next=k;
k=k->next;
ptr->next->next=0;
continue;
}
}
return nodeList;
}
int getBucketIndex(int value){
return value/interval;
}
void print(int arr[]){
int i;
for(i=0;i<narray;++i){
cout<<setw(3)<<arr[i];
cout<<endl;
}
}
void printBuckets(struct node *list){
struct node *cur=list;
while(cur){
cout<<setw(3)<<cur->data;
cur=cur->next;
}
}
int main(){
int array[narray] = {29,25,3,49,9,37,21,43};
cout << "Initial array" << endl;
print(array);
cout << "-------------" << endl;
BucketSort(array);
cout << "-------------" << endl;
cout << "Sorted array" << endl;
print(array);
return 0;
}
but it writes following error
Error 1 error C2664: 'printBuckets' : cannot convert parameter 1 from 'node *' to 'Node *' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 39 bucket_sort
Error 2 error C2664: 'InsertionSort' : cannot convert parameter 1 from 'node *' to 'Node *' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 45 bucket_sort
Error 3 error C2664: 'printBuckets' : cannot convert parameter 1 from 'node *' to 'Node *' c:\documents and settings\student\my documents\visual studio 2008\projects\bucket_sort\bucket_sort\bucket_sort.cpp 51 bucket_sort
i did not understand why it can't convert?i have indicated everything so what is wrong?
Upvotes: 0
Views: 143
Reputation: 31425
typedef Node node;
will resolve your case-sensitivity issue.
Not the proper fix really though
Upvotes: 0
Reputation: 73433
C++ is case sensitive so node
and Node
are two different types, you have defined only type node
but are accepting a totally different type Node
as the argument to your functions. Compiler is complaining that it doesn't how to convert between these two types.
Upvotes: 2