From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter7/stack1.template | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 chapter7/stack1.template (limited to 'chapter7/stack1.template') diff --git a/chapter7/stack1.template b/chapter7/stack1.template new file mode 100644 index 0000000..ad311c2 --- /dev/null +++ b/chapter7/stack1.template @@ -0,0 +1,41 @@ +// FILE: stack1.template +// TEMPLATE CLASS IMPLEMENTED: stack (see stack1.h for documentation) +// This file is included in the header file, and not compiled separately. +// INVARIANT for the stack class: +// 1. The number of items in the stack is in the member variable used. +// 2. The actual items of the stack are stored in a partially-filled +// array data[0]..data[used-1]. The stack elements appear from the +// bottom (at data[0]) to the top (at data[used-1]). + +#include // Provides assert + +namespace main_savitch_7A +{ + template + const typename stack::size_type stack::CAPACITY; + + template + void stack::push(const Item& entry) + // Library facilities used: cassert + { + assert(size( ) < CAPACITY); + data[used] = entry; + ++used; + } + + template + void stack::pop( ) + // Library facilities used: cassert + { + assert(!empty( )); + --used; + } + + template + Item stack::top( ) const + // Library facilities used: cassert + { + assert(!empty( )); + return data[used-1]; + } +} -- cgit v1.2.3