C++ Help

profileeclipse831
7.12C.pdf

10/18/21, 7:24 PM Chapter 7 – 2150 Fall 2021 08/23-12/15 CS_3304 22026:...

https://bb.uhd.edu/webapps/blackboard/execute/content/file?cmd=view&content_id=_4397267_1&course_id=_86914_1&framesetWrapped=true 1/1

Figure 7.12C Driver Program for Linked List Implementation of Stack

/*--------------------------------------------------------------------- Driver program to test the Stack class. ----------------------------------------------------------------------*/

#include <iostream> using namespace std; #include "LStack.h"

void print(Stack st) { st.display(cout); }

int main() { Stack s; cout << "Stack created. Empty? " << boolalpha << s.empty() << endl;

cout << "How many elements to add to the stack? "; int numItems; cin >> numItems; for (int i = 1; i <= numItems; i++) s.push(i); cout << "Stack empty? " << s.empty() << endl;

cout << "Contents of stack s (via print):\n"; print(s); cout << endl; cout << "Check that the stack wasn't modified by print:\n"; s.display(cout); cout << endl;

Stack t, u; t = u = s; cout << "Contents of stacks t and u after t = u = s (via print):\n"; cout << "u:\n"; print(u); cout << endl; cout << "t:\n"; print(t); cout << endl;

cout << "Top value in t: " << t.top() << endl;

while (!t.empty()) { cout << "Popping t: " << t.top() << endl; t.pop(); } cout << "Stack t empty? " << t.empty() << endl; cout << "\nNow try to retrieve top value from t." << endl; cout << "Top value in t: " << t.top() << endl; cout << "\nTrying to pop t: " << endl; t.pop(); }