循环队列问题
#include<stdio.h>#include<stdlib.h>
typedef struct Queue
{
int *pBase;
int front;
int rear;
}Queue;
//函数的声明
void inti(Queue *);//初始化
bool enqueue(Queue *,int val);//入队列
bool dele(Queue *);//出队列
void print(Queue *);//打印队列
bool full(Queue *);
bool empty(Queue *);
int length(Queue *);
int main()
{
int k,f,val;
Queue Q;
printf("\nplease input f to choose flowing op,-1 to end");
printf("\n1:inti(Q)");
printf("\n2:enqueue(Q)");
printf("\n3:dele(Q)");
printf("\n4:print(Q)");
scanf("%d",&f);
while(f!=-1)
{
switch (f)
{ case 1:inti(&Q);break;
case 2:printf("\nplease input val:\n");
scanf("%d",&val);
enqueue(&Q,val);break;
case 3:dele(&Q);break;
case 4:print(&Q);break;
}
printf("\nplease input f to choose flowing op,-1 to end");
printf("\n1:inti(Q)");
printf("\n2:enqueue(Q)");
printf("\n3:dele(Q,val)");
printf("\n4:print(Q)");
scanf("%d",&f);
}
return 0;
}
void inti(Queue *pQ)
{
pQ->pBase=(int *)malloc(6*sizeof(int)); //申请24个字节的,并返回第一个字节的地址,并且是指向整形数据的
pQ->front=0;
pQ->rear=0;
printf("初始化队列成功!\n");
}
bool enqueue(Queue *pQ,int val)
{
if(!full(pQ))
{
printf("队列已满,不能入队列!\n");
return false;
}else
{
pQ->pBase[pQ->rear]=val;
pQ->rear=(pQ->rear+1)%6;
printf("入队列成功!\n");
return true;
}
}
bool dele(Queue *pQ)
{
int val;
if(pQ->front==pQ->rear)
{
printf("队列为空!\n");
return false;
}
val=pQ->pBase[pQ->front];
pQ->front=(pQ->front+1)%6;;
printf("出队列的整数为%d\n",val);
return true;
}
void print(Queue *pQ)
{
if(pQ->front==pQ->rear)
{
printf("队列为空!");
}
int i = pQ->front;
while (i != pQ->rear)
{
printf("%d ", pQ->pBase[i]);
i = (i+1) % 6;
}
printf("\n");
return;
}
bool full(Queue *pQ)
{
if((pQ->rear+1)%6==pQ->front)
{
return false;
}
else
{
return true;
}
}
int length(Queue *pQ)
{
return (pQ->rear-pQ->front)%6;
}
为什么访问队尾队头的时候是这样访问 pQ->pBase[pQ->rear], pQ->pBase[pQ->front],访问元素的这样访问 pQ->pBase[i]不是很理解,还有pQ->pBase=(int *)malloc(6*sizeof(int));,这个指针变量指向哪里,谢谢!