.
This commit is contained in:
41
bac2/os/chap2/ex5.c
Normal file
41
bac2/os/chap2/ex5.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct node{
|
||||
struct node* prev;
|
||||
void* data;
|
||||
};
|
||||
|
||||
typedef struct node* LIFO;
|
||||
|
||||
LIFO* mklifo ();
|
||||
void push(LIFO* lst , void*);
|
||||
void* pop(LIFO* lst);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char* t = "test";
|
||||
LIFO* lifo = mklifo();
|
||||
push(lifo, t);
|
||||
printf("%s", (char *)pop(lifo));
|
||||
|
||||
}
|
||||
|
||||
LIFO *mklifo (){
|
||||
LIFO* ret = malloc(sizeof(struct node));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void push(LIFO *lst , void *el){
|
||||
LIFO* next = mklifo();
|
||||
(*lst)->data = el;
|
||||
(*next)->prev = *lst;
|
||||
lst = next;
|
||||
}
|
||||
|
||||
void *pop(LIFO *lst){
|
||||
void* el;
|
||||
(*lst)->data = el;
|
||||
*lst = (*lst)->prev;
|
||||
return el;
|
||||
}
|
Reference in New Issue
Block a user