队列的顺序存储

实现的队列的顺序存储,直接附上代码:

实现功能:

1.初始化

2.输入

3.输出

4.判断队列是不是为空

5.判断队列非满

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#define MAXSIZE 1
typedef struct{
	int iData[MAXSIZE];
	int front, rear;
	int num;
}sQueue;
// 初始化
int initQueue(sQueue *(&queue))
{
	queue = (sQueue *)malloc(sizeof(sQueue)); //申请空间
	if(queue == NULL) return 0; //返回失败
	else
	{
		queue->front = MAXSIZE;
		queue->rear = MAXSIZE;
		queue->num = 0;
		return 1; //返回成功
	}
}
int inputQueue(sQueue *(&queue), int x)
{
	if(queue->num == MAXSIZE)
	{
		printf("输入队列已满\n");
		system("pause");
		return 0;
	}
	else
	{
		//队列插入元素
		queue->rear = (queue->rear+1)%MAXSIZE;
		queue->iData[queue->rear] = x;
		queue->num++;
		return 1;
	}
}
int outputQueue(sQueue *queue, int x)
{
	if(queue->num == 0)
	{
		printf("队空\n");
		return 0; //输出失败
	}
	else
	{
		queue->front = (queue->front+1)%MAXSIZE;
		x = queue->iData[queue->front];
		queue->num--;
		return 1;//输出成功
	}
}
int emptyQueue(sQueue *queue)
{
	if(queue->num == 0)
	{
		printf("队空\n");
		return 1;//队空
	}
	return 0;//队有元素
}
int maxQueue(sQueue *queue)
{
	queue->rear = (queue->rear+1)%MAXSIZE;
	if(queue->front == queue->rear)
	{
		printf("队满,插入失败\n");
		system("pause");
		return 1;
	}
	queue->rear = (queue->rear-1)%MAXSIZE;
	return 0;//队非满
}

函数中使用局部变量不能被赋值,如果要将mian方法中的变量,在外函数中成功赋值,需要的是它的地址,或者是使用引用。

上面的代码只实现的是结果的输出没有实现节点移动,下面给出代码:

typedef struct{
	int iData[MAXSIZE];
	int front, rear;
	int num;
}sQueue;
// 初始化
int initQueue(sQueue *(&queue))
{
	queue = (sQueue *)malloc(sizeof(sQueue)); //申请空间
	if(queue == NULL) return 0; //返回失败
	else
	{
		queue->front = MAXSIZE;
		queue->rear = MAXSIZE;
		queue->num = 0;
		return 1; //返回成功
	}
}
int inputQueue(sQueue *(&queue), int &x)
{
	if(queue->num == MAXSIZE)
	{
		printf("输入队列已满\n");
		system("pause");
		return 0;
	}
	else
	{
		//队列插入元素
		queue->rear = (queue->rear+1)%MAXSIZE;
		queue->iData[queue->rear] = x;
		queue->num++;
		return 1;
	}
}
int outputQueue(sQueue *(&queue), int &x)
{
	if(queue->num == 0)
	{
		printf("队空\n");
		queue->front = MAXSIZE;
		queue->rear = MAXSIZE;
		return 0; //输出失败
	}
	else
	{
		queue->front = (queue->front+1)%MAXSIZE;
		x = queue->iData[queue->front];
		queue->num--;
		return 1;//输出成功
	}
}
int emptyQueue(sQueue *queue)
{
	if(queue->num == 0)
	{
		printf("队空\n");
		return 1;//队空
	}
	return 0;//队有元素
}
int maxQueue(sQueue *queue)
{
	if(queue->front == (queue->rear+1)%MAXSIZE && queue->front == MAXSIZE)
	{
		printf("队满,插入失败\n");
		system("pause");
		return 1;
	}
	return 0;//队非满
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值