程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 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;   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved