顺序表实现队列

代码部分

cpp 复制代码
#include<iostream>
#include<stdexcept>
using namespace std;
template<typename T>
class Queue {
private:
	T* data;
	//int size;没有这个参数
	int front;
	int rear;
	int capacity;
	void resize();
public:
	Queue():data(new T[10]),front(0),rear(0),capacity(10){}
	void enqueue(T x);
	T dequeue();
	T getFront() const;//!
	int getSize() const;//!
	~Queue();
};
template<typename T>
void Queue<T>::resize() {
	T* newData = new T[capacity * 2];
	for (int i = front; i < rear; i++) {
		newData[i] = data[i];
	}
	delete[] data;
	data = newData;
	capacity *= 2;
}
template<typename T>
Queue<T>::~Queue() {
	delete[] data;
}
template<typename T>
void Queue<T>::enqueue(T x) {
	if (rear-front == capacity) {
		resize();
	}
	data[rear] = x;//!
	rear++;
}
template<typename T>
T Queue<T>::dequeue() {
	if (front == rear) {
		throw underflow_error("Queue is empty!");
	}
	front++;
	return data[front-1];//!
}
template<typename T>
T Queue<T>::getFront() const {
	if (front == rear) {
		throw underflow_error("Queue is empty!");
	}
	return data[front];
}
template<typename T>
int Queue<T>::getSize() const {
	return rear - front;
}
int main() {
	Queue<int> q;
	q.enqueue(1);
	q.enqueue(2);
	q.enqueue(3);
	q.enqueue(4);
	cout << q.dequeue()<<endl;
	cout << q.getSize()<<endl;
	return 0;
}

1.类的声明

cpp 复制代码
class Queue {
private:
	T* data;
	//int size;没有这个参数
	int front;
	int rear;
	int capacity;
	void resize();
public:
	Queue():data(new T[10]),front(0),rear(0),capacity(10){}
	void enqueue(T x);
	T dequeue();
	T getFront() const;//!
	int getSize() const;//!
	~Queue();
};

首先是队列的类的声明部分,咱们这个类名为Queue,即为队列,然后是私有权限的成员变量和函数,包括存放队列的数组,指向队列首位的指针front,指向队列最大索引位加一位置的指针rear,数组的容量capacity,以及扩容函数resize,然后是公共权限部分,包含用初始化列表初始各个成员变量的构造函数,以及入队,出队,获取队首元素、获取队列长度和析构。

2.具体函数实现

cpp 复制代码
template<typename T>
void Queue<T>::resize() {
	T* newData = new T[capacity * 2];
	for (int i = front; i < rear; i++) {
		newData[i] = data[i];
	}
	delete[] data;
	data = newData;
	capacity *= 2;
}
template<typename T>
Queue<T>::~Queue() {
	delete[] data;
}
template<typename T>
void Queue<T>::enqueue(T x) {
	if (rear-front == capacity) {
		resize();
	}
	data[rear] = x;//!
	rear++;
}
template<typename T>
T Queue<T>::dequeue() {
	if (front == rear) {
		throw underflow_error("Queue is empty!");
	}
	front++;
	return data[front-1];//!
}
template<typename T>
T Queue<T>::getFront() const {
	if (front == rear) {
		throw underflow_error("Queue is empty!");
	}
	return data[front];
}
template<typename T>
int Queue<T>::getSize() const {
	return rear - front;
}

具体函数实现部分,首先是扩容函数,经过模板声明和函数声明,与顺序表扩容原理一样,动态申请一个新的数组,把旧数组中的值传给新数组,然后删除旧数组对应的内存,然后让指针指向新的数组,同时容量更新。

cpp 复制代码
template<typename T>
void Queue<T>::resize() {
	T* newData = new T[capacity * 2];
	for (int i = front; i < rear; i++) {
		newData[i] = data[i];
	}
	delete[] data;
	data = newData;
	capacity *= 2;
}

然后是析构函数,就是将顺序表对应的内存释放即可。

cpp 复制代码
template<typename T>
Queue<T>::~Queue() {
	delete[] data;
}

然后是入队函数,经过模板声明和函数声明,函数声明部分,返回值类型为void,函数名,enqueue代表入队,然后参数列表为一个T类型的变量,代表待传入的元素,然后函数内部,首先判断数组是否还有空间,如果没有则调用扩容函数,然后在rear索引位即队列尾部插入元素,然后让rear指针向后偏移1位。

cpp 复制代码
template<typename T>
void Queue<T>::enqueue(T x) {
	if (rear-front == capacity) {
		resize();
	}
	data[rear] = x;//!
	rear++;
}

然后是出队函数,经过模板声明和函数声明,函数声明部分,返回值类型为T,函数名dequeue,即为出队,函数内部,首先判断队列是否为空,若为空则抛出异常,否则首先让头指针向后偏移一个位置,然后返回队列最前面的元素,即front-1位置的元素。

cpp 复制代码
template<typename T>
T Queue<T>::dequeue() {
	if (front == rear) {
		throw underflow_error("Queue is empty!");
	}
	front++;
	return data[front-1];//!
}

然后是获取队首元素的函数,经过模板声明和函数声明,返回值类型为T,由于队首元素为T类型,然后函数名后面加上括号,括号后面加上const,因为这个函数无需改变队列原本的值。函数实现里面,首先判断队列是否为空,若为空则抛出异常,否则返回front索引位的元素。

cpp 复制代码
template<typename T>
T Queue<T>::getFront() const {
	if (front == rear) {
		throw underflow_error("Queue is empty!");
	}
	return data[front];
}

然后是获取队列长度的函数,

cpp 复制代码
template<typename T>
int Queue<T>::getSize() const {
	return rear - front;
}

这里面跟获取队首元素一样,要加const,然后返回值类型为int,由于长度是int类型的,然后返回尾指针减去头指针,代表队列的长度。

反思

1.首先是获取队列长度和队首元素的函数,函数声明时,小括号要紧跟在函数名后面,不要写在const后面。

2.其次队列的实现没有size.

相关推荐
GuWenyue1 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法
同勉共进3 小时前
记一例 vibe coding + gcc bug 导致的线程池死锁问题
c++·线程池·gcc·死锁·vibe coding
我叫洋洋3 小时前
C ++ [ hello world ]
c语言·c++·算法
奋发向前wcx4 小时前
y1,y2总复习笔记5 2026.7.19
数据结构·笔记·算法
王维同学6 小时前
[自学][Windows C++]RunOnceEx 注册表分组键的安全遍历
c++·windows·安全·开源
战族狼魂7 小时前
高频面试题精选:分治与AI Agent架构
人工智能·算法·大模型·大语言模型
李剑一7 小时前
你用过网易的将军令嘛?它底层实现账号保护的原理相当简单粗暴
算法
Scott9999HH7 小时前
告别流量波动玄学!从法拉第电磁定律到底层 C/C++ 流量累积算法,深度解密工业级电磁流量计选型与开发
c语言·c++·算法
中达瑞和-高光谱·多光谱8 小时前
1nm到8nm光谱分辨率,你的应用该选哪款光谱相机?
算法·高光谱·高光谱相机
脱胎换骨-军哥8 小时前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust