队列:也是一个线性表(即包括顺序队列和链式队列),先进先出,但限制在两端进行插入和删除
队尾:进行存入操作的一端
队头:进行删除操作的一端
顺序队列:
//sqqueue.h #ifndef _SQ_QUEUE_H_H #define _SQ_QUEUE_H_H #define N 6 typedef int data_t; typedef struct{ data_t data[N]; int front;//指向头 int rear;//指向尾的后一个 }sqqueue; sqqueue *sqqueue_create(); int sqqueue_free(sqqueue *q); int sqqueue_in(sqqueue *q, data_t value); data_t sqqueue_out(sqqueue *q); int sqqueue_empty(sqqueue *q); int sqqueue_full(sqqueue *q); int sqqueue_clear(sqqueue *q); void sqqueue_show(sqqueue *q); #endif
//sqqueue.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "sqqueue.h" sqqueue *sqqueue_create() { sqqueue *q = (sqqueue *)malloc(sizeof(sqqueue)); if(q == NULL){ printf("malloc failed!\n"); return NULL; } memset(q->data, 0, sizeof(q->data)); q->front = 0; q->rear = 0; return q; } int sqqueue_free(sqqueue *q) { if(q == NULL){ printf("queue is NULL\n"); return -1; } free(q); } /* *ret @ 1-empty */ int sqqueue_empty(sqqueue *q) { if(q->front == q->rear){ return 1; }else{ return 0; } } /* *ret @ 1-full */ int sqqueue_full(sqqueue *q) { if((q->rear+1)%N == q->front){ return 1; }else{ return 0; } } int sqqueue_in(sqqueue *q, data_t value) { if(sqqueue_full(q)){ printf("queue is full,not in\n"); return -1; } q->data[q->rear] = value; q->rear = (q->rear+1)%N; printf("\n"); return 0; } data_t sqqueue_out(sqqueue *q) { if(sqqueue_empty(q)){ printf("queue is empty,not out\n"); return -1; } data_t data = q->data[q->front]; q->front = (q->front+1)%N; return data; } int sqqueue_clear(sqqueue *q) { return 0; } void sqqueue_show(sqqueue *q) { while(!sqqueue_empty(q)){ printf("%d\t",sqqueue_out(q)); } printf("\n"); }
//sqqueue_test.c #include <stdio.h> #include "sqqueue.h" int main() { sqqueue *q = sqqueue_create(); sqqueue_in(q, 10); sqqueue_in(q, 20); sqqueue_in(q, 30); sqqueue_show(q); sqqueue_free(q); return 0; }
标签:return,队列,sqqueue,int,printf,front,data From: https://www.cnblogs.com/zj-studyrecoding/p/17338611.html