数组仿真队列

数组仿真队列,仅供参考:

#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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值