summaryrefslogtreecommitdiff
path: root/chapter7/stack2.template
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-04-07 23:18:32 -0700
committerFuwn <[email protected]>2024-04-07 23:18:32 -0700
commitc1b6ffe70bd281c6c230fd63fabcaac2aff47514 (patch)
treee8af3b1782a7cd0754590ed618fddc1bdb9b7385 /chapter7/stack2.template
downloaddscode-c1b6ffe70bd281c6c230fd63fabcaac2aff47514.tar.xz
dscode-c1b6ffe70bd281c6c230fd63fabcaac2aff47514.zip
feat: initial commitHEADmain
Diffstat (limited to 'chapter7/stack2.template')
-rw-r--r--chapter7/stack2.template59
1 files changed, 59 insertions, 0 deletions
diff --git a/chapter7/stack2.template b/chapter7/stack2.template
new file mode 100644
index 0000000..e02d827
--- /dev/null
+++ b/chapter7/stack2.template
@@ -0,0 +1,59 @@
+// FILE: stack2.template
+// TEMPLATE CLASS IMPLEMENTED: stack<Item> (see stack2.h for documentation)
+// This file is included in the header file, and not compiled separately.
+// INVARIANT for the stack class:
+// 1. The items in the stack are stored in a linked list, with the top of the
+// stack stored at the head node, down to the bottom of the stack at the
+// final node.
+// 2. The member variable top_ptr is the head pointer of the linked list.
+
+#include <cassert> // Provides assert
+#include "node2.h" // Node template class
+
+namespace main_savitch_7B
+{
+ template <class Item>
+ stack<Item>::stack(const stack<Item>& source)
+ // Library facilities used: node2.h
+ {
+ main_savitch_6B::node<Item> *tail_ptr; // Needed for argument of list_copy
+
+ list_copy(source.top_ptr, top_ptr, tail_ptr);
+ }
+
+ template <class Item>
+ void stack<Item>::push(const Item& entry)
+ // Library facilities used: node2.h
+ {
+ list_head_insert(top_ptr, entry);
+ }
+
+ template <class Item>
+ void stack<Item>::pop( )
+ // Library facilities used: cassert, node2.h
+ {
+ assert(!empty( ));
+ list_head_remove(top_ptr);
+ }
+
+ template <class Item>
+ void stack<Item>::operator =(const stack<Item>& source)
+ // Library facilities used: node2.h
+ {
+ main_savitch_6B::node<Item> *tail_ptr; // Needed for argument of list_copy
+
+ if (this == &source) // Handle self-assignment
+ return;
+
+ list_clear(top_ptr);
+ list_copy(source.top_ptr, top_ptr, tail_ptr);
+ }
+
+ template <class Item>
+ Item stack<Item>::top( ) const
+ // Library facilities used: cassert
+ {
+ assert(!empty( ));
+ return top_ptr->data( );
+ }
+}