关于循环队列的入队、出队、遍历的操作

#include"stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
//定义队列结构
typedef struct Queue
{
	int *data;
	int rear;
	int front;
}squeue;
//初始化100个数字的队列
void Initqueue(squeue *q)
{
	q->data=(int *)malloc(MAX*sizeof(int));
	q->front=q->rear=0;
}
//返回队列长度
void queueL(squeue *q)
{
	printf("此队列的长度为:%d\n",(q->rear-q->front+MAX)%MAX);
}
//插入结点(入队)
void Insert(squeue *q,int e)
{
	if((q->rear+1)%MAX==q->front)
		printf("队列已满!\n");
	else
	{
		q->data[q->rear]=e;
		q->rear=(q->rear+1)%MAX;
	}
}
//删除结点(出队)
void Delete(squeue *q)
{
	if(q->rear==q->front)
		printf("队列已空!\n");
	else
	{
		q->front=(q->front+1)%MAX;
		printf("出队/删除 成功!\n");
	}
}
//打印(遍历)队列
void Print(squeue q)
{
	printf("________________________________________________________\n");
	while(q.front!=q.rear)
	{
		printf("%d ",q.data[q.front]);
		q.front=(q.front+1)%MAX;
	}
	printf("\n________________________________________________________\n");
}


int main()
{
	squeue l;
	int i,n,x,e;
	Initqueue(&l);//初始化队列
	while(1)
	{
		printf("\n***********************************************");
		printf("\n    1.建立队列");
		printf("\n    2.打印队列");
		printf("\n    3.入队");
		printf("\n    4.出队");
		printf("\n    0.退出");
		printf("\n***********************************************");
		printf("\n请输入操作数:");
		scanf("%d",&n);
		switch(n)
		{
		case 0:
			return 0;
		case 1:
			Initqueue(&l);
			printf("\n请输入队列长度:");
			scanf("%d",&x);
			for(i=1;i<=x;i++)
			{
				printf("\n请输入第%d个结点值:",i);
				scanf("%d",&e);
				Insert(&l,e);
			}
			printf("\n操作成功!");
			break;
		case 2:
			Print(l);
			break;
		case 3:
			printf("\n请输入需要入队的值:");
			scanf("%d",&e);
			Insert(&l,e);
			Print(l);
			break;
		case 4:
			Delete(&l);
			Print(l);
			break;
		default:
			printf("\n您的输入有误!");
			break;
		}
	}
}

以下是C++实现循环队列的基本操作的代码,包括初始化、入队出队、取队头元素、遍历出队列、求队列长度等: ```c++ #include <iostream> using namespace std; const int MAXSIZE = 100; // 循环队列的最大长度 class CircularQueue { private: int front; // 队头指针 int rear; // 队尾指针 int data[MAXSIZE]; // 队列数组 public: CircularQueue(int n) { // 初始化队列 front = rear = 0; for (int i = 0; i < n; i++) { data[i] = 0; } } bool isEmpty() { // 判断队列是否为空 return front == rear; } bool isFull() { // 判断队列是否已满 return (rear + 1) % MAXSIZE == front; } int length() { // 求队列长度 return (rear - front + MAXSIZE) % MAXSIZE; } bool enqueue(int x) { // 入队操作 if (isFull()) { return false; } data[rear] = x; rear = (rear + 1) % MAXSIZE; return true; } bool dequeue() { // 出队操作 if (isEmpty()) { return false; } front = (front + 1) % MAXSIZE; return true; } int getFront() { // 取队头元素操作 if (isEmpty()) { return -1; } return data[front]; } void traverse() { // 遍历出队列 if (isEmpty()) { cout << "The queue is empty." << endl; return; } cout << "The elements in the queue are: "; int i = front; while (i != rear) { cout << data[i] << " "; i = (i + 1) % MAXSIZE; } cout << endl; } }; int main() { int n = 5; // 循环队列的初始长度为5 CircularQueue q(n); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); q.enqueue(5); q.traverse(); // 输出:The elements in the queue are: 1 2 3 4 5 q.dequeue(); q.dequeue(); q.traverse(); // 输出:The elements in the queue are: 3 4 5 cout << "The front element is: " << q.getFront() << endl; // 输出:The front element is: 3 cout << "The length of the queue is: " << q.length() << endl; // 输出:The length of the queue is: 3 return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韓豆豆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值