p2.cpp - Collin deKorne Lab11 Part 2#include <iostream>#include <iomanip> using namespace std typedef char el_t class Stack private struct StackNode

p2.cpp - Collin deKorne Lab11 Part 2#include...

This preview shows page 1 - 3 out of 3 pages.

/**************************************** Collin deKorne Lab11 Part 2 ****************************************/ #include <iostream> #include <iomanip> using namespace std; typedef char el_t; class Stack { private: struct StackNode { el_t element; // letter in the node StackNode *next; // Pointer to the next node }; StackNode *top; // Pointer to the stack top public: // Constructor Stack() { top = nullptr; } // Destructor ~Stack(); // Stack operations void push(el_t); void pop(el_t &); void getTop(el_t &) const; void displayAll() const; bool isEmpty() const; }; // implement the public methods highlighted in bold here Stack::~Stack() { cout << "Calling destructor!" << endl; while (top != nullptr) { pop(top->element); } cout << top << endl; } void Stack::push(el_t elem) { StackNode* temp = new StackNode; cout << "Pushing element " << elem << endl; temp->element = elem; temp->next = top; top = temp; } void Stack::pop(el_t &elem) { if (top == nullptr) cout << "List is empty" << endl; else { StackNode* temp = top->next; elem = top->element; cout << "Popping element " << top->element << endl; delete top; top = temp;
Image of page 1

Subscribe to view the full document.

} } void Stack::getTop(el_t &elem) const { if (isEmpty()) cout << "List is empty" << endl; else { elem = top->element; } } void Stack::displayAll() const { if (isEmpty()) cout << "[ empty ]" << endl; else { StackNode* temp = top; cout << "[ "; while (temp != nullptr) { cout << temp->element << " ";
Image of page 2
Image of page 3

Unformatted text preview: temp = temp-&gt;next; } cout &lt;&lt; &quot; ]&quot; &lt;&lt; endl; } } bool Stack::isEmpty() const { if (top == nullptr) return true; return false; } int main () { Stack s; el_t c; cout &lt;&lt; &quot; initial stack contents&quot; &lt;&lt; endl; s.displayAll(); s.pop(c); s.push('a'); cout &lt;&lt; endl &lt;&lt; &quot; stack contents after pushing a: &quot; &lt;&lt; endl; s.displayAll(); s.push('b'); cout &lt;&lt; endl &lt;&lt; &quot; stack contents after pushing b: &quot; &lt;&lt; endl; s.displayAll(); s.push('c'); s.push('d'); s.push('e'); s.push('f'); s.push('g'); cout &lt;&lt; endl &lt;&lt; &quot; stack contents after pushing c-g: &quot; &lt;&lt; endl; s.displayAll(); s.getTop(c); cout &lt;&lt; endl &lt;&lt; &quot;top elment is &quot; &lt;&lt; c &lt;&lt; endl; s.pop(c); cout &lt;&lt; endl &lt;&lt; c &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; &quot; stack contents after popping one element: &quot; &lt;&lt; endl; s.displayAll(); s.pop(c); cout &lt;&lt; endl &lt;&lt; &quot; popped element: &quot; &lt;&lt; c &lt;&lt; endl; cout &lt;&lt; endl &lt;&lt; &quot; stack contents after popping another element: &quot; &lt;&lt; endl; s.displayAll(); s.pop(c); s.pop(c); if (!s.isEmpty()) { s.getTop(c); cout &lt;&lt; endl &lt;&lt; &quot;top elment is &quot; &lt;&lt; c &lt;&lt; endl; } s.pop(c); cout &lt;&lt; endl &lt;&lt; &quot; stack contents after popping 3 more elements: &quot; &lt;&lt; endl; s.displayAll(); s.pop(c); s.pop(c); s.push('a'); s.push('b'); cout &lt;&lt; endl &lt;&lt; &quot; final stack contents &quot; &lt;&lt; endl; s.displayAll(); return 0; }...
View Full Document

  • Fall '09
  • Pointer, Void type, The Stack

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes