Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

From Book: Data Abstraction and Problem Solving using Java; Ch 7; Problem 12 add

ID: 3540410 • Letter: F

Question

From Book: Data Abstraction and Problem Solving using Java; Ch 7; Problem 12

addition to the standard stack operations%u2014isEmpty, push, pop, and peek %u2014 a traversable stack includes the operation traverse. The traverse operation is an iterator that begins at the bottom of the stack and displays each item in the stack until it reaches the top of the stack. Alternatively, you could create a StackIterator class based on the StackInterface, as given in Chapter 7. However, the iterator would then only have access to the methods contained in the interface, such as push, pop, and peek. Since the iterator should not change the contents of the stack,

the implementation would be quite inef%uFB01cient.

The StackIterator class would be more ef%uFB01cient if it had access to the under-

lying stack implementation (either StackArrayBased or StackReferenceBased).

Implement a Stack class within a package that provides package access to the stack%u2019s

underlying data structure. Then, in the same package, create a StackIterator class

that uses this new Stack class.

Explanation / Answer

Ans) #ifndef __StackClassH__ #define __StackClassH__ #include // For error-checking purposes //------------------------------------------------- // Main structure of Stack Class: //------------------------------------------------- template class Stack { public: Stack(int MaxSize=500); Stack(const Stack &OtherStack); ~Stack(void); inline void Push(const Elem &Item); // Adds Item to the top inline Elem Pop(void); // Returns Item from the top inline const Elem &Peek(int Depth) const; // Peek a depth downwards protected: Elem *Data; // The actual Data array int CurrElemNum; // The current number of elements const int MAX_NUM; // Maximum number of elements }; //------------------------------------------------- // Implementation of Stack Class: //------------------------------------------------- // Stack Constructor function template Stack::Stack(int MaxSize) : MAX_NUM( MaxSize ) // Initialize the constant { Data = new Elem[MAX_NUM]; CurrElemNum = 0; } // Stack Destructor function template Stack::~Stack(void) { delete[] Data; } // Push() function template inline void Stack::Push(const Elem &Item) { // Error Check: Make sure we aren't exceeding the maximum storage space assert(CurrElemNum
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote