链表类
class ChainNode
{
public:
//元素
int element;
//下一个节点
ChainNode* next;
//空构造函数
ChainNode() {
}
//包含元素的构造
ChainNode(const int& element)
{
this->element = element;
}
ChainNode(const int& element, ChainNode* next)
{
this->element = element;
this->next = next;
}
};
//包装链表类的Chain类
class Chain
{
protected:
ChainNode* firstNode;
int listSize;
bool checkIndex(int index) const
{
return (index < 0 || index > listSize);
}
public:
Chain(int capacity = 10)
{
if (capacity < 1)
{
cout << "the capacity your input is to slow" << endl;
return;
}
firstNode = NULL;
listSize = 0;
}
Chain(const Chain& c)
{
listSize = c.listSize;
if (listSize == 0)
{
firstNode = NULL;
return;
}
ChainNode* sourceNode = c.firstNode;
firstNode = new ChainNode(sourceNode->element);
}
~Chain()
{
//逐个删除
while (firstNode != NULL)
{
ChainNode* next = firstNode->next;
delete firstNode;
firstNode = next;
}
}
bool empty()
{
return (listSize == 0);
}
int size()
{
return listSize;
}
int get(int index)
{
if (checkIndex(index))
{
cout << "the index you input is wrong" << endl;
//这里正常要抛出异常,省略了
return -11111111111;
}
ChainNode* current = firstNode;
for (int i = 0; i < index; i++)
{
current = current->next;
}
return current->element;
}
int indexOf(const int element) const
{
ChainNode* current = firstNode;
for (int index = 0; index < listSize && current != NULL; index++)
{
if (current->element == element)
{
return index;
}
current = current->next;
}
return -1;
}
void erase(int index)
{
if (checkIndex(index))
{
cout << "the index you input is wrong" << endl;
return;
}
ChainNode* deleteNode = firstNode;
//如果移除的是头节点
if (index == 0)
{
firstNode = firstNode->next;
}
else
{
ChainNode* current = firstNode;
//index - 1即为索引为index的前驱节点
for (int i = 0; i < index - 1; i++)
{
current = current->next;
}
//将要删除的元素赋值给deleteNode
deleteNode = current->next;
current->next = current->next->next;
}
listSize--;
delete deleteNode;
}
void insert(int index, const int element)
{
if (checkIndex(index))
{
cout << "the index you input is wrong" << endl;
return;
}
ChainNode* current = firstNode;
if (index == 0)
{
firstNode = new ChainNode(element, firstNode);
}
else
{
for (int i = 0; i < index - 1; i++)
{
current = current->next;
}
current->next = new ChainNode(element, current->next);
}
listSize++;
}
//将单链表反转
void reverse()
{
int length = listSize;
for (int index = 0; index < listSize - 1; index++)
{
int headEle = this->get(0);
this->insert(length--, headEle);
this->erase(0);
}
}
friend ostream& operator << (ostream& cout, Chain& c);
};
ostream& operator << (ostream& cout, Chain& c)
{
ChainNode* current = c.firstNode;
while (current != NULL)
{
cout << current->element << " ";
current = current->next;
}
return cout;
}
完整代码
要排序的数的范围和个数由用户输入
#include<iostream>
#include<ctime>
using namespace std;
//链表类
class ChainNode
{
public:
//元素
int element;
//下一个节点
ChainNode* next;
//空构造函数
ChainNode() {
}
//包含元素的构造
ChainNode(const int& element)
{
this->element = element;
}
ChainNode(const int& element, ChainNode* next)
{
this->element = element;
this->next = next;
}
};
//包装链表类的Chain类
class Chain
{
protected:
ChainNode* firstNode;
int listSize;
bool checkIndex(int index) const
{
return (index < 0 || index > listSize);
}
public:
Chain(int capacity = 10)
{
if (capacity < 1)
{
cout << "the capacity your input is to slow" << endl;
return;
}
firstNode = NULL;
listSize = 0;
}
Chain(const Chain& c)
{
listSize = c.listSize;
if (listSize == 0)
{
firstNode = NULL;
return;
}
ChainNode* sourceNode = c.firstNode;
firstNode = new ChainNode(sourceNode->element);
}
~Chain()
{
//逐个删除
while (firstNode != NULL)
{
ChainNode* next = firstNode->next;
delete firstNode;
firstNode = next;
}
}
bool empty()
{
return (listSize == 0);
}
int size()
{
return listSize;
}
int get(int index)
{
if (checkIndex(index))
{
cout << "the index you input is wrong" << endl;
return -11111111111;
}
ChainNode* current = firstNode;
for (int i = 0; i < index; i++)
{
current = current->next;
}
return current->element;
}
int indexOf(const int element) const
{
ChainNode* current = firstNode;
for (int index = 0; index < listSize && current != NULL; index++)
{
if (current->element == element)
{
return index;
}
current = current->next;
}
return -1;
}
void erase(int index)
{
if (checkIndex(index))
{
cout << "the index you input is wrong" << endl;
return;
}
ChainNode* deleteNode = firstNode;
//如果移除的是头节点
if (index == 0)
{
firstNode = firstNode->next;
}
else
{
ChainNode* current = firstNode;
//index - 1即为索引为index的前驱节点
for (int i = 0; i < index - 1; i++)
{
current = current->next;
}
//将要删除的元素赋值给deleteNode
deleteNode = current->next;
current->next = current->next->next;
}
listSize--;
delete deleteNode;
}
void insert(int index, const int element)
{
if (checkIndex(index))
{
cout << "the index you input is wrong" << endl;
return;
}
ChainNode* current = firstNode;
if (index == 0)
{
firstNode = new ChainNode(element, firstNode);
}
else
{
for (int i = 0; i < index - 1; i++)
{
current = current->next;
}
current->next = new ChainNode(element, current->next);
}
listSize++;
}
//将单链表反转
void reverse()
{
int length = listSize;
for (int index = 0; index < listSize - 1; index++)
{
int headEle = this->get(0);
this->insert(length--, headEle);
this->erase(0);
}
}
friend ostream& operator << (ostream& cout, Chain& c);
};
ostream& operator << (ostream& cout, Chain& c)
{
ChainNode* current = c.firstNode;
while (current != NULL)
{
cout << current->element << " ";
current = current->next;
}
return cout;
}
int main()
{
//用户手动输入要排序数据的总数
int n = 0;
cout << "请输入要排序的数的个数" << endl;
cin >> n;
//用户输入数的范围
cout << "请输入参与排序的数的最大值" << endl;
int max = 0;
cin >> max;
//随机数种子
srand((unsigned)time(NULL));
//准备初始链表
Chain c(n);
//随机产生n个0-max的随机数,并插入链表中
for (int index = 0; index < n; index++)
{
int temp = rand() % (max + 1);
c.insert(0, temp);
}
//创建箱子
Chain* box = new Chain[max + 1];
//将链表c中的元素插入box中
//链表c采用头删除,box[index]链表采用头插入
//循环n次
for (int index = 0; index < n; index++)
{
//获取第一个元素
int temp = c.get(0);
//删除第一个元素
c.erase(0);
//放入对应的箱子当中
box[temp].insert(0, temp);
}
//初始化保存排序后的链表
Chain sorted;
//将箱子中的元素取出,删除,并插入新的链表中
for (int index = max; index >= 0; index--)
{
//先获取箱子的长度,由于箱子在删除元素的时候长度在不断减小
//这时候要先获取长度才行
int size = box[index].size();
//从箱子中取元素并删除
for (int i = 0; i < size; i++)
{
int temp = box[index].get(0);
box[index].erase(0);
sorted.insert(0, temp);
}
}
//输出新的链表
cout << sorted << endl;
}