數據結構——數組模仿循環隊列

#include<stdio.h>
#include<stdlib.h>
#define MAXN 5
int queue[MAXN];
int Front=0;
int Rear=0;
/*輸入value值入隊列*/
void addqueue(int value)
{
	if (Front == 0 && (Rear + 1) % MAXN == MAXN)
	{
		printf("隊列已滿!!\n");
	}
	else if ((Rear + 1) % MAXN == Front)
	{
		printf("隊列已滿!!\n");
	}
	else
	{
		queue[Rear] = value;
		Rear = (Rear + 1) % MAXN;
	}
}
/*出隊*/
int delqueue()
{
	int temp;
	if (Front == Rear)
	{
		printf("隊列爲空!!\n");
	}
	else
	{
		temp = queue[Front];
		queue[Front] = -1;
		Front = (Front + 1) % MAXN;
		return temp;
	}
}
/*顯示隊列*/
void display()
{
	for (int i = 0; i < MAXN; i++)
	{
		printf("%-4d", queue[i]);
	}
	printf("\n");
}
/*初始化隊列*/
void init()
{
	for (int i = 0; i < MAXN; i++)
	{
		queue[i] = -1;
	}
}
int main()
{
	int select, temp;
	init();
	while (1)
	{
		scanf("%d", &select);
		switch (select)
		{
			case 1:
				scanf("%d", &temp);
				addqueue(temp);
				break;
			case 2:
				delqueue();
				break;
			case 3:
				display();
				break;
			case 4:
				return 0;
		}
	}
	display();
	system("pause");
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章