一、队列链式储存的特点
在使用顺序储存创建一个队列是,此时这个队列是有限储存的,有时候无法满足我们的储存需要,而队列的链式储存则是使用单链表作为一个容器,在单链表的基础上限制了元素的进入出去的方式而形成了,队列这个暑假结构。
向队列中插入元素:
弹出队列中的一个元素:
二、队列的链式储存实现
AL_Node.h
#ifndef AL_NODE_INCLUDE
#define AL_NODE_INCLUDE
#include <windows.h>
template<typename T> class AL_QueueList;
template<typename T>
class AL_Node
{
//声明一个朋友类,使这个朋友类可以访问这个类的私有元素
friend class AL_QueueList<T>;
public:
~AL_Node();
private:
AL_Node();
AL_Node(const T &tTemplate);
AL_Node(const AL_Node<T>& cAL_Node);
T m_data;//存储数据
AL_Node *m_pPre;//记录链表的头结点地址
AL_Node *m_pNext; //指向下一个节点地址
};
template<typename T>
AL_Node<T>::AL_Node() :m_pPre(NULL), m_pNext(NULL)
{
}
template<typename T>
AL_Node<T>::AL_Node(const T &tTemplate) :m_pPre(NULL), m_pNext(NULL), m_data(tTemplate)
{
}
template<typename T>
AL_Node<T>::~AL_Node()
{
m_pPre = NULL;
m_pNext = NULL;
}
#endif // AL_NODE
AL_QueueList.h
#ifndef AL_QUEUELIST_INCLUDE
#define AL_QUEUELIST_INCLUDE
#include <windows.h>
#include "AL_Node.h"
template<typename T>
class AL_QueueList
{
public:
AL_QueueList();
~AL_QueueList();
bool IsEmpty();
bool Front(T&tTypeOut);
bool Back(T&tTypeOut);
bool Push(const T&tTempatle);
bool Pop(T&tTempatle);
DWORD Size() const;
void Clear();
private:
//队列初始节点
AL_Node<T> * m_pHeader;
DWORD m_dwSize;
//记录尾部的头部节点
AL_Node<T> * m_pFront;
//记录尾部的节点
AL_Node<T> * m_pRear;
};
template<typename T>
inline AL_QueueList<T>::AL_QueueList():m_pHeader(NULL),m_dwSize(0x00),m_pFront(NULL),m_pRear(NULL)
{
m_pHeader = new AL_Node<T>();
}
template<typename T>
inline AL_QueueList<T>::~AL_QueueList()
{
Clear();
if (m_pHeader!=NULL)
{
delete m_pHeader;
m_pHeader = NULL;
}
}
template<typename T>
inline bool AL_QueueList<T>::IsEmpty()
{
return (m_pHeader->m_pNext == NULL) ? true : false;
}
template<typename T>
inline bool AL_QueueList<T>::Front(T & tTypeOut)
{
if (IsEmpty()==true)
{
return false;
}
tTypeOut = m_pFront->m_data;
return false;
}
template<typename T>
inline bool AL_QueueList<T>::Back(T & tTypeOut)
{
if (IsEmpty()==true)
{
return false;
}
tTypeOut = m_pRear->m_data;
return true;
}
template<typename T>
inline bool AL_QueueList<T>::Push(const T & tTempatle)
{
AL_Node<T> *Instat = new AL_Node<T>();
if (Instat==NULL)
{
return false;
}
Instat->m_data = tTempatle;
if (IsEmpty()==true)
{
m_pHeader->m_pNext = Instat;
m_pFront = m_pHeader->m_pNext;
m_pRear =Instat;
}
else
{
m_pRear->m_pNext = Instat;
m_pRear = Instat;
}
m_dwSize++;
return true;
}
template<typename T>
inline bool AL_QueueList<T>::Pop(T & tTempatle)
{
if (IsEmpty()==true)
{
return false;
}
AL_Node<T> *pDelete = NULL;
pDelete = m_pFront;
if (pDelete == NULL)
{
return false;
}
m_pHeader->m_pNext = m_pFront->m_pNext;
m_pFront = m_pHeader->m_pNext;
tTempatle = pDelete->m_data;
delete pDelete;
pDelete = NULL;
m_dwSize--;
return true;
}
template<typename T>
inline DWORD AL_QueueList<T>::Size() const
{
return m_dwSize;
}
template<typename T>
inline void AL_QueueList<T>::Clear()
{
AL_Node<T> * pDelete = NULL;
while (m_pHeader->m_pNext!=NULL)
{
pDelete = m_pHeader->m_pNext;
m_pHeader->m_pNext = pDelete->m_pNext;
delete pDelete;
pDelete = NULL;
}
m_dwSize = 0x00;
}
#endif
测试代码:
void QueueListTest()
{
AL_QueueList<DWORD> clQueueList;
bool bEmpty = clQueueList.IsEmpty();
cout << bEmpty << endl;
DWORD size = clQueueList.Size();
cout << size << endl;
clQueueList.Push(123);
clQueueList.Push(456);
clQueueList.Push(789);
cout << size << endl;
DWORD p1;
clQueueList.Pop(p1);
cout << size << endl;
DWORD P2, P3;
clQueueList.Back(P2);
clQueueList.Front(P3);
cout << P2 << " " << P3<< endl;
clQueueList.Clear();
bEmpty = clQueueList.Pop(P2);
cout << size << " "<<bEmpty<<endl;
}
int main(int argc, char *argv[])
{
QueueListTest();
getchar();
return 0;
}
运行结果: