随笔-13  评论-0  文章-2  trackbacks-0
 1/**
 2        Queue
 3        Version: 1.0
 4        Member function as follow:
 5        size()
 6        front()        // the head element
 7        back()         // the rear element
 8        push(T)        // inset an elm
 9        pop()        // delete the last elm
10        empty()         // if it is an empty list
11        print()
12
13        Use C++ template
14        @author fleap
15        data: 2009/03/20
16**/

17
18// arqueue.h
19#ifndef ARQUEUE_H
20#define ARQUEUE_H
21
22#include<iostream>
23#include<string>
24
25using namespace std;
26
27template<typename T>
28class arqueue
29{
30public:
31    arqueue():head(0),rear(0){}
32    ~arqueue(){ head = 0; rear = 0; }
33    bool empty()const return rear == 0; }
34    int  size() const return rear + 1; }
35    T front() const return elem[head]; }
36    T back() const return elem[rear]; }
37    void push(const T&);
38    void pop();
39    void print() const;
40private:
41    static const int MaxSize = 200;
42    T elem[MaxSize];
43    int head, rear;
44}
;
45
46#endif
47
48
49// the member function is not good ,but effect, i will modify in the next version
50template<typename T> void arqueue<T>::push(const T& one)
51{
52    static int flag = 1;
53    if(flag) { elem[0= one; flag = 0; }
54    else
55    {
56        rear++;
57        elem[rear] = one;
58    }

59}

60
61template<typename T> void arqueue<T>::pop()
62{
63    rear--;
64}

65
66template<typename T> void arqueue<T>::print() const
67{
68    //assert(rear < MaxSize);
69    for(int i = head; i <= rear; i++)
70    cout << elem[i] << " ";
71    cout << endl;
72}

73
//Test function
// Test function
int main()
{

    arqueue
<int> que;
    cout 
<< "----------------Display the queue program-----------------------" << endl;
    cout 
<< "\nInitial the queue,please input the number of element to push stack "<< endl;
    cout 
<< "number = " ;
    
int number;
    cin 
>> number;
    cout 
<< "Secondly input the number in the following\n" << endl;
    
int temp;
    
forint i = 0; i < number; i++ )
    
{
        cin 
>> temp;
        que.push(temp);
    }

    cout 
<< "You Input As Follow" << endl;
    que.print();
    cout 
<< "head:" << que.front() << " rear:" << que.back() << endl;
    cout 
<< "Would you like to display the pop functionYes or No" << endl;
    
string answer;
    cin 
>> answer;
    
if( answer == "yes" || answer == "YES" )
    
{
        que.pop();
        que.print();
        cout 
<< "head:" << que.front() << " rear:" << que.back() << endl;
    }

    cout 
<<"-----------------------------------------------------------------" << endl;
    system(
"pause");
    
return 0;
}

posted on 2009-03-20 02:18 亦夏 阅读(213) 评论(0)  编辑 收藏 引用 所属分类: DataStruct

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理