#include<iostream>
using namespace std;
enum Error_code { success, fail, range_error, underflow, overflow, fatal, not_present, duplicate_error, entry_inserted, entry_found, internal_error };
struct Node { //结构体
Node_entry entry; //数据域
Node *next; //指针域
Node(); //构造函数
Node(Node_entry item,Node *add_on = NULL); //带参数的构造函数
};
Node::Node()
{
next = NULL;
}
Node::Node(Node_entry item, Node *add_on)
{
entry = item;
next = add_on;
}
class Queue {
public:
Queue(); //构造函数
bool empty() const; //判断是否为空
Error_code append(const Queue_entry &item); //插入一个元素
Error_code serve(); //删除一个元素
Error_code retrieve(Queue_entry &item) const; //读取队头元素
~Queue(); //析构函数
Queue(const Queue &original); //拷贝构造函数
void operator =(const Queue &original); //运算符重载
protected:
Node *front, *rear; //指向队列的头和尾
};
Queue::Queue() //构造函数
{
front = rear = NULL; //头和尾都制空
}
bool Queue::empty() const //判断是否为空
{
return front == NULL; //只要判断头指针是否为空即可
}
Error_code Queue::append(const Queue_entry &item) //插入一个元素
{
Node *new_rear = new Node(item); //创建一个新链表并将item值插入
if (new_rear == NULL) return overflow; //判断链表空间是否足够
if (rear == NULL) front = rear = new_rear; //如果插入的是链表的第一个元素,则front也要指向新元素
else {
rear->next = new_rear; //如果插入的不是表头元素,则让原链表的尾指针指向新的数据域
rear = new_rear; //rear指向链表尾
}
return success;
}
Error_code Queue::serve() //删除一个元素
{
if (front == NULL) return underflow; //判断链表中是否有元素
Node *old_front = front; //设一个新的链表指向原链表的头
front = old_front->next; //原链表向后移一位
if (front == NULL) rear = NULL; //如果移位后没有元素,说明链表为空,那么rear也要为空
delete old_front; //删除掉元素
return success;
}
Error_code Queue::retrieve(Queue_entry &item) const //取队头元素
{
if (front == NULL) return underflow; //判断链表中是否有元素
item = front->entry; //如果有则将队头元素赋给item
return success;
}
Queue::~Queue() //析构函数
{
while(!empty()) serve(); //遍历,只要链表不空就删掉元素
}
Queue::Queue(const Queue ©) //拷贝构造函数,传入原链表
{
Node *copy_node = copy.front; //设一个新链表指向原链表的头
front = rear = NULL;
while(copy_node != NULL) //遍历,将原链表的每个元素都赋给新链表
{
append(copy_node->entry);
copy_node = copy_node->next;
}
}
void Queue::operator =(const Queue ©) //"="重载
{
while(!empty()) serve(); //先删除掉原有链表中的数据
Node *copy_node = copy.front; //设一个新链表指向原链表的头
while(copy_node != NULL) //遍历,将原链表的每个元素都赋给新链表
{
append(copy_node->entry);
copy_node = copy_node->next;
}
}
扩展:
class Extended_queue: public Queue {
public:
bool full() const; //判断链表是否已满
int size() const; //求链表的大小
void clear(); //清空链表
Error_code serve_and_retrieve(Queue_entry &item); //取出头元素并删除
};
bool Extended_queue:: full() const //判断链表是否已满
{
return false; //返回理想结果
}
int Extended_queue::size() const //求链表的大小
{
Node *window = front; //定义一个新链表指向要检测的链表头
int count = 0;
while (window != NULL) { //遍历
window = window->next;
count++;
}
return count;
}
void Extended_queue::clear() //清空链表
{
while(!empty()) serve(); //遍历,有元素时就删除
}
Error_code Extended_queue::serve_and_retrieve(Queue_entry &item) //取出头元素并删除
{
retrieve(item);
serve();
}
简单的运用main.cpp:
typedef int Node_entry;
typedef int Queue_entry;
#include "chainqueue.cpp"
int chainqueuemain()
{
Queue chain1;
bool isempty;
int item;
chain1.append(3);
chain1.retrieve(item);
cout << item << endl;
chain1.append(4);
chain1.append(5);
chain1.retrieve(item);
cout << item << endl;
isempty = chain1.empty();
cout << isempty << endl;
chain1.serve();
chain1.retrieve(item);
cout << item << endl;
}
结果: