#ifndef STACK_LIST_H
#define STACK_LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct Node
{
void* data;
struct Node* next;
}Node;
typedef struct StackList
{
Node* top;
size_t cnt;
}StackList;
Node* create_node(void* data);
StackList* create_stack_list(void);
bool empty_stack_list(StackList* stack);
bool pop_stack_list(StackList* stack);
void destroy_stack_list(StackList* stack);
void push_stack_list(StackList* stack,void* data);
void* top_stack_list(StackList* stack);
size_t size_stack_list(StackList* stack);
#endif
#include "stack_list.h"
Node* create_node(void* data)
{
Node* node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
StackList* create_stack_list(void)
{
StackList* stack = malloc(sizeof(StackList));
stack->top = NULL;
stack->cnt = 0;
return stack;
}
bool empty_stack_list(StackList* stack)
{
return NULL == stack->top;
}
bool pop_stack_list(StackList* stack)
{
if(empty_stack_list(stack))
return false;
Node* temp = stack->top;
stack->top = temp->next;
free(temp);
return stack->cnt--;
}
void destroy_stack_list(StackList* stack)
{
while(pop_stack_list(stack));
free(stack);
}
void push_stack_list(StackList* stack,void* data)
{
Node* node = create_node(data);
node->next = stack->top;
stack->top = node;
stack->cnt++;
}
void* top_stack_list(StackList* stack)
{
return stack->top->data;
}
size_t size_stack_list(StackList* stack)
{
return stack->cnt;
}
|