//SeqQueue.h
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int Datatype;
typedef struct SeqQueue
{
Datatype* data;
int front;
int rear;
int capacity;
}SQ;
void SeqqueueInit(SQ* ps);
void SeqqueueDestory(SQ* ps);
int SeqqueueLen(SQ* ps);
void SeqqueueCheck(SQ* ps);
void EnSeqqueue(SQ* ps,Datatype x);
void DeSeqqueue(SQ* ps);
int SeqqueueEmpty(SQ* ps);
//SeqQueue.c
#include "SeqQueue.h"
void SeqqueueInit(SQ* ps)
{
assert(ps);
ps->data = (Datatype*)malloc(sizeof(Datatype) * 4);
if (ps->data == NULL)
{
printf("失败");
exit(-1);
}
ps->front = 0;
ps->rear = 0;
ps->capacity = 4;
}
void SeqqueueDestory(SQ* ps)
{
assert(ps);
free(ps->data);
ps->data = NULL;
ps->front = ps->rear =ps->capacity= 0;
}
int SeqqueueLen(SQ* ps)
{
assert(ps);
return ps->rear - ps->front;
}
void SeqqueueCheck(SQ* ps)
{
if (ps->front == (ps->rear+1)%(ps->capacity))
{
ps->capacity = ps->capacity * 2;
ps->data = (Datatype*)realloc(ps->data, sizeof(Datatype) * ps->capacity);
if (ps->data == NULL)
{
printf("失败");
exit(-1);
}
}
}
void EnSeqqueue(SQ* ps, Datatype x)
{
assert(ps);
SeqqueueCheck(ps);
ps->data[ps->rear] = x;
ps->rear = (ps->rear + 1) % (ps->capacity);
}
void DeSeqqueue(SQ* ps)
{
assert(ps);
if (ps->front == ps->rear)
{
printf("空队列");
exit(-1);
}
ps->front = (ps->front + 1) % (ps->capacity);
}
int SeqqueueEmpty(SQ* ps)
{
assert(ps);
if (ps->front == ps->rear)
return 1;
else
return 0;
}