Queue_I
saxiy
·
·
个人记录
#include <iostream>
using namespace std;
template<class T>
class node_Q {
public:
T data;
node_Q<T> *next;
node_Q() { next = NULL; }
node_Q(T a) { next = NULL; data = a; }
};
template<class T>
class QI {
private:
node_Q<T> *m_first, *m_last;
int nodesum;
public:
QI() { m_first = m_last = NULL; nodesum = 0; }
virtual ~QI() { clear(); }
int nodenum() { return nodesum; }
bool empty() { return nodesum == 0; }
void add_node(node_Q<T> *nd);
void push(T data) {
node_Q<T> *ls = new node_Q<T>(data);
add_node(ls);
}
void pop();
T& first() { return m_first->data; }
T& last() { return m_last->data; }
void clear();
};
template<class T>
void QI<T>::add_node(node_Q<T>* nd) {
if (empty()) m_first = m_last = nd;
else {
m_last->next = nd;
m_last = nd;
} nodesum++;
}
template<class T>
void QI<T>::pop() {
if (empty()) {
cout << " 无法完成删除操作!!\n 原因:: 队列是空的!! " << endl;
return;}
node_Q<T> *de = m_first;
m_first = de->next;
delete de;
nodesum--;
}
template<class T>
void QI<T>::clear() {
if (empty()) return;
node_Q<T> *de = m_first;
while (m_first->next != NULL) {
m_first = m_first->next;
delete de;
de = m_first;
} delete de;
nodesum = 0;
}