简单的队列

#include <stdio.h>
#include <stdlib.h>

#define ERROR 0
#define OK 1

typedef struct Queue{
    int *data;
    int length;
    int head;
    int tail;
}Queue;

void init(Queue *q, int length) {
    q->length = length;
    q->data = (int *)malloc(sizeof(int)*length);
    q->head = 0;
    q->tail = -1;
}

int push(Queue *q, int element) {
    if(q->length <= q->tail+1){
        return ERROR;
    }
    q->tail++;
    q->data[q->tail] = element;
    return OK;
}
void output(Queue *q) {
    if(q->head >q->tail){
        return;
    }
    printf("%d", q->data[q->head]);
    for(int i = q->head+1; i <= q->tail; i++){
        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 temp;
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d",&temp);
        push(queue, temp);
    }
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        if(!empty(queue)){
            pop(queue);
        }
    }
    printf("%d\n", queue->data[queue->head]);
    output(queue);
    return 0;
}

菜鸟,欢迎大佬斧正。

上一篇:Linux命令-tail


下一篇:对AQS的源码解析理解