1 #include <cstddef> 2 3 struct Node{ 4 Node * next; 5 6 Node() { next = NULL;} 7 Node(const Node & node) { 8 next = node.next; 9 } 10 }; 11 12 enum Error_code { 13 succeed, 14 overflow, 15 underflow 16 }; 17 18 class LinkQueue { 19 public: 20 LinkQueue(); 21 LinkQueue(const LinkQueue & rhs); 22 ~LinkQueue(); 23 24 LinkQueue & operator=(const LinkQueue & rhs); 25 26 bool empty() const; 27 Error_code append(const Node & item); 28 Error_code serve(); 29 Error_code retrieve(Node & item) const; 30 int size() const; 31 32 protected: 33 Node * front, * rear; 34 }; 35 36 LinkQueue::LinkQueue() { 37 front = rear = NULL; 38 } 39 40 LinkQueue::~LinkQueue() { 41 while (!empty()) 42 serve(); 43 } 44 45 LinkQueue::LinkQueue(const LinkQueue & rhs) { 46 Node * new_front=NULL, * new_rear=NULL; 47 Node * new_copy, * old_node=rhs.front; 48 if (old_node != NULL) { 49 new_front=new_copy=new Node(*old_node); 50 while (old_node->next != NULL) { 51 old_node = old_node->next; 52 new_copy->next = new Node(*old_node); 53 new_copy = new_copy->next; 54 } 55 new_rear = new_copy; 56 } 57 58 front = new_front; 59 rear = new_rear; 60 } 61 62 int LinkQueue::size() const { 63 Node * window = front; 64 int count = 0; 65 66 while(window != NULL) { 67 window = window->next; 68 count++; 69 } 70 71 return count; 72 } 73 74 Error_code LinkQueue::serve() { 75 if (front == NULL) 76 return underflow; 77 78 Node * old_front = front; 79 front = front->next; 80 if (front == NULL) 81 rear = NULL; 82 83 delete old_front; 84 85 return succeed; 86 } 87 88 bool LinkQueue::empty() const{ 89 if (front == NULL) 90 return true; 91 return false; 92 } 93 94 Error_code LinkQueue::append(const Node & item) { 95 Node * new_rear = new Node(item); 96 if (new_rear == NULL) 97 return overflow; 98 if (rear == NULL) 99 front = rear = new_rear; 100 else { 101 rear->next = new_rear; 102 rear = new_rear; 103 } 104 return succeed; 105 } 106 107 Error_code LinkQueue::retrieve(Node & item) const{ 108 if (front == NULL) 109 return underflow; 110 item = *front; 111 return succeed; 112 }
LinkQueue class,布布扣,bubuko.com
原文:http://www.cnblogs.com/aoun/p/3674386.html