输入若干个整数, 以-1结束, 利用入队的操作生成一个循环队列, 求出该队列的长度|数据结构

输入若干个整数, 以-1结束, 利用入队的操作生成一个循环队列, 求出该队列的长度。
输入若干个整数, 以-1结束, 利用入队的操作生成一个循环队列, 求出该队列的长度|数据结构_第1张图片

#include 
#include 
#include 
#define MAX 5
typedef int datatype;

typedef struct
{
	datatype data[MAX];
	int front;
	int rear;
}Sequeue;

void main()
{
	int x;
	Sequeue *SS;
	int len;
	int EnQueue(Sequeue *sq, datatype val);
	int DeQueue(Sequeue *sq);
	void OutQueue(Sequeue *sq);
	int Length(Sequeue *sq);
	SetConsoleOutputCP(437);
	SS = (Sequeue*)malloc(sizeof(Sequeue));
	SS->front = 0;
	SS->rear = 0;
	

	printf("Please input the data!(end with -1)\n");
	scanf("%d", &x);
	while (x != -1)
	{
		if (EnQueue(SS, x))
			printf("%d Insert Sucessfully!\n",x);
		else
			printf("%d Insert Wrongly!\n",x);
		scanf("%d", &x);
	}
	printf("Initial a sequeue sucessfully!\n");
	OutQueue(SS);
	len = Length(SS);
	printf("The sequeue's length is %d\n", len);
}

int EnQueue(Sequeue *sq, datatype val)
{
	if ((sq->rear + 1) % MAX == sq->front)
	{
		printf("OverFlow!\n");
		return 0;
	}
	sq->data[sq->rear] = val;
	sq->rear = (sq->rear + 1) % MAX;
	return 1;
}

int DeQueue(Sequeue *sq)
{
	if ((sq->rear) == (sq->front))
	{
		printf("The Sequeue is Empty!\n");
		return 0;
	}
	sq->front = (sq->front + 1) % MAX;
	return 1;
}

void gotoxy(int x,int y)
{
 COORD Pos;
 HANDLE hCon;
 hCon = GetStdHandle(STD_OUTPUT_HANDLE);
 Pos.X = x;
 Pos.Y = y;
 SetConsoleCursorPosition(hCon,Pos);
}

void OutQueue(Sequeue *sq)
{
	int i = sq->front;
	printf("\t%c%c%c%c%c%c%c%c%c%c%c%c\n",196,196,196,196,196,196,196,196,196,196,196,196);
	printf("\t");
	while (i != sq->rear)
	{
		printf("%d  ", sq->data[i]);
		i = (i + 1) % MAX;
	}
	printf("\n");
	printf("\t%c%c%c%c%c%c%c%c%c%c%c%c\n",196,196,196,196,196,196,196,196,196,196,196,196);
}


int Length(Sequeue *sq)
{
	return ((sq->rear-sq->front+MAX)%MAX);
}

你可能感兴趣的:(作业)