循环队列的入队和出队的程序
时间: 2024-06-13 10:05:05 浏览: 16
循环队列的入队和出队程序如下所示:
入队程序:
1.判断队列是否已满,如果满了则返回错误信息。
2.将元素插入队尾,即将元素存储在rear指针所指向的位置。
3.将rear指针向后移动一位,如果rear指针已经到达队列的末尾,则将其指向队列的头部。
出队程序:
1.判断队列是否为空,如果为空则返回错误信息。
2.将队头元素取出,即将元素从front指针所指向的位置取出。
3.将front指针向后移动一位,如果front指针已经到达队列的末尾,则将其指向队列的头部。
相关问题
建立一个循环队列,实现出队和入队操作的C语音程序
好的,以下是建立循环队列并实现入队和出队操作的C语言程序:
```
#include <stdio.h>
#define MAX_SIZE 5
int front = -1, rear = -1;
int queue[MAX_SIZE];
void enqueue(int value) {
if ((front == 0 && rear == MAX_SIZE - 1) || rear == front - 1) {
printf("队列已满,无法入队。\n");
return;
}
else if (front == -1 && rear == -1) {
front = rear = 0;
}
else if (rear == MAX_SIZE - 1) {
rear = 0;
}
else {
rear++;
}
queue[rear] = value;
printf("%d 入队成功。\n", value);
}
void dequeue() {
if (front == -1 && rear == -1) {
printf("队列为空,无法出队。\n");
return;
}
int value = queue[front];
printf("%d 出队成功。\n", value);
if (front == rear) {
front = rear = -1;
}
else if (front == MAX_SIZE - 1) {
front = 0;
}
else {
front++;
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5); // 队列已满
dequeue();
dequeue();
dequeue();
dequeue();
dequeue(); // 队列为空
return 0;
}
```
这是一个基于数组的循环队列,使用了front和rear两个指针来分别指向队首和队尾。入队时需要判断队列是否已满,如果已满则无法入队;否则将新元素赋值给rear指向的位置,并更新rear指针。出队时需要判断队列是否为空,如果为空则无法出队;否则将队首元素取出并赋值给value,将front指针向后移动一位并删除对应元素。
编写程序,实现循环队列的创建、入队和出队等基本操作算法
以下是基于数组实现的循环队列的基本操作算法,包括创建、入队和出队:
1. 创建循环队列
```C++
#define MAXSIZE 100 // 循环队列的最大容量
struct Queue {
int data[MAXSIZE];
int front; // 队列头指针
int rear; // 队列尾指针
};
void InitQueue(Queue &Q) { // 初始化队列
Q.front = Q.rear = 0;
}
```
2. 入队操作
```C++
bool EnQueue(Queue &Q, int x) { // 入队操作
if ((Q.rear + 1) % MAXSIZE == Q.front) { // 队列已满
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MAXSIZE; // 队尾指针加1
return true;
}
```
3. 出队操作
```C++
bool DeQueue(Queue &Q, int &x) { // 出队操作
if (Q.front == Q.rear) { // 队列为空
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MAXSIZE; // 队头指针加1
return true;
}
```
完整代码如下:
```C++
#include <iostream>
using namespace std;
#define MAXSIZE 100 // 循环队列的最大容量
struct Queue {
int data[MAXSIZE];
int front; // 队列头指针
int rear; // 队列尾指针
};
void InitQueue(Queue &Q) { // 初始化队列
Q.front = Q.rear = 0;
}
bool EnQueue(Queue &Q, int x) { // 入队操作
if ((Q.rear + 1) % MAXSIZE == Q.front) { // 队列已满
return false;
}
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MAXSIZE; // 队尾指针加1
return true;
}
bool DeQueue(Queue &Q, int &x) { // 出队操作
if (Q.front == Q.rear) { // 队列为空
return false;
}
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MAXSIZE; // 队头指针加1
return true;
}
int main() {
Queue Q;
InitQueue(Q);
EnQueue(Q, 1);
EnQueue(Q, 2);
EnQueue(Q, 3);
int x;
DeQueue(Q, x);
cout << x << endl; // 输出1
DeQueue(Q, x);
cout << x << endl; // 输出2
EnQueue(Q, 4);
EnQueue(Q, 5);
while (DeQueue(Q, x)) {
cout << x << " "; // 输出3 4 5
}
cout << endl;
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)