#include #include const int MAXCHARS = 30; const int TRUE = 1; const int FALSE = 0; // class declaration // preconditions: requires that MAXCHARS, the maximum size of a name, be defined // : requires that TRUE be defined as a nonzero integer // : requires that FALSE be defined as 0 class Queue { private: struct NameRec { char name[MAXCHARS]; NameRec *nextaddr; }; NameRec *queueIn; // top-of-queue pointer NameRec *queueOut; // bottom-of-queue pointer public: Queue(); // constructor void enque(char *); char *serve(); int isempty(); }; // implementation section Queue::Queue() // constructor { queueIn = NULL; queueOut = NULL; } void Queue::enque(char *newname) { NameRec *newAddr; // a pointer to a NameRec newAddr = new (NameRec); if (newAddr == NULL) { cout << "\No more storage allocation space" << "\nThe last name has not been entered on the queue" << endl; } else { strcpy(newAddr->name, newname); // store the name newAddr->nextaddr = NULL; // set the address to NULL // empty queue checks if(queueIn != NULL) // can be replaced by if(!isempty()) queueIn->nextaddr = newAddr; if (queueOut == NULL) // can be replaced by if(isempty()) queueOut = newAddr; queueIn = newAddr; } return; } char *Queue::serve() { NameRec *tempAddr; char name[MAXCHARS]; strcpy(name, queueOut->name); // retrieve the name from queue bottom tempAddr = queueOut->nextaddr; // retrieve next address delete(queueOut); // free record's memory space queueOut = tempAddr; // update the bottom-of-queue pointer return name; } int Queue::isempty() { if (queueOut == NULL) return TRUE; else return FALSE; } int main() { char newname[MAXCHARS]; Queue namequeue; // define a Queue object cout << "Enter as many names as you wish, one per line" << "\nTo stop entering names, enter a single x\n"; while (1) { cout << "Enter a name: "; cin.getline(newname,MAXCHARS); if (strcmp(newname,"x") == 0) break; else namequeue.enque(newname); } // serve and display names from the queue cout << "\nThe names served from the queue are:\n"; while(!namequeue.isempty()) // display till end of queue { strcpy(newname, namequeue.serve()); cout << newname << endl; } return 0; }