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

输入若干个整数, 以-1结束, 利用入队的操作生成一个循环队列, 求出该队列的长度。
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#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);
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值