cours_progra/bac2/os/chap2/ex5.c

45 lines
607 B
C
Raw Normal View History

2023-10-18 20:27:40 +02:00
#include <stdio.h>
2023-10-24 20:25:22 +02:00
#include <stdlib.h>
2023-10-18 20:27:40 +02:00
2023-10-24 20:25:22 +02:00
typedef struct Node* LIFO;
struct Node{
struct Node* prev;
2023-10-18 20:27:40 +02:00
void* data;
};
2023-10-24 20:25:22 +02:00
LIFO mklifo();
void push(LIFO*, void*);
void* pop(LIFO*);
2023-10-18 20:27:40 +02:00
int main(void)
{
2023-10-24 20:25:22 +02:00
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);
2023-10-18 20:27:40 +02:00
}
2023-10-24 20:25:22 +02:00
LIFO mklifo(){
return calloc(1, sizeof(struct Node));
2023-10-18 20:27:40 +02:00
}
2023-10-24 20:25:22 +02:00
void push(LIFO* lst, void* el){
LIFO next = mklifo();
2023-10-18 20:27:40 +02:00
(*lst)->data = el;
2023-10-24 20:25:22 +02:00
next->prev = *lst;
*lst = next;
2023-10-18 20:27:40 +02:00
}
2023-10-24 20:25:22 +02:00
void *pop(LIFO* lst){
2023-10-18 20:27:40 +02:00
*lst = (*lst)->prev;
2023-10-24 20:25:22 +02:00
return (*lst)->data;
2023-10-18 20:27:40 +02:00
}