//Queue.h
#if ! defined _Queue_H_
#define _Queue_H_class Queue
{
private:
struct QUE
{
int Int;
double Double;
char Char;
struct QUE *next;
}*head,*tail,*temp;
int Queue_Length;
int flag;
#define Queue_Size sizeof(QUE)
public:
Queue();
~Queue();
bool push(int Int);
bool push(double Double);
bool push(char Char);
void pop();
int getFrontInt();
double getFrontDouble();
char getFrontChar();
bool Empty();
int Size();
void Clear();
void DisplayDataType();
};
#endif
//Queue.cpp
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include"Queue.h"
//构造函数
Queue::Queue()
{
flag = Queue_Length = 0;
}
//析构函数
Queue::~Queue()
{
Clear();
}
//push函数
bool Queue::push(int Int)
{
if(!flag)
{
head = tail = (QUE*)malloc(Queue_Size);
if(!head) {printf("内存分配失败!\n");return false;}
head->Int = Int;
tail->next=NULL;
++Queue_Length;
flag=1;
}
else if(flag == 1)
{
temp = (QUE*)malloc(Queue_Size);
if(!temp) {printf("内存分配失败!\n");return false;}
temp->Int = Int;
tail->next = temp;
tail = temp;
tail->next=NULL;
++Queue_Length;
}
else {printf("数值类型不匹配!\n");return false;}
return true;
}
bool Queue::push(double Double)
{
if(!flag)
{
head = tail = (QUE*)malloc(Queue_Size);
if(!head) {printf("内存分配失败!\n");return false;}
head->Double = Double;
tail->next=NULL;
++Queue_Length;
flag=2;
}
else if(flag == 2)
{
temp = (QUE*)malloc(Queue_Size);
if(!temp) {printf("内存分配失败!\n");return false;}
temp->Double = Double;
tail->next = temp;
tail = temp;
tail->next=NULL;
++Queue_Length;
}
else {printf("数值类型不匹配!\n");return false;}
return true;
}
bool Queue::push(char Char)
{
if(!flag)
{
head = tail = (QUE*)malloc(Queue_Size);
if(!head) {printf("内存分配失败!\n");return false;}
head->Char = Char;
tail->next=NULL;
++Queue_Length;
flag=3;
}
else if(flag == 3)
{
temp = (QUE*)malloc(Queue_Size);
if(!temp) {printf("内存分配失败!\n");return false;}
temp->Char = Char;
tail->next = temp;
tail = temp;
tail->next=NULL;
++Queue_Length;
}
else {printf("数值类型不匹配!\n");return false;}
return true;
}
//pop函数
void Queue::pop()
{
if(flag)
{
temp = head;
head = head->next;
free(temp);
--Queue_Length;
if(!Queue_Length) flag=0;
}
}
//getFront函数
int Queue::getFrontInt()
{
if(flag==1) return head->Int;
else printf("操作有误!\n");
return 0;
}
double Queue::getFrontDouble()
{
if(flag==2) return head->Double;
else printf("操作有误!\n");
return 0.0;
}
char Queue::getFrontChar()
{
if(flag==3) return head->Char;
else printf("操作有误!\n");
return '\n';
}
//Empty函数
bool Queue::Empty()
{
return !Queue_Length;
}
//Size函数
int Queue::Size()
{
return Queue_Length;
}
//Clear函数
void Queue::Clear()
{
while(head)
{
temp=head;
head=head->next;
free(temp);
}
flag=Queue_Length=0;
}
//Display函数
void Queue::DisplayDataType()
{
if(flag==1) printf("int\n");
else if(flag==2) printf("double\n");
else if(flag==3) printf("char\n");
else printf("null\n");
}