【数据结构】 队列的基本操作

/*
==========================================================================================
				队列的基本操作
		By~fanxingzju		2014.04.17
1.构造一个空队列Q
2.销毁队列Q,Q不再存在
3.将Q清为空队列
4.若队列Q为空,则返回true,否则换回false
5.用length返回队列Q的元素个数
6.若队列不为空,则用elem返回队列Q的队头元素,并返回true;否则换回false
7.插入元素elem为队列Q的新的队尾元素
8.若队列不为空,则删除Q的队头元素,用elem返回其值,并返回true,否则返回false
==========================================================================================
*/
#include <stdio.h>
#include <stdlib.h>

typedef int QElement;

typedef struct QNode
{
	QElement data;
	struct QNode *next;
}QNode;

typedef struct
{
	QNode *front;
	QNode *rear;
}LinkQueue;

//1.构造一个空队列Q
bool InitQueue(LinkQueue &Q)
{
	Q.front = Q.rear = new QNode;
	if (!Q.front)
	{
		printf("InitQueue()函数执行,内存分配失败\n");
		system("pause");
		exit(-1);
	}
	Q.front->next = NULL;
	printf("InitQueue()函数执行,队列初始化成功\n");
	return true;
}

//2.销毁队列Q,Q不再存在
bool DestoryQueue(LinkQueue &Q)
{
	while(Q.front)
	{
		Q.rear = Q.front->next;
		delete Q.front;
		Q.front = Q.rear;
	}
	printf("DestoryQueue()函数执行,队列销毁成功\n");
	return true;
}

//3.将Q清为空队列
bool ClearQueue(LinkQueue &Q)
{
	QNode *temp = Q.front->next;
	while(!temp)
	{
		QNode *ttemp = temp->next;
		temp = temp->next;
		delete ttemp;
	}
	Q.front->next = NULL;
	Q.rear = Q.front;
	printf("ClearQueue()函数执行,清空队列成功\n");
	return true;
}

//4.若队列Q为空,则返回true,否则换回false
bool EmptyQueue(LinkQueue Q)
{
	if (Q.front == Q.rear)
	{
		printf("EmptyQueue()函数执行,队列为空\n");
		return true;
	}
	printf("EmptyQueue()函数执行,队列非空\n");
	return false;
}

//5.用length返回队列Q的元素个数
bool LengthQueue(LinkQueue Q, int &length)
{
	length = 0;
	QNode *temp = Q.front;
	while(temp != Q.rear)
	{
		++length;
		temp = temp->next;
	}
	printf("LengthQueue()函数执行,队列的长度为 %d \n", length);
	return true;
}

//6.若队列不为空,则用elem返回队列Q的队头元素,并返回true;否则换回false
bool GetHeadQueue(LinkQueue Q, QElement &elem)
{
	if (Q.front == Q.rear)
	{
		printf("GetHeadQueue()函数执行,队列为空,获取头元素失败\n");
		return false;
	}
	elem = Q.front->next->data;
	printf("GetHeadQueue()函数执行,队列的头元素为 %d \n", elem);
	return true;
}

//7.插入元素elem为队列Q的新的队尾元素
bool InsertQueue(LinkQueue &Q, QElement elem)
{
	QNode *temp;
	temp = new QNode;
	if (!temp)
	{
		printf("InsertQueue()函数执行,内存分配失败\n");
		exit(-1);
	}
	temp->data = elem;
	temp->next = NULL;
	Q.rear->next = temp;
	Q.rear = temp;
	printf("InsertQueue()函数执行,元素 %d 插入成功\n", elem);
	return true;
}

//8.若队列不为空,则删除Q的队头元素,用elem返回其值,并返回true,否则返回false
bool DeleteQueue(LinkQueue &Q, QElement &elem)
{
	if (Q.front == Q.rear)
	{
		printf("DeleteQueue()函数执行,目标队列为空,删除头元素失败\n");
		return false;
	}

	QNode *temp = Q.front->next;
	elem = temp->data;
	Q.front->next = temp->next;
	if (Q.rear == temp)
	{
		Q.rear = Q.front;
	}
	delete temp;
	printf("DeleteQueue()函数执行,头元素 %d 删除成功\n", elem);
	return true;
}

int main()
{
	LinkQueue Q;
	int length;
	QElement temp;

	InitQueue(Q);
	for (int i = 0; i != 10; ++i)
	{
		LengthQueue(Q, length);
		InsertQueue(Q, i);
	}
	EmptyQueue(Q);
	ClearQueue(Q);
	EmptyQueue(Q);

	for(int i = 9; i != 1; --i)
	{
		InsertQueue(Q, i);
	}
	
	LengthQueue(Q, length);

	while(DeleteQueue(Q, temp))
	{
		GetHeadQueue(Q, temp);
	}

	EmptyQueue(Q);

	DestoryQueue(Q);

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用 C++ 编写的循环队列示例代码: ```c++ #include <iostream> using namespace std; class CircularQueue { private: int *queue; // 队列数组指针 int front; // 队首索引 int rear; // 队尾索引 int size; // 队列大小 public: CircularQueue(int k) { // 构造函数,初始化队列 queue = new int[k]; front = 0; rear = -1; size = k; } bool enqueue(int value) { // 入队操作 if (isFull()) { // 队列已满,入队失败 return false; } rear = (rear + 1) % size; queue[rear] = value; return true; } bool dequeue() { // 出队操作 if (isEmpty()) { // 队列为空,出队失败 return false; } front = (front + 1) % size; return true; } int frontValue() { // 获取队首元素 if (isEmpty()) { return -1; } return queue[front]; } int rearValue() { // 获取队尾元素 if (isEmpty()) { return -1; } return queue[rear]; } bool isEmpty() { // 判断队列是否为空 return front == (rear + 1) % size; } bool isFull() { // 判断队列是否已满 return front == (rear + 2) % size; } }; int main() { CircularQueue q(5); // 创建大小为 5 的循环队列 q.enqueue(1); // 入队 1 q.enqueue(2); // 入队 2 q.enqueue(3); // 入队 3 q.enqueue(4); // 入队 4 q.enqueue(5); // 入队 5,此时队列已满,入队失败 cout << q.frontValue() << endl; // 输出队首元素,即 1 q.dequeue(); // 出队,队列中元素变为 2,3,4,5 cout << q.frontValue() << endl; // 输出队首元素,即 2 cout << q.rearValue() << endl; // 输出队尾元素,即 5 return 0; } ``` 这个循环队列的实现采用了“队首指针指向队列第一个元素的前一个位置,队尾指针指向队列最后一个元素”的方式,这样可以方便地判断队列是否为空或已满。在入队操作中,先将队尾指针加一,然后将元素插入到队尾指向的位置;在出队操作中,先将队首指针加一,然后将队首指向的元素删除。同时,队列大小为 k 时,队列中最多可以存储 k-1 个元素,因此在判断队列是否已满时需要将队首指针减一后再与队尾指针比较。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值