Skip to content

Instantly share code, notes, and snippets.

@qiuwei
Last active December 24, 2015 09:49
Show Gist options
  • Save qiuwei/6780243 to your computer and use it in GitHub Desktop.
Save qiuwei/6780243 to your computer and use it in GitHub Desktop.
fifo
#include <stdio.h>
#define MAX_SIZE 128
struct fifo{
int size;
int head;
int count;
char element[MAX_SIZE];
};
/*static struct fifo R;*/
/*static struct fifo T;*/
int init(struct fifo*);
int add(struct fifo*, char);
char pop(struct fifo*);
int init(struct fifo* f){
f->size = MAX_SIZE;
f->head = 0;
f->count= 0;
int i;
for (i=0; i<= f->size; i++){
f->element[i] = 0;
}
return 0;
}
int add(struct fifo *f, char c){
f->element[(f->head+f->count)%f->size] = c;
if (f->count == f->size){
f->head = (f->head + 1) % f->size;
}
else{
f->count++;
}
return 0;
}
char pop(struct fifo *f){
if (f->count > 0){
f->count--;
int current_head = f->head;
f->head = (f->head + 1)%f->size;
return f->element[current_head];
}
else{
printf("No element in the FIFO, can not pop!\n");
return -1;
}
}
int main(int argc, char args[]){
struct fifo rfifo;
if(0 != init(&rfifo)){
printf("Init failed");
}
/*struct fifo tfifo;*/
/*if(0 != init(&tfifo, element_t)){*/
/*printf("Init failed");*/
/*}*/
/*printf("%d\n", rfifo.size);*/
/*add(&rfifo, 'g');*/
/*add(&rfifo, 'o');*/
/*add(&rfifo, 'b');*/
/*add(&rfifo, 'a');*/
/*add(&rfifo, 'd');*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*add(&rfifo, 'o');*/
/*add(&rfifo, 'd');*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*printf("%c\n", pop(&rfifo));*/
/*pop(&rfifo);*/
/*printf("test");*/
/*add(&rfifo, 'a');*/
for(int i=0; i<= 127; i++){
add(&rfifo, 'b');
}
/*printf("%c\n", rfifo.element[128]);*/
for(int i=0; i<= 127; i++){
int a = pop(&rfifo);
if (a != -1){
printf("%c\n", a);
}
/*[>add(&rfifo, 'b');<]*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment