a.
// Al_8-3_a.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
struct bucket
{
double value;
bucket *next;
};
bucket *buckets[100];
bucket *insert(bucket *head, double m)
{
bucket *p0, *p1, *p2;
p0 = (bucket*)malloc(sizeof(bucket)); //需先初始化p0,后面才方便赋值
p2 = (bucket*)malloc(sizeof(bucket)); //避免提示p2未初始化
p0->value = m;
p1 = head;
if (head == nullptr) //如果链表为空链表,则插入节点为头指针且为唯一结点
{
head = p0;
head->next = nullptr;
return head;
}
else //链表不为空
{
while (p1->value < p0->value && p1->next != nullptr) //寻找插入点
{
p2 = p1;
p1 = p1->next;
}
if (p1->value > p0->value) //找到插入点
{
if (p1 == head) //特殊情况,插入点在头指针前
{
p0->next = head;
head = p0; //插入结点作为头指针
}
else //结点插入在p2和p1之间
{
p0->next = p1;
p2->next = p0; //此行会提示p2可能未初始化,所以开头加上p2 = (bucket*)malloc(sizeof(bucket));
}
}
else //链表的结点之间没有插入点,p0结点应该插入在链表尾部
{
p1->next = p0;
p0->next = nullptr; //p0结点作为链表的尾结点
}
return head; //返回链表头指针
}
}
void print(bucket *head)
{
bucket *p;
p = head;
while (p != nullptr)
{
cout << p->value << endl;
p = p->next;
}
}
int get_length(int a)
{
int count = 0;
while (a)
{
a /= 10;
++count;
}
return count;
}
int _tmain(int argc, _TCHAR* argv[])
{
double a[] = { 1,34,8,456,23,365,9999,567,4567 };
int len = sizeof(a) / sizeof(double);
for (int i = 0; i < len; ++i)
{
buckets[i] = nullptr; //每个桶都为空链表
}
for (int i = 0; i < len; ++i)
{
int k = get_length(a[i]); //按照位数分桶
buckets[k] = insert(buckets[k], a[i]); //将每个元素放入对应区间中的桶,并返回对应桶的头指针
}
for (int i = 1; i < 10; ++i) //共有10个桶,因为赋值时从i=1开始,因此打印也需要从i=1开始
{
print(buckets[i]);
}
return 0;
}
b.
// Al_8-3_b.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
struct bucket
{
char value[20]; //需定义为字符串数组,方便之后赋值
bucket *next;
};
bucket *buckets[100];
bucket *insert(bucket *head, string m)
{
bucket *p0, *p1, *p2;
p0 = (bucket*)malloc(sizeof(bucket)); //需先初始化p0,后面才方便赋值
p2 = (bucket*)malloc(sizeof(bucket)); //避免提示p2未初始化
strcpy_s(p0->value, m.c_str()); //strcpy将一个字符串数组赋值到另一个字符串数组;c_str()将string转换为char形式
p1 = head;
if (head == nullptr) //如果链表为空链表,则插入节点为头指针且为唯一结点
{
head = p0;
head->next = nullptr;
return head;
}
else //链表不为空
{
while (p1->value < p0->value && p1->next != nullptr) //寻找插入点
{
p2 = p1;
p1 = p1->next;
}
if (p1->value > p0->value) //找到插入点
{
if (p1 == head) //特殊情况,插入点在头指针前
{
p0->next = head;
head = p0; //插入结点作为头指针
}
else //结点插入在p2和p1之间
{
p0->next = p1;
p2->next = p0; //此行会提示p2可能未初始化,所以开头加上p2 = (bucket*)malloc(sizeof(bucket));
}
}
else //链表的结点之间没有插入点,p0结点应该插入在链表尾部
{
p1->next = p0;
p0->next = nullptr; //p0结点作为链表的尾结点
}
return head; //返回链表头指针
}
}
void print(bucket *head)
{
bucket *p;
p = head;
while (p != nullptr)
{
cout << p->value << endl;
p = p->next;
}
}
/*int get_length(int a)
{
int count = 0;
while (a)
{
a /= 10;
++count;
}
return count;
}*/
int _tmain(int argc, _TCHAR* argv[])
{
string a[] = { "av", "a", "kjd", "we", "poip", "qwio", "slkjfs", "joijuoifd", "sefwhiuo" };
int len = sizeof(a) / sizeof(string);
for (int i = 0; i < len; ++i)
{
buckets[i] = nullptr; //每个桶都为空链表
}
for (int i = 0; i < len; ++i)
{
int k = a[i].size(); //按照字符数分桶
buckets[k] = insert(buckets[k], a[i]); //将每个元素放入对应区间中的桶,并返回对应桶的头指针
}
for (int i = 1; i < 10; ++i) //先分为10个桶,因为赋值时从i=1开始,因此打印也需要从i=1开始
{
print(buckets[i]);
}
return 0;
}