介绍
用了c++里的模板类。
还是用void指针…
源代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 10, queue_max = 9;
template<class T>class queue
{
private:
T *datas;
int queue_front, queue_rear, max_size;
public:
queue<T>(int queue_size) { queue_front = 0, queue_rear = 0, max_size = queue_size, datas = (T*)malloc(queue_size * sizeof(T)); }
queue<T>() { queue_front = 0, queue_rear = 0, max_size = maxn, datas = (T*)malloc(maxn * sizeof(T)); }
bool push(T data)
{
if (queue_rear - queue_front == max_size) return false;
datas[queue_rear%max_size] = data, queue_rear++;
return true;
}
bool pop(void)
{
if (queue_front == queue_rear) return false;
queue_front++;
if (queue_front == max_size) queue_front = 0, queue_rear -= max_size;
return true;
}
void clear(void)
{
memset(datas, 0, sizeof(datas));
queue_front = 0, queue_rear = 0;
}
T front(bool &comp)
{
comp = queue_front != queue_rear;
return(queue_front == queue_rear) ? 0 : datas[queue_front % max_size];
}
unsigned size(void) { return queue_rear - queue_front; }
};
struct queue_point
{
void* root;
int data_type;
}queues[queue_max + 1];
int main(void)
{
int com, com2, com3,temp_int;
double temp_double;
char temp_char;
bool comp;
string queue_type;
while (1)
{
printf("********指令菜单********\n");
printf("*1-------------创建队列*\n");
printf("*2-------------销毁队列*\n");
printf("*3-------------清空队列*\n");
printf("*4-----------------入队*\n");
printf("*5-----------------出队*\n");
printf("*6-------------打印队头*\n");
printf("*0-----------------退出*\n");
printf("请输入指令编号:\n");
scanf("%d", &com);
if (com == 1)
{
printf("请输入要创建的队列编号(0-%d):\n", queue_max);
scanf("%d", &com2);
if (queues[com2].root != NULL) printf("队列已存在!\n");
else
{
printf("请输入要创建的队列数据类型(int、double、char)及队列长度(>0):\n");
cin >> queue_type;
scanf("%d", &com3);
if (queue_type.compare("int") == 0) queues[com2].root = new queue<int>(com3), queues[com2].data_type = 1;
else if (queue_type.compare("double") == 0) queues[com2].root = new queue<double>(com3), queues[com2].data_type = 2;
else if (queue_type.compare("int") == 0) queues[com2].root = new queue<char>(com3), queues[com2].data_type = 3;
else printf("数据类型不支持!\n");
}
}
else if (com == 2)
{
printf("请输入要销毁的队列编号(0-%d):\n", queue_max);
scanf("%d", &com2);
if (queues[com2].root == NULL) printf("队列不存在!\n");
else
{
if (queues[com2].data_type == 1) delete (queue<int>*)queues[com2].root;
else if (queues[com2].data_type == 2) delete (queue<double>*)queues[com2].root;
else if (queues[com2].data_type == 3) delete (queue<char>*)queues[com2].root;
queues[com2].data_type = 0, printf("链表已销毁!\n");
}
}
else if (com == 3)
{
printf("请输入要清空的队列编号(0-%d):\n", queue_max);
scanf("%d", &com2);
if (queues[com2].root == NULL) printf("队列不存在!\n");
else
{
if (queues[com2].data_type == 1) ((queue<int>*)queues[com2].root)->clear();
else if (queues[com2].data_type == 2) ((queue<double>*)queues[com2].root)->clear();
else if (queues[com2].data_type == 3) ((queue<char>*)queues[com2].root)->clear();
printf("链表已清空!\n");
}
}
else if (com == 4)
{
printf("请输入要执行操作的队列编号(0-%d):\n", queue_max);
scanf("%d", &com2);
printf("请输入数据:\n");
if (queues[com2].root == NULL) printf("队列不存在!\n");
else
{
if (queues[com2].data_type == 1) scanf("%d", &temp_int), ((queue<int>*)queues[com2].root)->push(temp_int);
else if (queues[com2].data_type == 2) scanf("%lf", &temp_double), ((queue<double>*)queues[com2].root)->push(temp_double);
else if (queues[com2].data_type == 3) getchar(), temp_char = getchar(), ((queue<char>*)queues[com2].root)->push(temp_char);
printf("入队成功!\n");
}
}
else if (com == 5)
{
printf("请输入要执行操作的队列编号(0-%d):\n", queue_max);
scanf("%d", &com2);
if (queues[com2].root == NULL) printf("队列不存在!\n");
else
{
if (queues[com2].data_type == 1 && ((queue<int>*)queues[com2].root)->pop()) printf("出队成功!\n");
else if (queues[com2].data_type == 2 && ((queue<double>*)queues[com2].root)->pop()) printf("出队成功!\n");
else if (queues[com2].data_type == 3 && ((queue<char>*)queues[com2].root)->pop()) printf("出队成功!\n");
else printf("出队失败!\n");
}
}
else if (com == 6)
{
printf("请输入要执行操作的队列编号(0-%d):\n", queue_max);
scanf("%d", &com2);
if (queues[com2].root == NULL) printf("队列不存在!\n");
else
{
if (queues[com2].data_type == 1)
{
temp_int = ((queue<int>*)queues[com2].root)->front(comp);
comp ? printf("队头元素为: %d\n", temp_int) : printf("队列为空!\n");
}
else if (queues[com2].data_type == 2)
{
temp_double = ((queue<double>*)queues[com2].root)->front(comp);
comp ? printf("队头元素为: %lf\n", temp_double) : printf("队列为空!\n");
}
else if (queues[com2].data_type == 3)
{
temp_char = ((queue<char>*)queues[com2].root)->front(comp);
comp ? printf("队头元素为: %d\n", temp_char) : printf("队列为空!\n");
}
}
}
else if (com == 0) return 0;
printf("按任意键继续..");
getchar();
getchar();
system("cls");
}
}