// FILE: stacktShell.cpp // PURPOSE: Implementation of Stack class //--------------------------------------------------------------------- // COMPILE COMMAND: g++ stacktShellS.cpp //--------------------------------------------------------------------- // PROGRAMMING INSTRUCTIONS: // Use a linked list to implement the member functions of the Stack // class given below. //--------------------------------------------------------------------- #include #include #include template class Stack { public: Stack(void); Stack(const Stack &s); ~Stack(void); void push(const T &item); void pop(T &item); void pop(void); Stack &operator=(Stack &rhs); const T &top(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 *myTop; }; ///////////////////////////////////////////////////////// // Your implementation of the templated Stack class: template Stack::Stack(void) { } //*** default constructor template Stack::Stack(const Stack &s) { } //*** copy constructor template Stack::~Stack(void) { } template void Stack::push(const T &item) { } //*** push template void Stack::pop(T &item) { } //*** pop template Stack &Stack::operator=(Stack &rhs) { return rhs; } template void Stack::pop(void) { } //*** pop template bool Stack::isEmpty(void) const { return false; } //*** isEmpty template const T &Stack::top(void) const { return myTop->info; } template int Stack::length(void) const { return -77; } //*** length template void Stack::makeEmpty(void) { }//**** makeEmpty ///////////////////////////////////////////////////////////////// template void Stack::print(void) const { if ( isEmpty() ) cout << "Stack is empty\n"; else { int count(0); for ( NodeType *p = myTop; 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 &is, Student &student); ostream &operator<<(ostream &os, const Student &student); void menu(char &); void insertProc(Stack &); void removeProc(Stack &); void removeProc2(Stack &); void destroyProc(Stack &); void assignProc(Stack &); void lengthProc(const Stack &); void testTop(Stack &); void testCopyCon(Stack &); void cls(void); int main() { Stack x; char ch; do { menu(ch); switch( ch ) { case '1': insertProc(x); break; //push case '2': removeProc(x); break; //pop(item); case '3': removeProc2(x); break; //pop(); case '4': break; //** print case '5': destroyProc(x); break; case '6': testTop(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("Stack Operations on Stack Class Objects"); cls(); cout << setw(40 + title.length()/2) << title.c_str(); cout << PAD << "----------------------------------"; cout << PAD << "Push an item 1"; cout << PAD << "Pop an item 2"; cout << PAD << "Pop Stack (disregard item) 3"; cout << PAD << "Print Stack 4"; cout << PAD << "Destroy Stack 5"; cout << PAD << "Test top() 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(Stack &s) { Student data; cin >> data; s.push(data); }//*** insertProc void removeProc(Stack &s) { Student data; bool wasEmpty = s.isEmpty(); s.pop(data); if ( !wasEmpty ) cout << "Item popped = " << data << endl; }//*** removeProc void removeProc2(Stack &s) { bool wasEmpty = s.isEmpty(); s.pop(); if ( !wasEmpty ) cout << "Stack popped." << endl; }//*** removeProc2 void testTop(Stack &s) { if ( !s.isEmpty() ) cout << "s.top() = " << s.top() << endl; }//*** testTop void destroyProc(Stack &s) { s.makeEmpty(); }//*** destroyProc void reverseProc2(Stack &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.pop(v[k]); } for (int k = 0; k < SIZE; k++) { s.push( v[k] ); } cout << "State of s after reverse code: "; s.print(); }//*** reverseProc2 void lengthProc(const Stack &s) { cout << "\nStack contains " << s.length() << " elements\n"; }//*** lengthProc void testCopyCon(Stack &dummy) { string title("Testing copy constructor"); cls(); cout << setw(40 + title.length()/2) << title.c_str() << endl << endl; Stack s; s.push( Student("Ava", 3.8) ); s.push( Student("Bob", 2.7) ); s.push( Student("Cyd", 3.6) ); Stack X(s); cout << "Stack S = " ; s.print(); cout << "After Stack X(S), X = " ; X.print(); s.makeEmpty(); Stack Y(s); cout << "\nStack S = " ; s.print(); cout << "After Stack Y(S), Y = " ; Y.print(); } void assignProc(Stack &dummy) { string title("Testing overloaded operator="); cls(); cout << setw(40 + title.length()/2) << title.c_str() << endl << endl; Stack s; s.push( Student("Ava", 3.8) ); s.push( Student("Bob", 2.7) ); s.push( Student("Cyd", 3.6) ); Stack X; X = s; cout << "Stack S = "; s.print(); cout << "After X = S, X = "; X.print(); s.makeEmpty(); X = s; cout << "\nStack S = "; s.print(); cout << "After X = S, X = "; X.print(); s.push( Student("Ava", 3.8) ); s.push( Student("Bob", 2.7) ); s.push( Student("Cyd", 3.6) ); cout << "\nStack S = "; s.print(); s = s; cout << "After S = S, S = "; s.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; }