在循环队列里添加删除元素

#include
using namespace std;
const int Maxsize = 12;
struct QNode {
	int *Data;
	int front, rear;
	int Maxsize;
};
typedef struct QNode * Queue;

Queue CreatQueue(int Maxsize)
{
	Queue Q = new struct QNode;
	Q->Data = new int[Maxsize];
	Q->front = Q->rear = 0;
	Q->Maxsize = Maxsize;
	return Q;
}
bool Isfull(Queue Q)
{
	return((Q->rear - 1) % (Q->Maxsize) ==Q->front);  
}
bool IsEmpty(Queue Q)
{
	return(Q->front == Q->rear);
}
Queue AddQ(Queue Q,int a)
{
	if (Isfull(Q))
	{
		return Q;
	}
	else
	{
		Q->rear = (Q->rear + 1) % Q->Maxsize;
		Q->Data[Q->rear] = a;
		return Q;
	}
}
Queue Delete(Queue Q)
{
	if (IsEmpty(Q))
	{
		return Q;
	}
	else
	{
		Q->front = (Q->front + 1) % Q->Maxsize;		//front位序低,要加才能使对列减少
		return Q;
	}
}
void main()
{
	Queue Q;
	Q = CreatQueue(Maxsize);
	for (int i = 0; i < 10; i++)
		Q->Data[i] = i;
	Q->rear = 9;
	Queue k = AddQ(Q, 100);
	if (k->rear >= k->front)				//如果尾指针大于头指针,直接输出
	{
		for (int j = k->front; j <= k->rear; j++)
			cout << Q->Data[j] << " ";
	}
	else							//如果尾指针小于头指针,线输出到Maxsize,再从头输出
	{
		for (int j = k->front; j <= k->Maxsize; j++)
			cout << Q->Data[j] << " ";
		for (int j = 0; j <= k->rear; j++)
			cout << Q->Data[j] << " ";
	}
	cout << endl;
	Queue m = Delete(Q);


	if (m->rear >= m->front)
	{
		for (int j = m->front; j <= m->rear; j++)
			cout << Q->Data[j] << " ";
	}
	else
	{
		for (int j = m->front; j <= m->Maxsize; j++)
			cout << Q->Data[j] << " ";
		for (int j = 0; j <= m->rear; j++)
			cout << Q->Data[j] << " ";
	}
	cin.get();
}

你可能感兴趣的:(在循环队列里添加删除元素)