C++ 简单实现队列
测试环境:windows,ubuntu、vscode,C++11
数据范围:int
思路
队列思想:先进先出
文件目录:
- main.cpp
- queue001.cpp
- queue001.h
queue001.h:
# ifndef __QUEUE__H__
# define __QUEUE__H__
# define MAXSIZE 10
//队列头部指针、尾部指针、队列内存起点、队列长度
typedef struct{
int * head;
int * front;
int * base;
int QueueSize;
}CatQueue;
class Queue{
private:
CatQueue * Q;
public:
Queue();
~Queue();
//在尾部加入元素
void queue_append(int val);、
//从头部出队元素1
void queue_pop();
//队列长度
void queue_size();
//队列判空
bool queue_isEmpty();
//队列判满
bool queue_isFull();
//打印尾部元素
void queue_front();
//打印头部元素
void queue_head();
};
# endif
功能实现
queue001.cpp:
其实跟栈实现差不多,只不过队列是从头部出队。
#include <iostream>
#include "queue001.h"
using namespace std;
//队列初始化
Queue::Queue()
{
Q = new CatQueue;
Q->base = new int [MAXSIZE];
if (!Q->base){
cout << "创建队列失败!" << endl;
return ;
}else{
Q->head = Q->base;
Q->front = Q->base;
Q->QueueSize = 0;
cout << "创建队列成功!" << endl;
}
}
//尾部加入元素
void Queue::queue_append(int val)
{
if (!Queue::queue_isFull()){
*(Q->front++) = val;
Q->QueueSize++;
}else{
cout << "队列已满!" << endl;
}
}
//头部出队
void Queue::queue_pop()
{
if (!Queue::queue_isEmpty()){
int tmp = *(Q->head);
*(Q->head++) = 0;
Q->QueueSize--;
cout << "头部元素" << tmp << "已出队!" << endl;
}else{
cout << "队列已空!" << endl;
}
}
//队列长度
void Queue::queue_size()
{
cout << "现在的队列长度为: " << Q->QueueSize << endl;
}
bool Queue::queue_isEmpty()
{
if (Q->QueueSize == 0) return true;
return false;
}
bool Queue::queue_isFull()
{
if (Q->QueueSize == MAXSIZE) return true;
return false;
}
//非空、打印头部元素
void Queue::queue_head()
{
if (!Queue::queue_isEmpty()){
cout << "头部元素为: " << *Q->head << endl;
}else{
cout << "队列空!" << endl;
}
}
//非空、打印尾部元素
void Queue::queue_front()
{
if (!Queue::queue_isEmpty()){
cout << "尾部元素为: " << *Q->front << endl;
}else{
cout << "队列空!" << endl;
}
}
//析构函数,释放内存和对象
Queue::~Queue()
{
delete []Q->base;
delete Q;
cout << "队列摧毁完成!" << endl;
}
main.cpp:
加入40、50,此时长度为2,头部元素应为40,然后执行pop,所有从头出队一个元素,头部元素应为50.
#include <iostream>
#include "queue001.h"
using namespace std;
int main()
{
Queue q;
q.queue_append(40);
q.queue_append(50);
q.queue_size();
q.queue_head();
q.queue_pop();
q.queue_head();
q.queue_size();
return 0;
}
运行实现
顺便用valgrind查查有没有内存泄漏,但感觉还是不要太过依靠valgrind的好。
root@ubuntu:/mnt/hgfs/c/queue# g++ main.cpp queue001.cpp -o main
root@ubuntu:/mnt/hgfs/c/queue# valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes ./main
==77267== Memcheck, a memory error detector
==77267== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==77267== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==77267== Command: ./main
==77267==
创建队列成功!
现在的队列长度为: 2
头部元素为: 40
头部元素40已出队!
头部元素为: 50
现在的队列长度为: 1
队列摧毁完成!
==77267==
==77267== HEAP SUMMARY:
==77267== in use at exit: 0 bytes in 0 blocks
==77267== total heap usage: 4 allocs, 4 frees, 73,800 bytes allocated
==77267==
==77267== All heap blocks were freed -- no leaks are possible
==77267==
==77267== For lists of detected and suppressed errors, rerun with: -s
==77267== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
root@ubuntu:/mnt/hgfs/c/queue#
真希望自己快点变强...