1. 循环队列
1.头文件
#ifndef __FUNCH__
#define __FUNCH__
#define N 10
typedef int datatype;
typedef struct
{
datatype data[N];
int front;
int rear;
}Sequeue;
Sequeue* create_sequeue(void);
void insert_sequeue(Sequeue*sq,datatype num);
void show_sequeue(Sequeue*sq);
datatype output_sequeue(Sequeue*sq);
int len_sequeue(Sequeue*sq);
void clean_sequeue(Sequeue*sq);
void free_sequeue(Sequeue**psq);
#endif
2.02_sequeue
#include <stdio.h>
#include <stdlib.h>
#include "./01_func.h"
//创建一个空的循环队列
Sequeue* create_sequeue(void)
{
Sequeue*sq=(Sequeue*)malloc(sizeof(Sequeue));
if(NULL==sq)
{
printf("创建队列失败");
return NULL;
}
sq->front=sq->rear=0;
return sq;
}
//判满
int isFull_sequeue(Sequeue*sq)
{
return (sq->rear+1)%N==sq->front? 1:0;
}
//入队
void insert_sequeue(Sequeue*sq,datatype num)
{
if(isFull_sequeue(sq)==1)
{
printf("队列为满,插入失败");
return;
}
sq->data[sq->rear]=num;
sq->rear=(sq->rear+1)%N;
return;
}
//判空
int isEmpty_sequeue(Sequeue*sq)
{
return sq->rear==sq->front?1:0;
}
//出队
datatype output_sequeue(Sequeue*sq)
{
if(isEmpty_sequeue(sq)==1)
{
printf("列表为空,出队失败");
return (datatype)-1;
}
datatype num=sq->data[sq->front];
sq->front=(sq->front+1)%N;
return num;
}
//获取队列长度
int len_sequeue(Sequeue*sq)
{
return (sq->rear-sq->front+N)%N;
}
//清空队列()
void clean_sequeue(Sequeue*sq)
{
sq->front=sq->rear=0;
return;
}
//释放队列
void free_sequeue(Sequeue**psq)
{
(*psq)->rear=(*psq)->front=0;
free(*psq);
(*psq)=NULL;
return;
}
//遍历
void show_sequeue(Sequeue*sq)
{
for(int i=sq->front;i!=sq->rear;i=(i+1)%N)
{
printf("%d ",sq->data[i]);
}
putchar(10);
return;
}
3.03_main.c
#include <stdio.h>
#include <stdlib.h>
#include "./01_func.h"
int main(int argc, const char *argv[])
{
Sequeue*sq=create_sequeue();
insert_sequeue(sq,22);
insert_sequeue(sq,33);
insert_sequeue(sq,44);
insert_sequeue(sq,55);
insert_sequeue(sq,66);
show_sequeue(sq);
output_sequeue(sq);
int len = len_sequeue(sq);
printf("%d\n",len);
// clean_sequeue(sq);
free_sequeue(&sq);
printf("%p",sq);
show_sequeue(sq);
return 0;
}