c++ array判断为空_循环队列(C实现)

思想:

1.了解“”假溢出“”现象

2.利用循环队列解决

注:循环队列判满时,要利用一个没有元素的空间。所以分配空间时要在多申请一个空间,即使用空间+1。(代码注释里有)

可以借鉴一下别人写的,思路很清稀。

现世安稳:循环队列(C语言)​zhuanlan.zhihu.com
3ed9c324199697c3dcbb244f91b6d441.png

代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct QueueRecord {
    int Capacity;
    int Front;
    int Rear;
    int* array;
}*Queue;
int IsEmpty(Queue Q);//判断队是否为空
int IsFull(Queue Q);//判断队是否已满
Queue InitQueue(int ElementDigits);//初始化队列
void Destroy(Queue* Q);//销毁队列
void Enqueue(Queue Q);//入队
int Dequeue(Queue Q);//出队
//判断队列是否为空
int IsEmpty(Queue Q) {
    return Q->Rear == Q->Front;
}
//判断队列是否已满
int IsFull(Queue Q) {
    return (Q->Rear+1)%Q->Capacity == Q->Front;
}
//初始化队列
Queue InitQueue(int Size) {
    Queue Q;
    Q = (Queue)malloc(sizeof(struct QueueRecord));
    if (Q == NULL) {
        printf("out of space");
        return 0;
    }
    Q->Capacity = Size + 1;//多申请一个空间用来判断队列是否已满
    Q->array = (int*)malloc(sizeof(int) * Q->Capacity);
    if (Q->array == NULL) {
        printf("out of space");
        return 0;
    }
    Q->Rear = 0;
    Q->Front = 0;
    return Q;
}
//入队
void Enqueue(Queue Q,int element) {
    if (IsFull(Q)) {
        printf("stack is full");
    }
    else {
        Q->array[Q->Rear] = element;
        Q->Rear = (Q->Rear++) % Q->Capacity;
    }
}

//出队
int DeQueue(Queue Q) {
    int temp;
    if (IsEmpty(Q)) {
        printf("队列以空");
        return 0;
    }
    else {
        temp = Q->array[Q->Front];
        Q->Front = (Q->Front++) % Q->Capacity;
        return temp;
    }
}
//销毁队列
void Destroy(Queue* Q)
{
    free(Q);
    printf("队列已销毁!n");
}
int main() {
    Queue Q;
    int num;
    int e;
    printf("输入入队的个数:");
    scanf_s("%d", &num);
    Q = InitQueue(num);
    printf("输入入队元素:");
    for (int i = 0; i < num; i++) {
        scanf_s("%d",&e);
        Enqueue(Q, e);
        if (IsFull(Q))
            printf("队列已满n");
    }
    for (int j = 0; j < num; j++) {
        printf("%d出队n",DeQueue(Q));
        if (IsEmpty(Q))
            printf("队列以空n");
    }
    Destroy(Q);
    return 0;
}

结果:

99c200591a7b18093c827eb90afc1b09.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我可以给你一个简单的C 类的例子:#include <stdio.h>typedef struct CircularQueue { int maxSize; int front; int rear; int *array; } CircularQueue;// 创建循环队列 CircularQueue *createCircularQueue(int maxSize) { CircularQueue *queue = (CircularQueue *)malloc(sizeof(CircularQueue)); queue->maxSize = maxSize; queue->front = 0; queue->rear = 0; queue->array = (int *)malloc(sizeof(int) * maxSize); return queue; }// 判断循环队列是否为 int isCircularQueueEmpty(CircularQueue *queue) { if (queue->front == queue->rear) { return 1; } return 0; }// 判断循环队列是否为满 int isCircularQueueFull(CircularQueue *queue) { if ((queue->rear + 1) % queue->maxSize == queue->front) { return 1; } return 0; }// 向循环队列中添加元素 void enCircularQueue(CircularQueue *queue, int element) { if (isCircularQueueFull(queue)) { printf("The queue is full!\n"); return; } queue->array[queue->rear] = element; queue->rear = (queue->rear + 1) % queue->maxSize; }// 从循环队列中取出元素 int deCircularQueue(CircularQueue *queue) { if (isCircularQueueEmpty(queue)) { printf("The queue is empty!\n"); return -1; } int element = queue->array[queue->front]; queue->front = (queue->front + 1) % queue->maxSize; return element; } ### 回答2: 循环队列是一种具有固定大小的队列数据结构,其中队尾可以连接到队首的环形结构。以下是一个使用C语言编写的循环队列类的示例。 ```c #include <stdio.h> #include <stdlib.h> typedef struct { int* elements; int front; int rear; // 队尾的下一个位置 int maxSize; } Queue; // 创建循环队列 Queue* createQueue(int maxSize) { Queue* queue = (Queue*)malloc(sizeof(Queue)); queue->elements = (int*)malloc(sizeof(int) * maxSize); queue->front = 0; queue->rear = 0; queue->maxSize = maxSize; return queue; } // 判断队列是否为 int isEmpty(Queue* queue) { return (queue->rear == queue->front); } // 判断队列是否已满 int isFull(Queue* queue) { return ((queue->rear + 1) % queue->maxSize == queue->front); } // 入队操作 void enqueue(Queue* queue, int item) { if (isFull(queue)) { printf("队列已满,无法入队。\n"); return; } queue->elements[queue->rear] = item; queue->rear = (queue->rear + 1) % queue->maxSize; } // 出队操作 int dequeue(Queue* queue) { if (isEmpty(queue)) { printf("队列,无法出队。\n"); return -1; } int item = queue->elements[queue->front]; queue->front = (queue->front + 1) % queue->maxSize; return item; } // 获取队列元素个数 int getSize(Queue* queue) { return (queue->rear - queue->front + queue->maxSize) % queue->maxSize; } // 打印队列元素 void printQueue(Queue* queue) { if (isEmpty(queue)) { printf("队列。\n"); return; } printf("队列元素为:"); int i = queue->front; while (i != queue->rear) { printf("%d ", queue->elements[i]); i = (i + 1) % queue->maxSize; } printf("\n"); } // 释放队列内存 void freeQueue(Queue* queue) { free(queue->elements); free(queue); } int main() { Queue* queue = createQueue(5); enqueue(queue, 1); enqueue(queue, 2); enqueue(queue, 3); enqueue(queue, 4); enqueue(queue, 5); enqueue(queue, 6); // 队列已满 printQueue(queue); // 输出队列元素为:1 2 3 4 5 printf("出队元素:%d\n", dequeue(queue)); // 输出出队元素:1 printf("出队元素:%d\n", dequeue(queue)); // 输出出队元素:2 printQueue(queue); // 输出队列元素为:3 4 5 freeQueue(queue); return 0; } ``` 以上是一个简单的循环队列类的实现,包括创建队列判断队列是否为判断队列是否已满、入队操作、出队操作、获取队列元素个数、打印队列元素、释放队列内存等功能。你可以根据具体需求对代码进行修改和扩展。 ### 回答3: 循环队列是一种特殊的队列数据结构,它的优点是可以有效地利用队列间,避免数据搬迁带来的额外开销。下面是一个简单的循环队列C++类示例: ```c++ #include <iostream> using namespace std; const int MAX_SIZE = 100; // 循环队列的最大容量 class CircularQueue { private: int queue[MAX_SIZE]; // 数组实现队列 int front; // 队首索引 int rear; // 队尾索引 public: CircularQueue() { // 初始化队列 front = 0; rear = 0; } void enqueue(int item) { // 入队操作 if ((rear + 1) % MAX_SIZE == front) { // 队列已满,无法入队 cout << "队列已满,无法入队!" << endl; return; } queue[rear] = item; rear = (rear + 1) % MAX_SIZE; } void dequeue() { // 出队操作 if (front == rear) { // 队列,无法出队 cout << "队列,无法出队!" << endl; return; } front = (front + 1) % MAX_SIZE; } int getFront() { // 获取队首元素 if (front == rear) { // 队列,返回-1表示获取失败 return -1; } return queue[front]; } bool isEmpty() { // 判断队列是否为 return front == rear; } bool isFull() { // 判断队列是否满了 return (rear + 1) % MAX_SIZE == front; } }; int main() { CircularQueue q; q.enqueue(1); q.enqueue(2); q.enqueue(3); cout << q.getFront() << endl; // 输出1 q.dequeue(); q.dequeue(); cout << q.isEmpty() << endl; // 输出0,队列不为 q.dequeue(); cout << q.isEmpty() << endl; // 输出1,队列 return 0; } ``` 这个循环队列类包含了入队、出队、获取队首元素、判断队列是否为判断队列是否满了的操作。你可以根据需要进行调用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值