// FILE: queuetShell.cpp // PURPOSE: Implementation of Queue class //--------------------------------------------------------------------- // COMPILE COMMAND: g++ queuetShell.cpp //--------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Use a linked list to implement the member functions of the Stack // class given below. //--------------------------------------------------------------------- #include #include #include template class Queue { public: Queue(void); Queue(const Queue &s); ~Queue(void); void enqueue(const T &item); void dequeue(T &item); void dequeue(void); Queue &operator=(Queue &rhs); const T &front(void) const; bool isEmpty(void) const; int length(void) const; void print(void) const; void makeEmpty(void); private: struct NodeType { T info; NodeType *next; }; NodeType *myFront, *myRear; }; ///////////////////////////////////////////////////////// // Your implementation of the templated Queue class: template Queue::Queue(void) { } //*** default constructor template Queue::Queue(const Queue &q) { } //*** copy constructor template Queue::~Queue(void) { } template void Queue::enqueue(const T &item) { } //*** enqueue template void Queue::dequeue(T &item) { } //*** dequeue template Queue &Queue::operator=(Queue &rhs) { return rhs; } template void Queue::dequeue(void) { } //*** dequeue template bool Queue::isEmpty(void) const { return false; } //*** isEmpty template const T &Queue::front(void) const { } template int Queue::length(void) const { return -77; } //*** length template void Queue::makeEmpty(void) { }//**** makeEmpty template void Queue::print(void) const { if ( isEmpty() ) cout << "Queue is empty\n"; else { int count(0); for ( NodeType *p = myFront; p != NULL; p = p->next) { count++; cout << setw(4) << count << ". " << p->info; } cout << endl; } } /////////////////////////////////////////////////////////// struct Student { Student(void); Student(string name2, double gpa2); string name; double gpa; }; istream &operator>>(istream &, Student &); ostream &operator<<(ostream &, const Student &); void menu(char &); void insertProc(Queue &); void removeProc(Queue &); void removeProc2(Queue &); void destroyProc(Queue &); void assignProc(Queue &); void lengthProc(const Queue &); void testFront(Queue &); void testCopyCon(Queue &); void cls(void); int main() { Queue x; char ch; do { menu(ch); switch( ch ) { case '1': insertProc(x); break; //enqueue case '2': removeProc(x); break; //dequeue(item); case '3': removeProc2(x); break; //dequeue(); case '4': break; //** print case '5': destroyProc(x); break; case '6': testFront(x); break; case '7': lengthProc(x); break; case '8': testCopyCon(x); break; case '9': assignProc(x); break; case '0': return 0; default: break; } // switch cout << "\n\n"; x.print(); cout << "\nPress : "; cin.ignore(80, '\n'); } while (ch != '0'); return 0; } void menu(char &choice) { const int left = 22; const string PAD('\n' + string(left, ' ')); string title("Queue Operations on Queue Class Objects"); cls(); cout << setw(40 + title.length()/2) << title.c_str();; cout << PAD << "----------------------------------"; cout << PAD << "Enqueue an item 1"; cout << PAD << "Dequeue an item 2"; cout << PAD << "Dequeue Queue (disregard item) 3"; cout << PAD << "Print Queue 4"; cout << PAD << "Destroy Queue 5"; cout << PAD << "Test front() member function 6"; cout << PAD << "Length (# of elements) 7"; cout << PAD << "Test copy constructor 8"; cout << PAD << "Test overloaded = operator 9"; cout << PAD << "Quit program 0"; cout << PAD << "----------------------------------"; cout << PAD << " SELECTION==> "; cin.get(choice); cin.ignore(80, '\n'); while ( choice < '0' || choice > '9' ) { cout << PAD << "*** Invalid choice. Try again: "; cin.get(choice); cin.ignore(80, '\n'); }// while } //*** menu void insertProc(Queue &s) { Student data; cin >> data; s.enqueue(data); }//*** insertProc void removeProc(Queue &s) { Student data; bool wasEmpty = s.isEmpty(); s.dequeue(data); if ( !wasEmpty ) cout << "Item dequeued = " << data << endl; }//*** removeProc void removeProc2(Queue &s) { bool wasEmpty = s.isEmpty(); s.dequeue(); if ( !wasEmpty ) cout << "Queue dequeued." << endl; }//*** removeProc2 void testFront(Queue &s) { if ( !s.isEmpty() ) cout << "q.front() = " << s.front() << endl; }//*** testFront void destroyProc(Queue &s) { s.makeEmpty(); }//*** destroyProc void reverseProc2(Queue &s) { cout << "State of s before reverse code: "; s.print(); const int SIZE(s.length()); Student tmp; vector v(SIZE); for (int k = 0; k < SIZE; k++) { s.dequeue(v[k]); } for (int k = 0; k < SIZE; k++) { s.enqueue( v[k] ); } cout << "State of s after reverse code: "; s.print(); }//*** reverseProc2 void lengthProc(const Queue &s) { cout << "\nQueue contains " << s.length() << " elements\n"; }//*** lengthProc void testCopyCon(Queue &Q) { string title("Testing copy constructor"); cls(); cout << setw(40 + title.length()/2) << title.c_str() << endl << endl; Queue q; q.enqueue( Student("Ava", 3.8) ); //Q.enqueue( Student("Ava", 3.8) ); q.enqueue( Student("Bob", 2.7) ); //Q.enqueue( Student("Bob", 2.7) ); q.enqueue( Student("Cyd", 3.6) ); //Q.enqueue( Student("Cyd", 3.6) ); Queue X(q); cout << "Queue S = "; q.print(); cout << "After Queue X(S), X = "; X.print(); q.makeEmpty(); Queue Y(q); cout << "\nQueue S = "; q.print(); cout << "After Queue Y(S), Y = " ; Y.print(); } void assignProc(Queue &dummy) { string title("Testing overloaded operator="); cls(); cout << setw(40 + title.length()/2) << title.c_str() << endl << endl; Queue q; q.enqueue( Student("Ava", 3.8) ); q.enqueue( Student("Bob", 2.7) ); q.enqueue( Student("Cyd", 3.6) ); Queue X; X = q; cout << "Queue Q = "; q.print(); cout << "After X = Q, X = "; X.print(); q.makeEmpty(); X = q; cout << "\nQueue Q = "; q.print(); cout << "After X = Q, X = "; X.print(); q.enqueue( Student("Ava", 3.8) ); q.enqueue( Student("Bob", 2.7) ); q.enqueue( Student("Cyd", 3.6) ); cout << "\nQueue Q = "; q.print(); q = q; cout << "After Q = Q, Q = "; q.print(); }//*** assignProc void cls(void) { for (int k = 0; k < 55; k++) cout << endl; } ////////////////////////////////////////// // Implementation of Student class ////////////////////////////////////////// Student::Student(void) : name("New record"), gpa(-9.9) {} Student::Student(string name2, double gpa2) : name(name2), gpa(gpa2) {} istream &operator>>(istream &is, Student &student) { cout << "\nEnter student name: "; is >> student.name; cout << "Enter student GPA: "; is >> student.gpa; is.ignore(10, '\n'); return is; } ostream &operator<<(ostream &os, const Student &student) { os << student.name << " " << student.gpa; return os; }