STL之顺序容器适配器(队列的循环数组实现)

队列的循环数组实现······如果按照Vector,或者List的基础上去做就不用担心队列会满····这是按照教材、《数据结构与算法分析》、《算法导论》的用数组实现的思想····比较简单······所以····所以就没有解释······

  1 #include<iostream>

  2 using namespace std;

  3 

  4 template<typename Object>

  5 class Queue

  6 {

  7 public:

  8     enum { Capacity = 10, Pos = 0 };

  9     Queue( )

 10     {

 11         init( );

 12     }

 13     ~Queue( )

 14     {

 15         delete [ ] data;

 16     }

 17 

 18     int size( )

 19     {

 20         return theSize;

 21     }

 22 

 23     bool empty( )

 24     {

 25         return  size( ) == 0;

 26     }

 27 

 28     void push( const Object & x )

 29     {

 30         if( size() == 0)

 31         {

 32             theFront++;

 33             data[ ++theBack ] = x;

 34             ++theSize;

 35         }

 36         else

 37         {

 38             ++theBack;

 39             if( theBack  == theCapacity )

 40             {

 41                 theBack %= ( theCapacity - 1 );

 42             }

 43             data[ theBack ] = x;

 44             ++theSize;

 45 

 46             if( size() == theCapacity )

 47             {

 48                 cout << "队列已经满了!" << endl;

 49             }

 50         }

 51     }

 52 

 53     void pop( )

 54     {

 55         theFront++;

 56         if( theFront == theCapacity )

 57         {

 58             theFront %= ( theCapacity - 1 );

 59         }

 60         --theSize;

 61     }

 62 

 63     Object front( )

 64     {

 65         return data[ theFront ];

 66     }

 67 

 68     Object back( )

 69     {

 70         return  data[ theBack ];

 71     }

 72 

 73 private:

 74     Object * data;

 75     int theSize;

 76     int theCapacity;

 77     int theFront;

 78     int theBack;

 79 

 80     void init( )

 81     {

 82         theSize = 0;

 83         theCapacity = Capacity;

 84         theFront = theBack = Pos - 1;

 85         data = new Object[ theCapacity ];

 86     }

 87 };

 88 

 89 int main( )

 90 {

 91     Queue<int> q1;

 92     cout << q1.size() << endl;

 93     bool flag = true;

 94     char c;

 95     int m;

 96     while( flag )

 97     {

 98         cout << "输入一个数字,这个数字将会进入队列!" << endl;

 99         cin >> m;

100         q1.push( m );

101         cout << "是否继续输入?如果是,请输入Y,否则输入N。" << endl;

102         cin >> c;

103         if( c == 'Y' )

104             flag = true;

105         else

106             flag = false;

107     }

108     cout << "输出队首!" << endl;

109     cout << q1.front() << endl;

110     cout << "输出队尾!" << endl;

111     cout << q1.back() << endl;

112     cout << "输出队列的数目!" << endl;

113     cout << q1.size() << endl;

114     q1.pop( );

115     cout << "经过pop函数的处理后!" << endl;

116     cout << "输出队首!" << endl;

117     cout << q1.front() << endl;

118     cout << "输出队尾!" << endl;

119     cout << q1.back() << endl;

120     cout << "输出队列的数目!" << endl;

121     cout << q1.size() << endl;

122     return 0;

123 }

你可能感兴趣的:(STL)