队列的数组实现
程序员文章站
2022-03-08 09:17:39
...
typedef struct QNode *Queue;
struct QNode
{
int *data;
int front;//头指针
int rear;//尾指针;
int size;//当前数组大小
};
int Scu(int value,int Maxsize)
{
if (++value == Maxsize)
value = 0;
return value;
}
Queue
creatQueue(int Maxsize)
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->data= (int*)malloc(Maxsize * sizeof(int));
Q->size= 0;
Q->front= 0;
Q->rear= 0;
return Q;
}
bool
IsfullQ(Queue Q,int Maxsize) //判断队列是否满
{
if (Q->size == Maxsize)
{
return true;
}
return false;
}
bool
IsEmptyQ(Queue Q) //判断队列是否空
{
if (Q->size == 0)
{
return true;
}
return false;
}
void AddQ(Queue Q,int item,int Maxsize) //向队列加入一个元素
{
if (IsfullQ(Q,Maxsize))
{
printf("队列已满\n");
return;
}
Q->rear = Scu(Q->rear,Maxsize);
Q->data[Q->rear] = item;
Q->size++;
}
int DelQ(Queue Q,int Maxsize)
{
if (IsEmptyQ(Q))
{
printf("队列为空\n");
return -1;
}
Q->size--;
Q->front = Scu(Q->front,Maxsize);
return Q->data[Q->front];
}
上一篇: 手写call,apply,bind
下一篇: Ubuntu添加新用户,修改密码