queue.h头文件
#ifndef QUEUE_H
#define QUEUE_H 1
typedef int INT32;
typedef unsigned int UINT32;
typedef unsigned char UCHAR8;
typedef char CHAR8;
typedef long long int LONG64;
typedef unsigned long long int ULONG64;
typedef double DOUBLE64;
typedef float FLOAT32;
struct Queue
{
INT32 *arr;
INT32 tail;
INT32 head;
INT32 max;
};
typedef struct Queue *Pqueue;
Pqueue Create(INT32 n);
void Pop(Pqueue q);
INT32 Front(Pqueue q);
void Push(Pqueue q,INT32 data);
INT32 Empty(Pqueue q);
INT32 Full(Pqueue q);
#endif
queue.c源文件
#include<stdio.h>
#include<stdlib.h>
#include"queue.h"
Pqueue Create(INT32 n)
{
Pqueue q = (Pqueue)malloc(sizeof(struct Queue));
if(q == NULL)
return NULL;
else
{
q->arr = (INT32 *)malloc(sizeof(INT32)*n);
q->head = 0;
q->tail = 0;
q->max = n;
}
return q;
}
INT32 Empty(Pqueue q)
{
if(q->tail == q->head)
return 1;
else
return 0;
}
INT32 Full(Pqueue q)
{
if((q->tail+1)%(q->max) == q->head)
return 1;
else
return 0;
}
void Push(Pqueue q,INT32 data)
{
if(Full(q) == 0)
q->arr[(q->tail++)%q->max] = data;
else
printf("队列已满\n");
}
void Pop(Pqueue q)
{
if(Empty(q) == 0)
q->head = (q->head+1)%(q->max);
else
printf("队列为空\n");
}
INT32 Front(Pqueue q)
{
if(Empty(q) == 0)
return q->arr[q->head];
else
{
printf("队列为空\n");
exit(0);
}
}
main测试文件
#include<stdlib.h>
#include<stdio.h>
#include"queue.h"
int main()
{
INT32 n,temp;
Pqueue q;
printf("输入要创建的队列大小\n");
scanf("%d",&n);
q = Create(n);
printf("输入队列元素:\n");
while(scanf("%d",&temp) != EOF)
{
Push(q,temp);
}
while(Empty(q) != 1)
{
printf("%d ",Front(q));
Pop(q);
}
printf("\n");
return 0;
}