Created
October 31, 2014 11:05
-
-
Save fredrb/86e3ed255ccaa0d06518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<cstdio> | |
#include<cstdlib> | |
#include<iostream> | |
const int ARRAY_SIZE = 20; | |
const int MAX_VALUE = 999; | |
typedef struct N_NODE{ | |
struct N_NODE *n_next; | |
struct N_NODE *n_prev; | |
int value; | |
} bucketNode; | |
typedef struct{ | |
bucketNode *head; | |
bucketNode *tail; | |
} bucketList; | |
int genNumber(int*, int); | |
int bucketSort(int*, int); | |
void printArr(int*, int); | |
//L_LIST | |
int addBucket(bucketList*, int); | |
void printBucket(bucketList*); | |
int main(int argc, char *argv[]){ | |
int tbsort[ARRAY_SIZE]; | |
bucketList blist; | |
addBucket(&blist, 1); | |
std::cout << "Added 1" << std::endl; | |
addBucket(&blist, 10); | |
addBucket(&blist, 100); | |
printBucket(&blist); | |
return 0; | |
} | |
int genNumber(int *arr, int n){ | |
std::srand(time(NULL)); | |
for(int i = 0; i < n; i++) | |
arr[i] = std::rand() % MAX_VALUE; | |
return 0; | |
} | |
int bucketSort(int *arr, int n){ | |
if(n < 2) | |
return 0; | |
bucketList vBucket[n]; | |
for(int i = 0; i < n; i++){ | |
std::cout << arr[i]; | |
} | |
return 0; | |
} | |
void printArr(int *arr, int n){ | |
for(int i = 0; i < n; i++){ | |
std::cout << arr[i] << std:: endl; | |
} | |
} | |
int addBucket(bucketList *b_list, int b_value){ | |
bucketNode node; | |
bucketNode* pivot; | |
node.value = b_value; | |
if(b_list->head == NULL ) | |
b_list->head = b_list->tail = &node; | |
else{ | |
pivot = b_list->head; | |
while(pivot != NULL){ | |
if(pivot->value > node.value){ | |
if(b_list->head == pivot){ | |
//IF SMALLER THAN HEAD | |
b_list->head->n_prev = &node; | |
node.n_next = b_list->head; | |
b_list->head = &node; | |
return 0; | |
} | |
pivot->n_prev->n_next = &node; | |
node.n_prev = pivot->n_prev; | |
pivot->n_prev = &node; | |
node.n_next = pivot; | |
return 0; | |
} | |
pivot = pivot->n_next; | |
} | |
b_list->tail->n_next = &node; | |
node.n_prev = b_list->tail; | |
b_list->tail = &node; | |
} | |
return 0; | |
} | |
void printBucket(bucketList* b_list){ | |
bucketNode* pivot; | |
pivot = b_list->head; | |
std::cout << "Bucket:" << std::endl; | |
while(pivot != NULL){ | |
std::cout << pivot->value << std::endl; | |
pivot = pivot->n_next; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment