当前位置:编程学习 > C/C++ >>

队列-----数组存储结构及其操作算法的实现-----舞伴问题

[cpp]  
//file:queue.h  
#ifndef _Queue_H_INCLUDE_  
#define _Queue_H_INCLUDE_  
#define N 10002  
#include<cstdio>  
template <class T>  
class Queue  
{  
public:  
    Queue():frount(1),rear(1) {}  
    void push(T t);  
    void pop();  
    T front();  
    bool empty();  
private:  
    int frount,rear;  
    T Q[N];  
};  
template <class T>  
bool Queue<T>::empty()  
{  
   if(frount==rear) return true;  
   else return false;  
}  
template <class T>  
T Queue<T>::front()  
{  
    if (!empty())    return Q[frount];  
    else   {printf("Empty!\n");}  
}  
template <class T>  
void Queue<T>::pop()  
{  
   if(!empty())  frount++;  
    else printf("Empty!\n");  
}  
template <class T>  
void Queue<T>::push(T t)  
{  
   Q[rear]=t;  
   rear++;  
}  
#endif // _STACK_H_INCLUDE_  
[cpp]  
//Îè°éÎÊÌâ  
#include<iostream>  
#include "queue.h"  
using namespace std;  
struct node  
{  
    char name[20];  
    char sex;  
};  
int main()  
{  
    Queue<node> m,w;  
    int n;  
    struct node x;  
    cin >> n;  
    while(n--)  
    {  
        cin >> x.name;  
        cin >> x.sex;  
        if(x.sex=='w')  w.push(x);  
        else if(x.sex=='m') m.push(x);  
        else ;  
    }  
    while(!w.empty()&&!m.empty())  
    {  
        cout << w.front().name << "--" << m.front().name << endl;  
        w.pop();  
        m.pop();  
    }  
    if(w.empty())  
    {  
        printf("There are men still waiting!\n");  
    }  
    else if(m.empty())  
    {  
        printf("There are women still waiting!\n");  
    }  
    else printf("There is no person waiting!\n");  
    return 0;  
}  
 
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,