栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

手写队列(C++)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写队列(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 
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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1028417.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号