C++实现一个简单的链表队列
/**---------------------------------------
* 文件名称: CLinkQueue.h
* 功能描述: 链表队列实现
* 创建标识: xad 2022/2/9
*
* 修改标识:
* 修改描述:
------------------------------------------**/
#ifndef LINKQUEUE_LINKQUEUE_H
#define LINKQUEUE_LINKQUEUE_H
namespace Algo02
{
typedef struct stuLinkNode
{
struct stuLinkNode* _pNextNode; //队列结点
}LinkNode;
class CLinkQueue
{
private:
LinkNode* _pHead; //头结点
int _nSize; //队列元素个数
public:
CLinkQueue();
~CLinkQueue();
/**
* @brief 入栈操作
*
* @param LinkNode * data
* @return void
*/
void push_linkQueue(LinkNode* data);
/**
* @brief 返回队头元素
*
* @return void*
*/
void* front_linkQueue() const;
/**
* @brief 出队
*
* @return void
*/
void pop_linkQueue();
/**
* @brief 返回队尾元素
*
* @return void*
*/
void* back_linkQueue();
/**
* @brief 返回大小
*
* @return int
*/
int get_size() const;
/**
* @brief 清空队列
*
* @return void
*/
void clear_linkQueue();
};
}
#endif
#include "CLinkQueue.h"
#include <iostream>
const int MATH_ZREO = 0;
Algo02::CLinkQueue::CLinkQueue()
{
_pHead = new LinkNode;
_pHead->_pNextNode = nullptr;
_nSize = 0;
}
Algo02::CLinkQueue::~CLinkQueue()
{
if (_pHead == nullptr)
{
return;
}
auto* pCurrent = _pHead;
while (pCurrent!= nullptr)
{
auto* pNext = pCurrent->_pNextNode;
delete pCurrent;
pCurrent = pNext;
}
std::cout << "free ClinkQueue" << std::endl;
}
void Algo02::CLinkQueue::push_linkQueue(LinkNode * data)
{
if (data == nullptr)
{
return;
}
if (_nSize == MATH_ZREO)
{
_pHead->_pNextNode = data;
data->_pNextNode = nullptr;
_nSize++;
return;
}
auto * pCurrent = _pHead;
for (int i = 0; i < _nSize;i++)
{
pCurrent = pCurrent->_pNextNode;
}
pCurrent->_pNextNode = data;
data->_pNextNode = nullptr;
_nSize++;
}
void * Algo02::CLinkQueue::front_linkQueue() const
{
if (_pHead == nullptr)
{
return nullptr;
}
return _pHead->_pNextNode;
}
void Algo02::CLinkQueue::pop_linkQueue()
{
if (_pHead == nullptr) {
return;
}
LinkNode *pNext = _pHead->_pNextNode;
_pHead->_pNextNode = pNext->_pNextNode;
_nSize--;
}
void * Algo02::CLinkQueue::back_linkQueue()
{
if (_pHead == nullptr)
{
return nullptr;
}
auto* pCurrent = _pHead;
for (int i = 0; i < _nSize;i++)
{
pCurrent = pCurrent->_pNextNode;
}
return pCurrent;
}
int Algo02::CLinkQueue::get_size() const
{
return _nSize;
}
void Algo02::CLinkQueue::clear_linkQueue()
{
if (_pHead == nullptr)
{
return;
}
auto* pCurrent = _pHead;
while (pCurrent != nullptr)
{
auto* pNext = pCurrent->_pNextNode;
delete pCurrent;
pCurrent = pNext;
}
_pHead->_pNextNode = nullptr;
}
#include <iostream>
#include "CLinkQueue.h"
#include <memory>
using namespace std;
using namespace Algo02;
typedef struct PERSON {
LinkNode node;
char name[64];
int age;
}Person;
void test_03(void);
int main()
{
test_03();
system("pause");
return 0;
}
void test_03(void)
{
// 创建队列
CLinkQueue queue;
// 创建数据
Person p1, p2, p3, p4, p5;
strcpy_s(p1.name, "aaa");
strcpy_s(p2.name, "bbb");
strcpy_s(p3.name, "ccc");
strcpy_s(p4.name, "ddd");
strcpy_s(p5.name, "eee");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
// 数据入队列
queue.push_linkQueue((LinkNode*)&p1);
queue.push_linkQueue((LinkNode*)&p2);
queue.push_linkQueue((LinkNode*)&p3);
queue.push_linkQueue((LinkNode*)&p4);
queue.push_linkQueue((LinkNode*)&p5);
// 输出队尾元素
auto backPerson = (Person*)queue.back_linkQueue();
std::cout << "Name:" << backPerson->name << " Age:" << backPerson->age << std::endl;
// 输出
while (queue.get_size() > 0) {
// 取队头元素
auto p = (Person*)queue.front_linkQueue();
std::cout << "Name:" << p->name << " Age:" << p->age << std::endl;
// 从队头弹出元素
queue.pop_linkQueue();
}
// 自动调用析构函数
}