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.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 chapter8/queue2.h (limited to 'chapter8/queue2.h') diff --git a/chapter8/queue2.h b/chapter8/queue2.h new file mode 100644 index 0000000..a9a7003 --- /dev/null +++ b/chapter8/queue2.h @@ -0,0 +1,76 @@ +// FILE: queue2.h (part of the namespace +// TEMPLATE CLASS PROVIDED: queue (a queue of items) +// +// TEMPLATE PARAMETER, TYPEDEFS and MEMBER CONSTANTS for the stack class: +// The template parameter, Item, is the data type of the items in the queue, +// also defined as queue::value_type. It may be any of the C++ built-in +// types (int, char, etc.), or a class with a default constructor, a copy +// constructor, and an assignment operator. The definition +// queue::size_type is the data type of any variable that keeps track of +// how many items are in a queue. +// NOTE: +// Many compilers require the use of the new keyword typename before using +// the expressions queue::value_type and queue::size_type. +// Otherwise the compiler doesn't have enough information to realize that it +// is the name of a data type. +// +// CONSTRUCTOR for the queue template class: +// queue( ) +// Postcondition: The queue has been initialized as an empty queue. +// +// MODIFICATION MEMBER FUNCTIONS for the queue template class: +// void pop( ) +// Precondition: size( ) > 0. +// Postcondition: The front item of the queue has been removed. +// +// void push(const Item& entry) +// Postcondition: A new copy of entry has been inserted at the rear of the +// queue. +// +// CONSTANT MEMBER FUNCTIONS for the queue template class: +// bool empty( ) const +// Postcondition: The return value is true if the queue is empty. +// +// Item front( ) const +// +// size_type size( ) const +// Postcondition: The return value is the total number of items in the queue. +// +// VALUE SEMANTICS for the queue template class: +// Assignments and the copy constructor may be used with queue objects. + +#ifndef MAIN_SAVITCH_QUEUE2_H // Prevent duplicate definition +#define MAIN_SAVITCH_QUEUE2_H +#include // Provides std::size_t +#include "node2.h" // Node template class + +namespace main_savitch_8C +{ + template + class queue + { + public: + // TYPEDEFS + typedef std::size_t size_type; + typedef Item value_type; + // CONSTRUCTORS and DESTRUCTOR + queue( ); + queue(const queue& source); + ~queue( ); + // MODIFICATION MEMBER FUNCTIONS + void pop( ); + void push(const Item& entry); + void operator =(const queue& source); + // CONSTANT MEMBER FUNCTIONS + bool empty( ) const { return (count == 0); } + Item front( ) const; + size_type size( ) const { return count; } + private: + main_savitch_6B::node *front_ptr; // head_ptr + main_savitch_6B::node *rear_ptr; + size_type count; // Total number of items in the queue + }; +} +#include "queue2.template" // Include the implementation + +#endif -- cgit v1.2.3