主函数:
#include <stdio.h>
#include "./03queue.h"
int main(int argc, const char *argv[])
{
queue* shoudizhi = createqueue();
Insertqueue(shoudizhi,1);
Insertqueue(shoudizhi,2);
Insertqueue(shoudizhi,3);
Insertqueue(shoudizhi,4);
Insertqueue(shoudizhi,5);
Insertqueue(shoudizhi,6);
showqueue(shoudizhi);
return 0;
}
头文件:
#ifndef __QUEUE_H__
#define __QUEUE_H__
#define N 10
typedef struct elem
{
int data[N];
int front;
int rear;
}queue;
queue* createqueue();
void Insertqueue(queue* head,int num);
void showqueue(queue* head);
#endif
函数文件:
在这里插入代码片#include <stdio.h>
#include <stdlib.h>
#include "./03queue.h"
/*
* function: 创建循环队列
* @param [ in]
* @param [out]
* @return
*/
queue* createqueue()
{
queue* head = (queue*)malloc(sizeof(queue));
if(NULL == head)
{
printf("栈区申请失败,循环队列创建失败\n");
return NULL;
}
head->front = head->rear = 0;
return head;
}
/*
* function: 插入循环队列
* @param [ in]
* @param [out]
* @return
*/
void Insertqueue(queue* temp,int num)
{
temp->data[temp->rear] = num;
temp->rear = (temp->rear+1)%N;
}
/*
* function: 循环队列遍历
* @param [ in]
* @param [out]
* @return
*/
void showqueue(queue* head)
{
int i = head->front;
printf("----------");
for(i = head->front;i!=head->rear;i=(i+1)%N)
{
printf("%d\n",head->data[i]);
}
}
头疼写不了