如何实现不限类型的栈或队列?
猜想的实现方式:
采用union这种特殊的结构体实现各种类型的表示。
结构体设计:
struct{
int type;
union Data{
char chr;
unsigned char uchr;
short sht;
unsigned short usht;
int itr;
unsigned int uitr;
long lng;
unsigned ulng;
long long lng2;
unsigned long long ulng2;
float flt;
double dbl;
int * any;
}data;
}Node;
push的设计:
void push(char item){
stack[len].type = 1;
stack[len].data.chr = item;
len++;
}
void push(unsigned char item){
stack[len].type = 2;
stack[len].data.uchr = item;
len++;
}
void push(short item){
stack[len].type = 3;
stack[len].data.sht = item;
len++;
}
……以此类推
void push(void *item){
stack[len].type = 13;
stack[len].data.any = (int *)item;
len++;
}
pop的设计:
由于pop没有参数,所以不能采用重载的形式,只能有一个pop函数 。那么如何设计返回类型???
解决方法可能就只有万能指针了。void *
void * pop()
{
Data* tmp = (Data*)malloc(sizeof(Data));
*tmp = stack[len].data;
if(stack[len].type < 13){
len--;
return tmp;
} else{
len--;
return tmp->any;
}
}
实验代码:没有检查溢出等情况,只是为了测试功能。
#include<iostream>
#include"memwatch.h"
using namespace std;
#define MAX_NUM 10
union Data {
char chr;
unsigned char uchr;
short sht;
unsigned short usht;
int itr;
unsigned int uitr;
long lng;
unsigned ulng;
long long lng2;
unsigned long long ulng2;
float flt;
double dbl;
int *any;
};
struct Node{
unsigned int type;
Data data;
};
class Stack
{
public:
int len;
Node stack[MAX_NUM];
Stack()
{
len = 0;
}
void push(char chr){
stack[len].type = 1;
stack[len].data.chr = chr;
len++;
}
void push(unsigned char chr)
{
stack[len].type = 2;
stack[len].data.uchr = chr;
len++;
}
void push(short chr)
{
stack[len].type = 3;
stack[len].data.sht = chr;
len++;
}
void push(unsigned short chr)
{
stack[len].type = 4;
stack[len].data.usht = chr;
len++;
}
void push(int chr)
{
stack[len].type = 5;
stack[len].data.itr = chr;
len++;
}
void push(unsigned int chr)
{
stack[len].type = 6;
stack[len].data.uitr = chr;
len++;
}
void push(long chr)
{
stack[len].type = 7;
stack[len].data.lng = chr;
len++;
}
void push(unsigned long chr)
{
stack[len].type = 8;
stack[len].data.ulng = chr;
len++;
}
void push(long long chr)
{
stack[len].type = 9;
stack[len].data.lng2 = chr;
len++;
}
void push(unsigned long long chr)
{
stack[len].type = 10;
stack[len].data.ulng2 = chr;
len++;
}
void push(float chr)
{
stack[len].type = 11;
stack[len].data.flt = chr;
len++;
}
void push(double chr)
{
stack[len].type = 12;
stack[len].data.dbl = chr;
len++;
}
void push(void * chr)
{
stack[len].type = 13;
stack[len].data.any = (int *)chr;
len++;
}
void *pop()
{
Data *tmp = (Data *)malloc(sizeof(Data));
*tmp = stack[len-1].data;
if (stack[len-1].type < 13)
{
cout<<"return node"<<endl;
len--;
return tmp;
}
else
{
cout << "return node.any" << endl;
len--;
int * x = tmp->any;
free(tmp);
return x;
}
}
void display()
{
cout << "len" << len << endl;
for (int i = 0; i < len; i++)
{
cout << stack[i].data.lng << endl;
}
}
};
class A
{
public:
int a;
~A(){
cout<<"disconstructor"<<endl;
}
};
int main()
{
char *str = "ccccc";
A *a = new A();
a->a = 100;
printf("%ld\n",str);
Stack q;
q.push(1);
q.push(4L);
q.push(str);
q.push(a);
q.display();
A * res = (A *)q.pop();
char * xxx = (char *)q.pop();
int * it = (int *)q.pop();
cout<<res->a<<" "<<xxx<<" "<<*it<<endl;
delete res;
free(it);
}