头文件
typedef struct Node
{
int val;
Node *next;
Node(){next=NULL;}
Node(int v)
{
val = v;
next = NULL;
}
}Node;
class QueueList
{
public:
QueueList(int max);
~QueueList();
int pop();
void push(int x);
int getFront() const;
int count();
private:
int m_count;
int m_max;
Node* m_head;
Node* m_tail;
};
cpp
#include "Queue_list.h"
int QueueList::getFront()const
{
if(m_count<=0)
{
throw "QueueList is empty";
return -1;
}
else
{
return m_head->val;
}
}
int QueueList::pop()
{
if(m_count<=0)
{
throw "QueueList is empty";
return -1;
}
else
{
m_count--;
int tmp = m_head->val;
Node *node = m_head;
m_head = m_head->next;
delete node;
if(m_head==NULL)
{
m_tail = NULL;
}
return tmp;
}
}
void QueueList::push(int x)
{
if(m_count>=m_max)
{
throw "QueueList is full";
}
else
{
Node * node = new Node(x);
if(m_head == NULL)
{
m_head = node;
m_tail = node;
}
else
{
m_tail->next = node;
m_tail = node;
}
m_count++;
}
}
QueueList::QueueList(int max):m_count(0),m_head(0),m_tail(0),m_max(max)
{
}
QueueList::~QueueList()
{
Node *node;
while(m_head != NULL)
{
node = m_head->next;
delete m_head;
m_head = node;
}
}
int QueueList::count()
{
return m_count;
}