/* * File: stack.cpp * Author: Leonid Reyzin (with thanks to David Metcalf) * Date: January 2002 * Class: CS112B1 * Assignment: Implementation of stack module * ----------------------------------------------------- */ //#include #include "stack.h" /* declarations of public functions */ /*------------------------------------------------------------*/ /* Stack::Stack constructor * creates stack and initializes it */ Stack::Stack() { count=0; } /*------------------------------------------------------------*/ /* Stack::Init: * Usage: stack.Init(); * Initializes a stack. */ bool Stack::Init() { count=0; return true; } /*------------------------------------------------------------*/ /* Stack::IsEmpty: * Usage: if(stack.IsEmpty()) {...} * checks whether a stack is empty. */ bool Stack::IsEmpty() { return (count==0); } /*------------------------------------------------------------*/ /* Stack::IsFull: * Usage: if(stack.IsFull()) {...} * checks whether a stack is full. */ bool Stack::IsFull() { return (count==MAXSTACKSIZE); } /*------------------------------------------------------------*/ /* Stack::Push: * Usage: flag= stack.Push(item); * pushes item onto stack. * returns true if successful. */ bool Stack::Push(ItemType item) { if (IsFull()) return false; data[count++]=item; return true; } /*------------------------------------------------------------*/ /* Stack::Pop: * Usage: flag= stack.Pop(item); * Pops top item off stac; returns true unless * the stack is empty and there is nothing to pop * (i.e., returns false in case of stack underflow). */ bool Stack::Pop(ItemType &item) { if (IsEmpty()) return false; item=data[--count]; return true; } /* ----------- END OF FILE: stack.cpp ---------- */