数组仿真队列,仅供参考:
#include <stdio.h>
#include <stdlib.h>
#define N 3
int queuenum[N] = {0};
int queuehead = 0;
void queuemmi(void);
void printqueue();
void queuein(int num);
void queueout(int num);
int main(void)
{
char choose = 0;
int tempnum = 0;
while(1)
{
queuemmi();
printf("Please choose operation:\n");
scanf("%c",&choose);
system("CLS");
switch(choose)
{
case 'a':
case 'A':
{
if (queuehead == N)
{
printf("Queue is full!\n");
getch();
}
else
{
queuemmi();
printf("Please input number:\n");
scanf("%d",&tempnum);
queuein(tempnum);
system("CLS");
}
}
break;
case 'b':
case 'B':
{
if (queuehead == 0)
{
printf("Queue is empty!\n");
getch();
}
else
{
queuemmi();
queueout(tempnum);
system("CLS");
}
}
break;
case 'c':
case 'C':
{
printqueue();
getch();
system("CLS");
}
break;
case 'd':
case 'D':
{
exit(0);
}
}
}
return 0;
}
void queuemmi(void)
{
printf("*********************************************\n");
printf("* *\n");
printf("* A.IN *\n");
printf("* B.OUT *\n");
printf("* C.PRINT *\n");
printf("* D.EXIT *\n");
printf("* *\n");
printf("*********************************************\n");
}
void printqueue()
{
if (0 == queuehead)
{
printf("Queue is empty!\n");
getch();
}
else
{
int i = 0;
printf("Queue num is:\n");
for(;i < queuehead;i ++)
{
printf("%d ",queuenum[i]);
}
getch();
}
}
void queuein(int num)
{
if (queuehead == N)
{
printf("Queue is full!\n");
getch();
}
else
{
printf("%d in!\n",queuenum[queuehead ++] = num);
getch();
}
}
void queueout(int num)
{
if (0 == queuehead)
{
printf("Queue is empty!\n");
getch();
}
else
{
int i = 0;
printf("%d out!\n",queuenum[0]);
for (; i < queuehead;i ++)
{
queuenum[i] = queuenum[i+1];
}
-- queuehead;
getch();
}
}