Created
October 17, 2012 20:45
-
-
Save zxmarcos/3908060 to your computer and use it in GitHub Desktop.
fila circular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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