Código
#include <iostream> #include <stdlib.h> using namespace std; const int MAX=6; typedef int contenedor[MAX]; typedef struct tcola{ contenedor datos; int final, frente; }; void init_queue(tcola &q); void push_queue(tcola &q, int nuevo, bool ultimo); bool full_queue(tcola q); bool empty_queue(tcola q); int pop_queue(tcola &q); int top_queue(tcola q); int bottom_queue(tcola q); int next(int indice); int prev(int indice); int main() { int dato; tcola q; bool opcion; init_queue(q); while(full_queue(q)==false){ cout<<"Ingrese dato: "; cin>>dato; cout<<"1. Agregar por final"<<endl; cout<<"0. Agregar por frente"<<endl; cin>>opcion; push_queue(q,dato,opcion); } q.frente=MAX-1; q.final=MAX-2; while(empty_queue(q)==false){ cout<<pop_queue(q); } cout<<endl; system("pause"); } void init_queue(tcola &q) { q.final=MAX-1; q.frente=MAX-1; } void push_queue(tcola &q, int nuevo, bool ultimo) { if(full_queue(q)==true) cout<<"COLA LLENA"<<endl; else{ if(ultimo==true){ q.final=next(q.final); q.datos[q.final]=nuevo; } else{ q.frente=prev(q.frente); q.datos[q.frente]=nuevo; } } } bool full_queue(tcola q) { return next(q.final)==q.frente; } bool empty_queue(tcola q) { return q.frente==q.final; } int pop_queue(tcola &q) { int aux; if(empty_queue(q)==true) aux=-1; else{ q.frente=next(q.frente); aux=q.datos[q.frente]; } return aux; } int top_queue(tcola q) { int aux; if(empty_queue(q)==true) aux=-1; else{ aux=q.datos[next(q.frente)]; } return aux; } int bottom_queue(tcola q) { int aux; if(empty_queue(q)==true) aux=-1; else{ aux=q.datos[q.final]; } return aux; } int next(int indice) { if(indice==MAX-1) indice=0; else indice++; return indice; } int prev(int indice) { if(indice==0){ indice=MAX-1; } else{ indice--; } return indice; }