手写队列的实现(只写了入队与出队)
作者:互联网
#include <iostream>
using namespace std;
#define max_size 10//栈的最大长度;
int len;//队列当前的长度;
void list1();//清单;
void print();//打队列的元素,从队开始打印;
int a[max_size];
int head,tail;
int main()
{
int c;
while(1)
{
list1();
scanf("%d",&c);
switch(c)
{
case 1://push
if((tail+1)%max_size==head)
printf("队列已满:\n");
else{
printf("你需要入队的元素:\n");
scanf("%d",&a[tail]);
tail=(tail+1)%max_size;
len++;}
system("pause");
system("cls");
break;
case 2://pop:
if(tail%max_size==head)
printf("队为空!\n");
else{
printf("%d已出队\n",a[head]);
head=(head+1)%max_size;
len--;}
system("pause");
system("cls");
break;
case 3:
print();
system("pause");
system("cls");
break;
case 0:
exit(0);
}
}
}
void print()
{
int i;
for(i=head;i<tail;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
void list1()
{
printf("1.入队\n");
printf("2.出队\n");
printf("3.打印\n");
printf("4.结束\n");
}
标签:head,tail,int,max,system,入队,出队,手写,size 来源: https://blog.csdn.net/m0_56281015/article/details/117386831