head.h
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <cstring>
#define MAXSIZE 5
using namespace std;
class Stack
{
private:
int data[MAXSIZE]; //创建一个数组储存队列
int front; //定义一个头指针
int rear; //定义一个尾指针
public:
Stack(){front = rear = 0;} //清空队列
void pushqueue(int e); //入队
int popqueue(); //出队
void show(); //展示
int lengthqueue(); //求队列长度
bool fullqueue(); //判满
bool emptyqueue(); //判空
};
#endif // QUEUE_H
main.cpp
#include"queue.h"
int main()
{
Stack s1;
s1.pushqueue(10);
s1.pushqueue(20);
s1.pushqueue(30);
s1.pushqueue(40);
s1.show();
s1.popqueue();
s1.show();
cout<<s1.fullqueue()<<endl;
cout<<s1.emptyqueue()<<endl;
cout<<"该队列长度size = "<<s1.lengthqueue()<<endl;
return 0;
}
test.cpp
#include"queue.h"
void Stack::pushqueue(int e)
{
data[rear] = e; //入队头插
rear = (rear+1)%MAXSIZE; //尾指针加一
}
int Stack::popqueue()
{
front = (front + 1)%MAXSIZE; //尾删
return 1;
}
void Stack::show()
{
for(int i=front;i!=rear;i=(i+1)%MAXSIZE) //循环展示
{
cout<<data[i]<<" ";
}
cout<<endl;
}
bool Stack::fullqueue() //判满
{
return front == (rear + 1)%MAXSIZE; //队列尾加一等于头表明队列为满
//返回1为满,0为非满
}
bool Stack::emptyqueue() //判空
{
return front == rear; //队列头尾相等,表明队列为空
//返回1为空,0为非空
}
int Stack::lengthqueue() //求队列长度
{
return((MAXSIZE+rear-front)%MAXSIZE);
}
运行结果: