Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // [ , , , , ] MAX
- // [1, , , , ]
- // [1,4,17, , ] front = 0, length = 3
- // dequeue -> tirei o 1 da fila
- // [1,4,17, , ] front = 1, length = 2
- // dequeue -> tirei o 4 da fila
- // [1,4,17, , ] front = 2, length = 1
- // inseri 23 e 55
- // [1,4,17,23,55] front = 2, length = 3
- // inserir 47
- // [47,4,17,23,55] front = 2, length = 4
- //inserir 39
- // [47,39,17,23,55] front = 2, length = 5
- //[38,4,17,23,55] front = 4, length = 2;
- #define MAX 5
- struct staticqueue{
- int front;
- int length;
- int values[MAX];
- };
- typedef struct staticqueue *SQueue;
- void SinitSQueue(SQueue q){
- q->front = 0;
- q->length = 0;
- }
- int Senqueue(SQueue q, int x){
- if(q->length == MAX) return -1;
- q->values[(q->length + q->front)%MAX] = x;
- q->length++;
- return 0;
- }
- int Sdequeue(SQueue q, int *x){
- if(q->length == 0) return -1;
- *x = q->values[q->front];
- q->length--;
- q->front = (q->front+1)%MAX;
- return 0;
- }
- int Sfront(SQueue, int *x){
- if(q->length == 0) return -1;
- *x = q->values[q->front];
- return 0;
- }
- int main() {
- struct staticqueue s1;
- SQueue s = &s1;
- int x;
- SinitSQueue(s);
- Senqueue(s,29);
- Senqueue(s,67);
- Senqueue(s,7);
- Senqueue(s,35);
- Senqueue(s,9);
- Sdequeue(s,&x);
- Sdequeue(s,&x);
- Sdequeue(s,&x);
- Sdequeue(s,&x);
- Senqueue(s,50);
- Sdequeue(s,&x);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement