第10关:队列的插入和删除

任务描述

本关任务:假设以数组Q[m]存放循环队列中的元素, 同时设置一个标志tag,以tag == 0和tag == 1来区别在队头指针(front)和队尾指针(rear)相等时,队列状态为“空”还是“满”。试编写与此结构相应的插入(enqueue)和删除(dlqueue)算法。

测试说明

输入相应的数字代表相关的操作 1.初始化 2.入队,当输入2时,还需要输入入队元素 3.出队 0.退出

测试输入: 1 2 a 2 b 3 3 3 0 预期输出: 成功对队列进行初始化 a已入队 b已入队 a已出队 b已出队 队列已空,无可出队元素

#include <iostream>
using namespace std;

#define MAXSIZE 5
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char ElemType;
typedef int Status;

typedef struct {
	ElemType *base;
	int front, rear, tag;
} SqQueue;

Status InitQueue(SqQueue &Q) {
	//###### Begin ######
	Q.base = new ElemType[5];
	if (!Q.base)
		return ERROR;
	Q.front = Q.rear = 0;
	Q.tag = 0;
	return OK;
	// ###### End ######
}

Status EnQueue(SqQueue &Q, ElemType e) {
	//###### Begin ######
	if (Q.tag == 1) {
		Q.tag = 1;
		return ERROR;
	}
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % 5;
	Q.tag = 2;
	if (Q.front == Q.rear)
		Q.tag = 1;
	return OK;
	// ###### End ######
}

Status DeQueue(SqQueue &Q, ElemType &e) {
	//###### Begin ######
	if (Q.tag == 0)
		return ERROR;
	e = Q.base[Q.front];
	Q.front = (Q.front + 1) % 5;
	Q.tag = 2;
	if (Q.front == Q.rear)
		Q.tag = 0;
	return OK;
	// ###### End ######
}

main() {
	SqQueue q;
	int choose, flag;
	ElemType j;

	choose = -1;
	while (choose != 0) {

		cin >> choose;
		switch (choose) {
			case 1:
				if (InitQueue(q)) {
					flag = 1;
					cout << "成功对队列进行初始化\n";
				} else
					cout << "初始化队列失败\n";
				break;
			case 2:
				if (flag) {
					flag = 1;

					cin >> j;
					if (EnQueue(q, j))
						cout << j << "已入队\n";
					else
						cout << "队列已满,不可入队\n";
				} else
					cout << "队列未建立,请重新选择\n";
				break;
			case 3:
				if (flag) {
					flag = 1;
					if (DeQueue(q, j))
						cout << j << "已出队\n";
					else
						cout << "队列已空,无可出队元素\n";
				} else
					cout << "队列未建立,请重新选择\n";
				break;
		}
	}
}

题目非原创,为头歌所见,侵删。
答案为作者原创。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列的数据结构是一种先进先出(First In First Out,FIFO)的线性数据结构,它可以通过数组或链表来实现。队列有两种插入数据的方法:尾插法和头插法。 1. 尾插法实现队列 尾插法是将新元素插入队列的末尾,因此也被称为入队操作。当需要删除队列中的数据时,从队列的头部删除即可,也被称为出队操作。 下面是C语言实现队列尾插法的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 typedef struct { int data[MAXSIZE]; int front; // 队头指针 int rear; // 队尾指针 } Queue; // 初始化队列 void initQueue(Queue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int isEmpty(Queue q) { return q.front == q.rear; } // 判断队列是否已满 int isFull(Queue q) { return (q.rear + 1) % MAXSIZE == q.front; } // 入队操作 int enqueue(Queue *q, int x) { if (isFull(*q)) { printf("Queue is full.\n"); return 0; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAXSIZE; return 1; } // 出队操作 int dequeue(Queue *q, int *x) { if (isEmpty(*q)) { printf("Queue is empty.\n"); return 0; } *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } int main() { Queue q; initQueue(&q); enqueue(&q, 1); enqueue(&q, 2); enqueue(&q, 3); int x; while (dequeue(&q, &x)) { printf("%d ", x); } return 0; } ``` 输出结果为: ``` 1 2 3 ``` 2. 头插法实现队列 头插法是将新元素插入队列的头部,因此也被称为入队操作。当需要删除队列中的数据时,从队列的头部删除即可,也被称为出队操作。 下面是C语言实现队列头插法的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 typedef struct { int data[MAXSIZE]; int front; // 队头指针 int rear; // 队尾指针 } Queue; // 初始化队列 void initQueue(Queue *q) { q->front = q->rear = 0; } // 判断队列是否为空 int isEmpty(Queue q) { return q.front == q.rear; } // 判断队列是否已满 int isFull(Queue q) { return (q.rear + 1) % MAXSIZE == q.front; } // 入队操作 int enqueue(Queue *q, int x) { if (isFull(*q)) { printf("Queue is full.\n"); return 0; } q->front = (q->front - 1 + MAXSIZE) % MAXSIZE; q->data[q->front] = x; return 1; } // 出队操作 int dequeue(Queue *q, int *x) { if (isEmpty(*q)) { printf("Queue is empty.\n"); return 0; } *x = q->data[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } int main() { Queue q; initQueue(&q); enqueue(&q, 1); enqueue(&q, 2); enqueue(&q, 3); int x; while (dequeue(&q, &x)) { printf("%d ", x); } return 0; } ``` 输出结果为: ``` 3 2 1 ``` 注意,头插法实现的队列的队头指针指向的是队列中的最后一个元素,队尾指针指向的是队列中的第一个元素。因此,入队操作时队头指针要减1,出队操作时队头指针要加1。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值