栈实际应用现场:先入后出 (顺序栈)
#include "linkedlist.h"
#include <stdlib.h>
#include <stdio.h>
#ifdef DMALLOC
#include "dmalloc.h"
#endif
typedef struct {
LinkedList *list;
} Stack
Stack *stack_construct() {
Stack *stack = NULL;
stack = calloc(1, sizeof(Stack));
if (stack == NULL) {
return NULL;
}
stack->list = linked_list_construct();
if (stack->list == NULL) {
return NULL;
}
return stack;
}
void stack_destroy(Stack *stack) {
if (stack == NULL) {
return;
}
linked_list_destroy(stack->list);
stack->list = NULL;
free(stack);
}
void stack_push(Stack *stack, const void *data) {
LinkedListNode *list_node = NULL;
if (stack == NULL) {
return;
}
list_node = linked_list_node_construct(data);
linked_list_append_node(stack->list, list_node);
}
const void *stack_pop(Stack *stack) {
LinkedListNode *list_node = NULL;
const void *data = NULL;
if (stack == NULL) {
return NULL;
}
linked_list_seek_end(stack->list);
list_node = linked_list_get_next_node(stack->list);
if (list_node == NULL) {
return NULL;
}
data = linked_list_node_get_data(list_node);
linked_list_remove_node(stack->list, list_node);
return data;
}
int stack_is_empty(Stack *stack) {
if (stack == NULL) return 1;
return linked_list_is_empty(stack->list);
}