summaryrefslogtreecommitdiff
path: root/chapter8/queue1.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 /chapter8/queue1.template
downloaddscode-main.tar.xz
dscode-main.zip
feat: initial commitHEADmain
Diffstat (limited to 'chapter8/queue1.template')
-rw-r--r--chapter8/queue1.template55
1 files changed, 55 insertions, 0 deletions
diff --git a/chapter8/queue1.template b/chapter8/queue1.template
new file mode 100644
index 0000000..5feee78
--- /dev/null
+++ b/chapter8/queue1.template
@@ -0,0 +1,55 @@
+// FILE: queue1.template
+// TEMPLATE CLASS IMPLEMENTED: queue<Item> (see queue1.h for documentation)
+// This file is included in the header file, and not compiled separately.
+// INVARIANT for the queue class:
+// 1. The number of items in the queue is in the member variable count;
+// 2. For a non-empty queue, the items are stored in a circular array
+// beginning at data[front] and continuing through data[rear].
+// The array's total capacity of the array is CAPACITY.
+// 3. For an empty array, rear is some valid index, and front is
+// always equal to next_index(rear).
+//
+
+#include <cassert> // Provides assert
+
+namespace main_savitch_8B
+{
+ template <class Item>
+ const typename queue<Item>::size_type queue<Item>::CAPACITY;
+
+ template <class Item>
+ queue<Item>::queue( )
+ {
+ count = 0;
+ first = 0;
+ last = CAPACITY - 1;
+ }
+
+ template <class Item>
+ Item queue<Item>::front( ) const
+ // Library facilities used: cassert
+ {
+ assert(!empty( ));
+ return data[first];
+ }
+
+ template <class Item>
+ void queue<Item>::pop( )
+ // Library facilities used: cassert
+ {
+ assert(!empty( ));
+ first = next_index(first);
+ --count;
+ }
+
+ template <class Item>
+ void queue<Item>::push(const Item& entry)
+ // Library facilities used: cassert
+ {
+ assert(size( ) < CAPACITY);
+ last = next_index(last);
+ data[last] = entry;
+ ++count;
+ }
+
+}