#include "stdio.h" #include "stdlib.h" typedef struct node { float data; node *prox; node *prev; } node; typedef struct { int size; node *base; node *top; } stack; void initStack(stack *s) { s->base = s->top = NULL; s->size = 0; } node *createNode(float data) { node *n; n = (node*)malloc(sizeof(node)); n->prev = n->prox = NULL; n->data = data; return n; } void push(stack *s, float data) { node *n = createNode(data); if (s->size == 0) s->base = s->top = n; else { n->prev = s->top; s->top->prox = n; s->top = n; } s->size++; } float pop(stack *s) { if (s->size == 0) return 0; else { node *t = s->top; float data = s->top->data; s->top = s->top->prev; if (s->top != NULL) s->top->prox = NULL; s->size--; free(t); return data; } } void showStack(stack *s) { node *t; t = s->base; while (t != NULL) { printf("%f\n", t->data); t = t->prox; } } int main() { stack s; initStack(&s); push(&s, 1); push(&s, 2); push(&s, 3); showStack(&s); printf("%f \n", pop(&s)); printf("%f \n", pop(&s)); printf("%f \n", pop(&s)); printf("%f \n", pop(&s)); return 0; }