图解
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#define N 100
#define mytype int
struct MyQueue
{
mytype data[N];//数组存储队列
int front;//拉屎
int rear;//吃东西
};
typedef struct MyQueue myQ;
//初始化
void init(myQ *p)
{
p->front = p->rear = 0;
memset(p->data,0,sizeof(mytype)*N);
}
void print(myQ *p)
{
if(isempty(p)==1)
{
return 0;
}else{
int i;
for(i=p->front;i<p->rear;i++)
{
printf("%d",p->data[i]);
}
}
}
int isfull(myQ *p)
{
if(p->rear == N)
{
return 1;
}else
{
return 0;
}
}
int isempty(myQ *p)
{
if(p->front==p->rear)
{
return 1;
}else{
return 0;
}
}
void enQ(myQ *p,mytype insertdata)
{
if(isfull(p)==1)
{
return;
}else{
p->data[p->rear] = insertdata;
p->rear+=1;
}
}
mytype deQ(myQ *p)
{
if(isempty(p)==1)
{
return 0;
}
int index = (p->rear)-(p->front);
if(index==1)
{
return p->data[0];
}else{
int i;
// printf("\n index=%d",index);
for(i=0;i<index-1;i++)
{
p->data[i] = p->data[i+1];
}
--p->rear;
}
//printf("\n index=%d",p->rear);
return p->data[p->rear];
}
mytype array_shift(myQ *p)
{
if(isempty(p)==1)
{
exit(1);
}
else{
int num = p->front;
free(p->data[num]);
return p->data[p->front++];
}
}
mytype array_pop(myQ *p)
{
if(isempty(p)==1)
{
return 0;
}else{
int num = p->rear;
free(p->data[num]);
return p->data[--p->rear];
}
}
void main()
{
myQ my1;//创建结构体
init(&my1);
enQ(&my1,1);
enQ(&my1,2);
enQ(&my1,3);
enQ(&my1,4);
printf("\npop =%d",array_pop(&my1));
printf("\nnumber=%d",array_shift(&my1));
printf("\n one=%d",array_shift(&my1));
printf("\n two = %d",array_shift(&my1));
printf("\n three = %d",array_shift(&my1));
system("pause");
}
转载于:https://blog.51cto.com/10930471/1886087