C++: Création d’une ‘Queue’ générique

Author:


{filelink=15767}


#include 
using namespace std;

#define SIZE 100

template  class Type_Queue {
  T queue[SIZE];
  int tete, cqueue;
public:
  Type_Queue() {
     tete = 0;
	 cqueue = 0;
  }
  void q(T nombre);
  T deq();
};

template  void Type_Queue::q(T nombre)
{
  if(cqueue+1==tete || (cqueue+1==SIZE && !tete)) {
    cout << "La Queue est pleine.";
    return;
  }
  cqueue++;
  if(cqueue==SIZE)
     cqueue = 0;
  queue[cqueue] = nombre;
}

template  T Type_Queue::deq()
{
  if(tete == cqueue) {
    cout << "La Queue est vide.";
    return 0;
  }
  tete++;
  if(tete==SIZE)
     tete = 0;
  return queue[tete];
}

int main()
{
  Type_Queue queue1;
  Type_Queue queue2;
  int i;

  for(i=1; i <=10; i++) {
    queue1.q(i);
    queue2.q(i-1+'a');
  }

  for(i=1; i <=10; i++) {
    cout << "Queue 1: " << queue1.deq() << endl;
    cout << "Queue 2: " << queue2.deq() << endl;
  }

  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *