Skip to content

Instantly share code, notes, and snippets.

@zxmarcos
Created October 17, 2012 20:45
Show Gist options
  • Select an option

  • Save zxmarcos/3908060 to your computer and use it in GitHub Desktop.

Select an option

Save zxmarcos/3908060 to your computer and use it in GitHub Desktop.
fila circular
#include <iostream>
using namespace std;
#define MAX 5
int queue[MAX];
int init = 0;
int final = 0;
int counter = 0;
// insere um elemento na fila...
void push(int id)
{
if (counter >= MAX)
cout << "fila cheia!\n";
else {
cout << "push " << id << endl;
queue[final] = id;
final = (final + 1) % MAX;
counter++;
}
}
// remove um elemento da fila
int pop()
{
if (counter) {
cout << "pop " << queue[init] << endl;
init = (init + 1) % MAX;
counter--;
} else
cout << "fila vazia!\n";
}
// mostra a fila em ordem...
void show()
{
int pos = init;
for (int i=0; i < counter; i++) {
cout << queue[pos] <<",";
pos = (pos + 1) % MAX;
}
cout << endl;
}
int main()
{
for (int i = 0; i < MAX; i++)
push(i);
show();
for (int i = 0; i < 2; i++)
pop();
push(1234);
show();
pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment