54 lines
927 B
C
54 lines
927 B
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
|
|
typedef struct node{
|
|
void* data;
|
|
struct node* next;
|
|
} NODE;
|
|
|
|
//Could be only head but it allows a complexity of O(1) instead of O(n) for insertion (where n would be the size of the chain)
|
|
typedef struct {
|
|
NODE* head;
|
|
NODE* tail;
|
|
} FIFO;
|
|
|
|
FIFO mkfifo ();
|
|
void enqueue(FIFO *lst ,void* el);
|
|
void* dequeue(FIFO *lst);
|
|
|
|
int main(void)
|
|
{
|
|
int a = 5;
|
|
int b = 12;
|
|
FIFO fifo = mkfifo();
|
|
|
|
enqueue(&fifo, &a);
|
|
enqueue(&fifo, &b);
|
|
int *c = dequeue(&fifo);
|
|
int *d = dequeue(&fifo);
|
|
|
|
printf("%d\n", *c);
|
|
printf("%d\n", *d);
|
|
|
|
}
|
|
|
|
FIFO mkfifo (){
|
|
return (FIFO) {NULL, NULL};
|
|
}
|
|
|
|
void enqueue(FIFO *lst ,void *el){
|
|
NODE *new = calloc(1, sizeof(NODE));
|
|
new->data = el;
|
|
if(lst->tail != NULL)
|
|
lst->tail->next = new;
|
|
lst->tail = new;
|
|
lst->head = lst->head == NULL ? new : lst->head;
|
|
}
|
|
|
|
void* dequeue(FIFO *lst){
|
|
void* ret = lst->head->data;
|
|
lst->head = lst->head->next;
|
|
return ret;
|
|
}
|