.h文件隊列
#ifndef Queue_hit
#define Queue_hio
typedef struct Queue *Queue;queue
typedef int State;di
struct Queue {文件
int *data;data
int len;return
int *head;void
int *tai;printf
};
#include <stdio.h>
Queue initQueue(int len);
State outQueue(Queue queue);
State inQueue(Queue queue,int value);
void printQueue(Queue queue);
#endif /* Queue_h */
.c文件
#include "Queue.h"
#include <mm_malloc.h>
Queue initQueue(int len){
Queue queue=malloc(sizeof(Queue));
queue->data=malloc(sizeof(int)*(len +1));
queue->len=len;
queue->head=queue->data;
queue->tai=queue->data;
return queue;
}
State inQueue(Queue queue,int value){
if (&queue->data[queue->len+1]<=queue->tai+1) {
return -1;//隊列已滿
}else{
*queue->tai=value;
queue->tai++;
printQueue(queue);
return 0;//入隊列成功
}
}
State outQueue(Queue queue){
if (queue->head+1>queue->tai) {
queue->head--;
return -1;//隊列爲空
}else{
queue->head++;
printQueue(queue);
return 0;//出隊列成功
}
}
void printQueue(Queue queue){
int *q=queue->head;
for (; q<queue->tai; q++) {
printf("%d ",*q);
}
printf("\n");
}