标签:队列 C++ Queue int MAXSize 数组 front data rear
Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
#include <cstring>
#define Queue_MAXSize 10
using namespace std;
class Queue
{
private:
int data[Queue_MAXSize];
int front;
int rear;
public:
Queue();
~Queue();
bool Queue_Init(void);
int Queue_Length(void);
int Queue_Front(void);
int Queue_Rear(void);
bool Queue_Insert(int data);
int Queue_Output(void);
bool Queue_Delete(int data);
bool Queue_Visit(void);
};
#endif
Queue.c
#include "Queue.h"
Queue::Queue()
{
}
Queue::~Queue()
{
}
bool Queue::Queue_Init()
{
memset(data, 0, Queue_MAXSize * 4);
front = 0;
rear = 0;
return true;
}
int Queue::Queue_Length()
{
if ((this->front == this->rear) && this->data[front])
return Queue_MAXSize;
return ((this->rear - this->front + Queue_MAXSize) % Queue_MAXSize);
}
int Queue::Queue_Front()
{
if ((this->front == this->rear) && (this->data[front] == 0))
return false;
return this->data[front];
}
int Queue::Queue_Rear()
{
if ((this->front == this->rear) && (this->data[front] == 0))
return false;
return this->data[(rear - 1 + Queue_MAXSize) % Queue_MAXSize];
}
bool Queue::Queue_Insert(int data)
{
if ((this->front == this->rear) && this->data[front])
return false;
this->data[rear] = data;
this->rear = (this->rear + 1) % Queue_MAXSize;
return true;
}
int Queue::Queue_Output()
{
int data = 0;
if ((this->front == this->rear) && (this->data[front] == 0))
return false;
data = this->data[this->front];
this->data[this->front] = 0;
this->front = (this->front + 1) % Queue_MAXSize;
return data;
}
bool Queue::Queue_Delete(int data)
{
int i = this->front;
if ((this->front == this->rear) && (this->data[front] == 0))
return false;
while (i != this->rear)
{
if (this->data[i] == data)
break;
i = (i + 1) % Queue_MAXSize;
}
if (this->data[i] == data)
{
while (i != ((rear - 1 + Queue_MAXSize) % Queue_MAXSize))
{
this->data[i] = this->data[(i + 1) % Queue_MAXSize];
i = (i + 1) % Queue_MAXSize;
}
this->data[i] = 0;
this->rear = (this->rear - 1 + Queue_MAXSize) % Queue_MAXSize;
}
return true;
}
bool Queue::Queue_Visit()
{
int i = this->front;
if ((this->front == this->rear) && (this->data[front] == 0))
return false;
while (i != this->rear)
{
cout << "this->data[" << i << "] = " << this->data[i] << endl;
i = (i + 1) % Queue_MAXSize;
}
return true;
}
qq_38705421
发布了15 篇原创文章 · 获赞 0 · 访问量 159
私信
关注
标签:队列,C++,Queue,int,MAXSize,数组,front,data,rear 来源: https://blog.csdn.net/qq_38705421/article/details/104067655
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。