队列(queue)的实现
队列的数组实现
#include<iostream>
using namespace std;
template<class T,int size=100>
class arrayQueue
{
public:
arrayQueue()
{
first = last = -1;
}
void enqueue(T);
T dequeque();
bool isFull()
{
return first == 0 && last == size - 1 || first == last + 1;
}
bool isEmpty()
{
return first == -1;
}
private:
int first, last;
T storage[size];
};
template<class T, int size>
void arrayQueue<T, size>::enqueue(T el)
{
if(!isFull())
if (last == size - 1 || last == -1)
{
storage[0] = el;
last = 0;
if (first == -1)
first = 0;
}
else storage[++last] = el;
else cout << "Full Queue.\n";
}
template<class T, int size>
T arrayQueue<T, size>::dequeque()
{
T tmp;
tmp = storage[first];
if (first == last)
last = first = -1;
else if (first == size - 1)
first = 0;
else first++;
return tmp;
}
队列的链表实现
#include<list>
using namespace std;
template <class T>
class lQueue
{
public:
lQueue()
{
}
void clear()
{
lst.clear();
}
bool isEmpty()
{
return lst.empty();
}
T& front()
{
return lst.front();
}
T dequeue()
{
T el = lst.front();
lst.pop_front();
return el;
}
void enqueue(const T& el)
{
lst.push_back(el);
}
private:
list<T> lst;
};
天上我才必有用,千金散尽还复来!