// // p.305 // Goal: with circular // #include using namespace std; struct NodeType { int info; NodeType* next; }; class QueType { NodeType* rear; public: QueType() // p.303 { rear = NULL; } void MakeEmpty() { // use rear; if ( rear != NULL ) { NodeType* t1 = rear->next; NodeType* t2 = rear->next; while( t1 !=rear ) { t1 = t1->next; delete t2; } delete rear; rear = NULL; } } ~QueType() { MakeEmpty(); } void Enqueue(int newItem) // p.304 { // empty -> add one node if( rear == NULL ) { NodeType* temp = new NodeType; temp->info = newItem; temp->next = temp; rear = temp; } else { NodeType* temp = new NodeType; temp->info = newItem; // fill the value temp->next = rear->next;// circular rear->next = temp; // enqueue to the rear rear = temp; // let rear point to the new } } void Dequeue(int& item) { // go here } void Print() const { // go here NodeType* temp = rear->next; while(temp != rear) { cout << temp->info << " "; temp = temp->next; } cout << rear->info << " "; cout << "\n"; } }; void menu(); int main() { QueType me; int item; char ch; menu(); cin >> ch; while(tolower(ch) != 'q') { if ( tolower(ch) == 'e') { cout << "\nInput the new value: "; cin >> item; me.Enqueue(item); cout << "Enqueue " << item << " successfully.\n\n"; } else if ( tolower(ch) == 'p') { cout << "\nThe queue is: " ; me.Print(); } menu(); cin >> ch; } cout << "\n\nDone.\n\n"; return 0; } void menu() { cout << "\n\n Queue with Circular List\n"; cout << "===============================\n"; cout << "E Enqueue an item.\n"; cout << "D Dequeue.\n"; cout << "P Print.\n"; cout << "Q Quit.\n"; cout << "===============================\n"; cout << "Your selection: "; }