队列的C、C++实现

  • C
#include <stdio.h>
#include <stdlib.h>
struct Queue{
	int *data;
	int capacity;
	int front;
	int rear;
}; 

void init(struct Queue *pq,int capacity){
	pq->capacity=capacity;
	pq->data=(int*)malloc(sizeof(int)*(capacity+1));
	pq->front=pq->rear=0; 
}

int isFull(const struct Queue *pq){
	if((pq->rear +1)%(pq->capacity+1)==pq->front)	return 1;
	else return 0;
}

int isEmpty(const struct Queue *pq){
	return pq->front==pq->rear;
}

int enQueue(struct Queue *pq,int k){
	if(isFull(pq)) return 0;
	else{
		pq->data[pq->rear]=k;
		pq->rear=(pq->rear+1)%(pq->capacity+1);
		return 1;
	}
}

int deQueue(struct Queue *pq,int *px){
	if(isEmpty(pq))	return 0;
	else{
		*px=pq->data[pq->front];
		pq->front=(pq->front+1)%(pq->capacity+1);
		return 1;
	}
}

int main(){
	struct Queue q;
	init(&q,5);
	enQueue(&q,11);
	enQueue(&q,22);
	enQueue(&q,33);
	enQueue(&q,44);
	enQueue(&q,55);
	enQueue(&q,66);
	int x;
	deQueue(&q,&x);
	printf("%d\n",x);
	deQueue(&q,&x);
	printf("%d\n",x);
	deQueue(&q,&x);
	printf("%d\n",x);
	deQueue(&q,&x);
	printf("%d\n",x);
	deQueue(&q,&x);
	printf("%d\n",x);
	deQueue(&q,&x);
	printf("%d\n",x);
	return 0;
}
  • C++
#include <iostream> 
using namespace std;

struct Node{
	int data;
	Node *next;
	Node(int x){
		data=x;
		next=NULL;
	}
};
class Queue{
private:
	Node* front;
	Node* rear;
public:
	Queue(){
		front=rear=NULL;
	}
	~Queue(){
		Node* tmp;
		while(front){
			tmp=front;
			front=front->next;
			delete tmp;
		}
	}
	bool isEmpty(){
		return front==NULL;
	}
	void enQueue(int x){
		Node *tmp;
		tmp=new Node(x);
		if(isEmpty()){
			front=rear=tmp;
		}
		else{
			rear->next=tmp;
			rear=tmp;
		}
	}
	bool deQueue(int *px){
		if(isEmpty())	return false;
		else{
			*px=front->data;
			Node *tmp;
			tmp=front;
			front=front->next;
			delete tmp;
			if(front==NULL)	
				rear=NULL;
			return true;
		}
	} 
}; 

int main(){
	Queue q;
	q.enQueue(11);
	q.enQueue(22);
	q.enQueue(33);
	q.enQueue(44);
	int x;
	q.deQueue(&x);
	cout<<x<<endl;
	q.deQueue(&x);
	cout<<x<<endl;
	q.deQueue(&x);
	cout<<x<<endl;
	q.deQueue(&x);
	cout<<x<<endl;
	q.deQueue(&x);
	cout<<x<<endl;
	return 0;
}
  • C++ STL
#include <iostream>
#include <queue>
using namespace std;

int main(){
	queue<int> q;
	q.push(11);
	q.push(22);
	int x;
	x=q.front();
	cout<<x<<endl;
	q.pop();
	x=q.front();
	cout<<x<<endl;
	return 0;
}
<think>好的,用户想了解如何用C++实现队列的数据结构,需要示例代码或教程。首先,我需要回忆队列的基本概念,队列是FIFO结构,支持入队和出队操作。常见的实现方式有数组和链表。 接下来,查看用户提供的引用内容。引用[1]和[3]提到了队列C++实现,特别是引用[3]中有一个顺序队列的示例代码。不过,用户提供的引用中的代码可能存在格式问题,比如使用了&这样的HTML实体,需要转换为&符号。此外,引用中的代码可能使用了C风格的函数指针,而C++中更常用类来封装数据结构。 用户需要的是清晰的示例代码和教程步骤。因此,我应该分步骤讲解,首先用数组实现循环队列,然后用链表实现。循环队列需要注意队满和队空的判断条件,通常通过预留一个位置或者使用计数器来解决。链表实现则更灵活,无需担心容量问题。 还需要注意代码的现代C++风格,比如使用类模板、智能指针等,但考虑到用户可能更倾向于基础实现,先提供结构化的类实现,再考虑模板化。同时,要确保代码的正确性,比如指针操作、内存管理等。 最后,生成相关问题,帮助用户深入理解队列的应用和扩展,比如性能优化、线程安全等。需要确保问题与队列相关,并且能够引导用户进一步学习。</think>### C++队列实现详解 队列是先进先出(FIFO)的线性数据结构,包含入队(enqueue)和出队(dequeue)两个基本操作[^1]。以下是两种常见的实现方式: #### 一、数组实现(循环队列) ```cpp #include <iostream> using namespace std; const int MAX_SIZE = 5; class ArrayQueue { private: int data[MAX_SIZE]; int front; int rear; public: ArrayQueue() : front(0), rear(0) {} bool enqueue(int value) { if ((rear + 1) % MAX_SIZE == front) { // 队满判断 cout << "队列已满!" << endl; return false; } data[rear] = value; rear = (rear + 1) % MAX_SIZE; return true; } bool dequeue() { if (front == rear) { // 队空判断 cout << "队列为空!" << endl; return false; } front = (front + 1) % MAX_SIZE; return true; } int getFront() { if (front == rear) return -1; return data[front]; } }; // 使用示例 int main() { ArrayQueue q; q.enqueue(10); q.enqueue(20); cout << "队首元素:" << q.getFront() << endl; q.dequeue(); cout << "新队首:" << q.getFront() << endl; return 0; } ``` 关键点说明: 1. 使用模运算实现循环队列,解决假溢出问题 2. 队满条件:$(rear+1)\%MAX\_SIZE == front$ 3. 队空条件:$front == rear$[^3] #### 二、链表实现 ```cpp #include <iostream> using namespace std; struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedListQueue { private: Node* front; Node* rear; public: LinkedListQueue() : front(nullptr), rear(nullptr) {} void enqueue(int value) { Node* newNode = new Node(value); if (rear == nullptr) { front = rear = newNode; return; } rear->next = newNode; rear = newNode; } void dequeue() { if (front == nullptr) return; Node* temp = front; front = front->next; if (front == nullptr) rear = nullptr; delete temp; } int getFront() { return front ? front->data : -1; } }; ``` ### 实现步骤解析 1. **初始化**:设置队首和队尾指针 2. **入队操作**: - 数组实现:检查队列是否已满,更新rear指针 - 链表实现:创建新节点,更新尾部指针 3. **出队操作**: - 数组实现:移动front指针 - 链表实现:释放头节点内存,更新front指针 4. **边界处理**: - 数组实现需处理循环逻辑 - 链表实现需处理空队列情况 ### 性能对比 | 实现方式 | 时间复杂度 | 空间利用率 | 适用场景 | |----------|------------|------------|----------| | 数组 | O(1) | 固定容量 | 已知最大容量 | | 链表 | O(1) | 动态扩展 | 频繁插入删除 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吉大秦少游

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值