This commit is contained in:
Debucquoy Anthony 2023-10-24 20:25:22 +02:00
parent dfe4fd3729
commit 0424fbcca6
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
3 changed files with 80 additions and 24 deletions

View File

@ -1,41 +1,44 @@
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
struct node{ typedef struct Node* LIFO;
struct node* prev;
struct Node{
struct Node* prev;
void* data; void* data;
}; };
typedef struct node* LIFO; LIFO mklifo();
void push(LIFO*, void*);
LIFO* mklifo (); void* pop(LIFO*);
void push(LIFO* lst , void*);
void* pop(LIFO* lst);
int main(void) int main(void)
{ {
char* t = "test"; int a = 5;
LIFO* lifo = mklifo(); int b = 12;
push(lifo, t); LIFO lifo = mklifo();
printf("%s", (char *)pop(lifo));
push(&lifo, &a);
push(&lifo, &b);
int *c = pop(&lifo);
int *d = pop(&lifo);
printf("%d\n", *c);
printf("%d\n", *d);
} }
LIFO *mklifo (){ LIFO mklifo(){
LIFO* ret = malloc(sizeof(struct node)); return calloc(1, sizeof(struct Node));
return ret;
} }
void push(LIFO *lst , void *el){ void push(LIFO* lst, void* el){
LIFO* next = mklifo(); LIFO next = mklifo();
(*lst)->data = el; (*lst)->data = el;
(*next)->prev = *lst; next->prev = *lst;
lst = next; *lst = next;
} }
void *pop(LIFO *lst){ void *pop(LIFO* lst){
void* el;
(*lst)->data = el;
*lst = (*lst)->prev; *lst = (*lst)->prev;
return el; return (*lst)->data;
} }

Binary file not shown.

53
bac2/os/chap2/ex6.c Normal file
View File

@ -0,0 +1,53 @@
#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;
}