手写队列(C++)

目录

1.功能

2.功能模块

3.全局代码


1.功能

1)push,入队列

2)pop,出队列

3)front,取队首元素

4)empty,判空

5)size,队列大小

2.功能模块

1)定义结构体

//初始化
void init(Deque& D)
{
	D.data = new int[MAXSIZE];
	D.begin = 0;
	D.end = 0;
	D.length =0;
} 

2)push,入队列

//入队:push
status push_deque(Deque& D,int tmp)
{
	if(!D.data)
	{
		return ERROR;
	}
	if(D.length == MAXSIZE)
	{
		cout << "队列元素已满" << endl; 
	}
	D.data[D.end] = tmp;
	D.end++;
	D.length++;
	return OK;
}

3)pop,出队列

//出队:pop
status pop_deque(Deque& D)
{
	if(!D.data)
	{
		return ERROR;
	}
	if(D.length == 0)
	{
		cout << "队列为空" << endl; 
	}
	D.end--;
	D.length--;
	return OK;
}

4)front,取队首元素

//队头:front
int front_deque(Deque D)
{
	if(!D.data)
	{
		return 0;
	}
	if(D.length == 0)
	{
		cout << "队列为空" << endl; 
	}
	return D.data[D.begin];
}

5)empty,判空

//队空:empty
bool empty_deque(Deque D)
{
	if(!D.data)
	{
		exit(OVER);
	}
	if(D.length != 0)
	{
		return true;
	}
	return false;
}

6)size,队列大小

//队列大小:size
int size_deque(Deque D)
{
	if(!D.data)
	{
		return 0;
	}
	return D.length; 
}

 

3.全局代码

#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVER -1
#define MAXSIZE 100
typedef int status;
typedef struct deque
{
	int* data;
	int begin;
	int end;
	int length;
}Deque;
//初始化
void init(Deque& D)
{
	D.data = new int[MAXSIZE];
	D.begin = 0;
	D.end = 0;
	D.length =0;
} 
//入队:push
status push_deque(Deque& D,int tmp)
{
	if(!D.data)
	{
		return ERROR;
	}
	if(D.length == MAXSIZE)
	{
		cout << "队列元素已满" << endl; 
	}
	D.data[D.end] = tmp;
	D.end++;
	D.length++;
	return OK;
}
//出队:pop
status pop_deque(Deque& D)
{
	if(!D.data)
	{
		return ERROR;
	}
	if(D.length == 0)
	{
		cout << "队列为空" << endl; 
	}
	D.end--;
	D.length--;
	return OK;
}
//队头:front
int front_deque(Deque D)
{
	if(!D.data)
	{
		return 0;
	}
	if(D.length == 0)
	{
		cout << "队列为空" << endl; 
	}
	return D.data[D.begin];
}
//对大小:size
int size_deque(Deque D)
{
	if(!D.data)
	{
		return 0;
	}
	return D.length; 
}
//队空:empty
bool empty_deque(Deque D)
{
	if(!D.data)
	{
		exit(OVER);
	}
	if(D.length != 0)
	{
		return true;
	}
	return false;
}
int main()
{
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客1号

感谢老板,老板大气!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值