cours_progra/bac2/os/chap2/ex5.c
2023-10-24 20:25:22 +02:00

45 lines
607 B
C

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