Queue类

//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");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值