Mercurial > Algorithms__Sedgewick
diff algs4-c++/src/Queue.hpp @ 8:b02533162b6e
Linked list based Queue class.
author | Eris Caffee <discordia@eldalin.com> |
---|---|
date | Fri, 29 May 2015 21:13:32 -0500 |
parents | |
children | 0f99a5ae6cd4 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/algs4-c++/src/Queue.hpp Fri May 29 21:13:32 2015 -0500 1.3 @@ -0,0 +1,66 @@ 1.4 +#ifndef QUEUE_HPP 1.5 +#define QUEUE_HPP 1.6 + 1.7 +#include <cstddef> 1.8 + 1.9 +// Linked list based queue after Sedgewick and Wayne, Algorithms 4th ed, Algorithm 1.3 1.10 + 1.11 +template <typename T> 1.12 +class Queue { 1.13 + 1.14 +public: 1.15 + 1.16 + //////////////////////////////////// 1.17 + class Node { 1.18 + public: 1.19 + T item; 1.20 + Node *next; 1.21 + }; 1.22 + 1.23 + 1.24 + //////////////////////////////////// 1.25 + class iterator; 1.26 + friend class iterator; 1.27 + class iterator { 1.28 + 1.29 + public: 1.30 + 1.31 + iterator( Node *c ); 1.32 + 1.33 + iterator& operator++(); 1.34 + iterator operator++(int); 1.35 + 1.36 + T operator*(); 1.37 + bool operator!=( typename Queue<T>::iterator ); 1.38 + 1.39 + private: 1.40 + 1.41 + Node *curr; 1.42 + }; 1.43 + 1.44 + //////////////////////////////////// 1.45 + 1.46 + Queue( void ); 1.47 + ~Queue( void ); 1.48 + 1.49 + void enqueue( T &item ); 1.50 + T dequeue( void ); 1.51 + 1.52 + bool is_empty( void ); 1.53 + size_t size( void ); 1.54 + 1.55 + iterator begin( void ); 1.56 + iterator end( void ); 1.57 + 1.58 +private: 1.59 + 1.60 + size_t N; 1.61 + Node *first; 1.62 + Node *last; 1.63 + 1.64 + }; 1.65 + 1.66 + 1.67 + 1.68 +#endif 1.69 +