#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;
}
結果: