Queue.h
上传用户:qdkongtiao
上传日期:2022-06-29
资源大小:356k
文件大小:4k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*
  2.  * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
  3.  * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
  4.  * copyright and warranty notices given in that book:
  5.  * 
  6.  * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
  7.  * 
  8.  * 
  9.  * "The authors and publisher have taken care in the preparation of this book,
  10.  * but make no expressed or implied warranty of any kind and assume no
  11.  * responsibility for errors or omissions. No liability is assumed for
  12.  * incidental or consequential damages in connection with or arising out of the
  13.  * use of the information or programs contained herein."
  14.  * 
  15.  * Permission is granted for this code to be used for educational purposes in
  16.  * association with the book, given proper citation if and when posted or
  17.  * reproduced.Any commercial use of this code requires the explicit written
  18.  * permission of the publisher, Addison-Wesley Professional, a division of
  19.  * Pearson Education, Inc. Send your request for permission, stating clearly
  20.  * what code you would like to use, and in what specific way, to the following
  21.  * address: 
  22.  * 
  23.  *  Pearson Education, Inc.
  24.  *  Rights and Contracts Department
  25.  *  75 Arlington Street, Suite 300
  26.  *  Boston, MA 02216
  27.  *  Fax: (617) 848-7047
  28. */ 
  29. #ifndef QUEUE_H
  30. #define QUEUE_H
  31. //NB: if change the interface in this file, also update Queue_interface.h!!!!
  32. //    and int-Queue.h
  33. #include <iostream>
  34. // function template declaration must precede friend declaration in Queue
  35. template <class T> class Queue;
  36. template <class T> 
  37. std::ostream& operator<<(std::ostream&, const Queue<T>&);
  38. template <class Type> class Queue {
  39.     friend std::ostream& 
  40.     operator<< <Type> (std::ostream&, const Queue<Type>&);
  41. public:
  42.     Queue(): head(0), tail(0) { }
  43.     // construct a Queue from a pair of iterators on some sequence
  44.     template <class It> Queue(It, It): head(0), tail(0)
  45.                              { copy(beg, end); }
  46.     // copy control to manage pointers to QueueItems in the Queue
  47.     Queue(const Queue &Q): head(0), tail(0) {copy(Q);}
  48.     Queue& operator=(const Queue&);
  49.     ~Queue() { destroy(); }
  50.     // replace current Queue by contents delimited by a pair of iterators
  51.     template <class Iter> void assign(Iter, Iter);
  52.     // return element from head of Queue
  53.     // unchecked operation: front on an empty Queue is undefined
  54.     Type& front()            { return head->item; }
  55.     const Type front() const { return head->item; }
  56.     void push(const Type &);// add element to back of Queue
  57.     void pop();             // remove element from head of Queue
  58.     bool empty() const {    // true if no elements in the Queue
  59.         return head == 0;
  60.     }
  61.     // interface functions to Queue are unchanged
  62. private:
  63.     // public members are ok: QueueItem is a private member of Queue
  64.     // only Queue and its friends may access the members of QueueItem
  65.     struct QueueItem {
  66.         QueueItem(const Type &);
  67.         Type item;           // value stored in this element
  68.         QueueItem *next;     // pointer to next element in the Queue
  69.     };
  70.     QueueItem *head;   // pointer to first element in Queue
  71.     QueueItem *tail;   // pointer to last element in Queue
  72.     // utility functions used by copy constructor, assignment and destructor
  73.     void destroy();          // delete all the elements
  74.     void copy(const Queue&); // copy all elements from parameter
  75. private:
  76.     // copy all elements from input range denoted by iterator pair
  77.     template <class Iter> void copy(Iter, Iter); 
  78. };
  79. // Inclusion Compilation Model: include member function definitions as well
  80. #include "Queue.cc"
  81. #endif