From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter8/queue2.template | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 chapter8/queue2.template (limited to 'chapter8/queue2.template') diff --git a/chapter8/queue2.template b/chapter8/queue2.template new file mode 100644 index 0000000..d08c1cb --- /dev/null +++ b/chapter8/queue2.template @@ -0,0 +1,88 @@ +// FILE: queue2.template +// TEMPLATE CLASS IMPLEMENTED: Queue (see queue2.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 stored in the member variable +// count. +// 2. The items in the queue are stored in a linked list, with the front of +// the queue stored at the head node, and the rear of the queue stored at +// the final node. +// 3. The member variable front_ptr is the head pointer of the linked list of +// items. For a non-empty queue, the member variable rear_ptr is the +// tail pointer of the linked list; for an empty list, we don't care +// what's stored in rear_ptr. + +#include // Provides assert +#include "node2.h" // Node template class + +namespace main_savitch_8C +{ + template + queue::queue( ) + { + count = 0; + front_ptr = NULL; + rear_ptr = NULL; + } + + template + queue::queue(const queue& source) + // Library facilities used: node2.h + { + count = source.count; + list_copy(source.front_ptr, front_ptr, rear_ptr); + } + + template + queue::~queue( ) + { + list_clear(front_ptr); + } + + template + void queue::operator =(const queue& source) + // Library facilities used: node2.h + { + if (this == &source) // Handle self-assignment + return; + list_clear(front_ptr); + list_copy(source.front_ptr, front_ptr, rear_ptr); + count = source.count; + } + + template + Item queue::front( ) const + // Library facilities used: cassert + { + assert(!empty( )); + return front_ptr->data( ); + } + + template + void queue::pop( ) + // Library facilities used: cassert, node2.h + { + assert(!empty( )); + list_head_remove(front_ptr); + --count; + } + + template + void queue::push(const Item& entry) + // Library facilities used: node2.h + { + if (empty( )) + { // Insert first entry. + list_head_insert(front_ptr, entry); + rear_ptr = front_ptr; + } + else + { // Insert an entry that is not the first. + list_insert(rear_ptr, entry); + rear_ptr = rear_ptr->link( ); + } + ++count; + } + +} + -- cgit v1.2.3