


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















What is a stack?
Push (ItemType newItem)
Pop (ItemType& item)
Stack Implementation #include "ItemType.h" // Must be provided by the user of the class // Contains definitions for MAX_ITEMS and ItemType class StackType { public: StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: int top; ItemType items[MAX_ITEMS]; };
Stack Implementation (cont.) StackType:: StackType() { top = -1; } void StackType:: MakeEmpty() { top = -1; } bool StackType:: IsEmpty() const { return (top == -1); }
Stack overflow
Stack underflow
Implementing stacks using templates
Example using templates
The compiler generates distinct class types and gives its own internal name to each of the types.
Function templates
template
Function templates (cont.) template
Comments using templates
Implementing stacks using dynamic array allocation (cont.) template
Example: postfix expressions