(COMP2012)[2016](f)midterm_=vngkoee^_10579.pdf

Object return null if the stack is empty size

Info icon This preview shows pages 8–13. Sign up to view the full content.

View Full Document Right Arrow Icon
object, return NULL if the stack is empty) size() (returning the number of objects in the stack) For simplicity, put all your function definition INSIDE the class template definition in a single file which will be called ”Stack.h”. Note that your solution should work with the given main function and produce the following output: Push: Successful Push: Successful Top: -56.5 Pop: -56.5 Pop: 2.1 8 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
#include < iostream > #include Stack.h using namespace std; int main() { string str[] = { "Fail" , "Successful" } ; Stack < double > stackD(5); // A stack of size 5 // Push data bool s = stackD.push(2.1); cout "Push: " str[s] endl; stackD.push(-56.5); cout "Push: " str[s] endl; // Top and pop data double * ptr = stackD.top(); if (ptr) cout "Top: " * ptr endl; else cout "Stack is empty" endl; ptr = stackD.pop(); if (ptr) cout "Pop: " * ptr endl; else cout "Stack is empty" endl; ptr = stackD.pop(); if (ptr) cout "Pop: " * ptr endl; else cout "Stack is empty" endl; } 9 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 9
Answer: /* ”Stack.h” */ // Implement the template class ”Stack” here template < typename T > // 1 point for class template declaration and class Stack { // putting public keyword private : int topIndex; // 2 points for all the data members T * data; const int maxSize; public : Stack( int ms) : maxSize(ms) { // 1 point for using member initialization list topIndex = -1; // 1 point for initializing topIndex and data data = new T[ms]; } Stack() { delete [] data; // 1 point } bool isEmpty() const { // 1 point return (topIndex == -1) ? true : false ; } bool isFull() const { // 1 point return (topIndex + 1 == maxSize) ? true : false ; } bool push( const T& x) { bool retVal = false ; if (!isFull()) { // 1 point topIndex++; // 1 point for increment, data[topIndex] = x; // assigning data and set return value to true retVal = true ; } return retVal; } 10 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 10

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
T * top() const { T * retPtr = NULL; if (!isEmpty()) // 1 point retPtr = &data[topIndex]; // 1 point return retPtr; } T * pop() { T * retPtr = NULL; if (!isEmpty()) { // 1 point retPtr = &data[topIndex]; // 1 point topIndex--; } return retPtr; } int size() const { // 1 point return topIndex + 1; } } ; 11 (COMP2012)[2016](f)midterm~=vngkoee^_10579.pdf downloaded by xjiae from at 2018-03-13 17:27:22. Academic use within HKUST only.
Image of page 11
(b) In this part, you are required to use the template class Stack implemented in part (a) to evaluate a postfix expression. A postfix expression is an algebraic expression that requires its operators come after the two corresponding operands. The following shows a couple of postfix expressions: 2 3 + (Equivalent to 2 + 3) 1 2 3 * + 4 - (Equivalent to 1 + 2 * 3 - 4) To evaluate a postfix expression using stack, we can use the following steps: i. Create a stack to store operands ii. Scan the given expression and do following for every scanned element If the element is a number, push it into the stack
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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