队列操作
2023-09-27 14:26:25 时间
输入格式
输入一共有 3 行,第一行为一个正整数 m(1≤m≤100),代表入队的元素个数。
第二行为 m 个整数,表示插入到队列中的元素。
第三行为一个整数 n(1≤n<m),表示执行出队操作的次数。
输出格式
输出一共有 2 行,第一行为执行完所有入队、出队操作后当前队列的队首元素。
第二行为最终队列的遍历输出结果,每两个整数之间一个空格,最后一个整数后面没有空格。
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
typedef struct Queue{
int head, tail, length;
int *data;
}Queue;
void init(Queue *q, int length) {
q->head = 0;
q->tail = -1;
q->length = length;
q->data = (int *)malloc(sizeof(int) * length);
}
int push(Queue *q, int element) {
if(q->tail + 1 >= q->length) {
return ERROR;
}
q->tail++;
q->data[q->tail] = element;
return OK;
}
void output(Queue *q) {
for (int i = q->head; i <= q->tail; i++) {
if(i != q->head) printf(" ");
printf("%d", q->data[i]);
}
printf("\n");
}
int front(Queue *q) {
return q->data[q->head];
}
void pop(Queue *q) {
q->head++;
}
int empty(Queue *q) {
return q->head > q->tail;
}
void clear(Queue *q) {
free(q->data);
free(q);
}
int main() {
Queue *queue = (Queue *)malloc(sizeof(Queue));
init(queue, 100);
int m;
int element[100];
scanf("%d",&m);
for(int i = 0; i < m; i++) {
scanf("%d",&element[i]);
push(queue,element[i]);
}
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++) {
if(!empty(queue)){
pop(queue);
}
}
printf("%d\n",front(queue));
output(queue);
return 0;
}
相关文章
- PHP+RabbitMQ消息队列的配置和使用方法【MQ】
- 各消息队列对比,Kafka深度解析,众人推荐,精彩好文!
- 使用 JavaScript 实现基本队列、优先队列和循环队列
- 测不准原理?记一次Guava队列问题的排查
- Windows迁移打印机与打印队列
- 力扣17 - 电话号码的字母组合【回溯、哈希映射、队列】
- 185、【栈与队列】leetcode ——496. 下一个更大元素 I:单调栈-哈希表(C++版本)
- RabbitMQ实现延迟队列
- 算法基础复盘笔记Day03【数据结构】—— 单链表、双链表、栈、队列、单调栈、单调队列
- 延时队列
- 线程_FIFO队列实现生产者消费者
- RabbitMQ如何保证队列里的消息99.99%被消费?