loop_queue.c 包含函数的定义
#include"loop_queue.h"
int create_queue(queue_t **my_queue){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
*my_queue=(queue_t *)malloc(sizeof(queue_t));
if(NULL==*my_queue){
printf("内存分配失败,请检查..\n");
return -1;
}
memset(*my_queue, 0, sizeof(queue_t));
return 0;
}
int is_full(queue_t *my_queue){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
if((my_queue->rear+1)%N==my_queue->front){
printf("队列已满,无法插入..\n");
return -1;
}else{
return 0;
}
}
int push_queue(queue_t *my_queue, int data){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
if(!is_full(my_queue)){
my_queue->s[my_queue->rear]=data;
my_queue->rear=(my_queue->rear+1)%N;
}else{
printf("队列已满,无法插入了奥!..\n");
return -1;
}
return 0;
}
int print_queue(queue_t *my_queue){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
int i=my_queue->front;
while(i!=my_queue->rear){
printf("%d ",my_queue->s[i]);
i=(i+1)%N;
}
printf("\n");
return 0;
}
int is_empty(queue_t *my_queue){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
if(my_queue->rear==my_queue->front){
printf("队列为空,无法删除..\n");
return -1;
}else{
return 0;
}
}
int pop_queue(queue_t *my_queue, int *data){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
if(!is_empty(my_queue)){
*data=my_queue->s[my_queue->front];
my_queue->front=(my_queue->front+1)%N;
}else{
printf("无法删除了奥..\n");
return -1;
}
}
int clean_queue(queue_t *my_queue){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
my_queue->front=my_queue->rear;
return 0;
}
int destroy_queue(queue_t **my_queue){
if(NULL==my_queue){
printf("入参为NULL,请检查..\n");
return -1;
}
free(*my_queue);
*my_queue=NULL;
return 0;
}
loop_queue.h 包含函数的声明、头文件、结构体的定义
#ifndef __LOOP_QUEUE_H__
#define __LOOP_QUEUE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5
typedef struct _Queue{
int s[N];
int front;//头
int rear;//尾
}queue_t;
int create_queue(queue_t **my_queue);//
int is_full(queue_t *my_queue);//
int push_queue(queue_t *my_queue, int data);//
int is_empty(queue_t *my_queue);//
int pop_queue(queue_t *my_queue, int *data);//
int clean_queue(queue_t *my_queue);
int destroy_queue(queue_t **my_queue);
int print_queue(queue_t *my_queue);
#endif
main.c 函数入口
#include"loop_queue.h"
int main(){
queue_t *my_queue=NULL;
create_queue(&my_queue);
//printf("%p\n",my_queue);
push_queue(my_queue, 0);
push_queue(my_queue, 1);
push_queue(my_queue, 2);
push_queue(my_queue, 3);
push_queue(my_queue, 4);
print_queue(my_queue);
int data=0;
pop_queue(my_queue, &data);
printf("出队元素:%d\n",data);
pop_queue(my_queue, &data);
printf("出队元素:%d\n",data);
print_queue(my_queue);
clean_queue(my_queue);
destroy_queue(&my_queue);
return 0;
}
结果