I have this C stack implementation that knows its minimum and maximum values:
minmax_stack.h
#ifndef NET_CODERODDE_UTIL_MINMAX_STACK
#define NET_CODERODDE_UTIL_MINMAX_STACK
#include <stdlib.h>
typedef struct minmax_stack_node {
void* datum;
struct minmax_stack_node* next_node;
struct minmax_stack_node* min_node;
struct minmax_stack_node* max_node;
} minmax_stack_node;
typedef struct {
size_t size;
minmax_stack_node* top_node;
minmax_stack_node* min_node;
minmax_stack_node* max_node;
int (*compare)(void*, void*);
} minmax_stack;
void minmax_stack_init (minmax_stack* stack, int (*compare)(void*, void*));
void minmax_stack_free (minmax_stack* stack);
void minmax_stack_push (minmax_stack* stack, void* datum);
size_t minmax_stack_size (minmax_stack* stack);
void* minmax_stack_pop (minmax_stack* stack);
void* minmax_stack_min (minmax_stack* stack);
void* minmax_stack_max (minmax_stack* stack);
void minmax_stack_print(minmax_stack* stack);
#endif /* NET_CODERODDE_UTIL_MINMAX_STACK */
minmax_stack.c
#include "minmax_stack.h"
#include <stdio.h>
#include <stdlib.h>
void minmax_stack_init(minmax_stack* stack, int (*compare)(void*, void*))
{
stack->compare = compare;
stack->size = 0;
stack->top_node = NULL;
stack->min_node = NULL;
stack->max_node = NULL;
}
void minmax_stack_free(minmax_stack* stack)
{
minmax_stack_node* current_node = stack->top_node;
minmax_stack_node* next_node;
while (current_node)
{
next_node = current_node->next_node;
free(current_node);
current_node = next_node;
}
stack->compare = NULL;
stack->min_node = NULL;
stack->max_node = NULL;
stack->top_node = NULL;
stack->size = 0;
}
void minmax_stack_push(minmax_stack* stack, void* datum)
{
int cmp_min;
int cmp_max;
minmax_stack_node* new_node = malloc(sizeof(*new_node));
new_node->datum = datum;
if (stack->top_node)
{
new_node->next_node = stack->top_node;
new_node->min_node = stack->min_node;
new_node->max_node = stack->max_node;
stack->top_node = new_node;
cmp_min = stack->compare(datum, stack->min_node->datum);
cmp_max = stack->compare(datum, stack->max_node->datum);
if (cmp_min < 0) stack->min_node = new_node;
if (cmp_max > 0) stack->max_node = new_node;
}
else
{
stack->top_node = new_node;
stack->min_node = new_node;
stack->max_node = new_node;
new_node->next_node = NULL;
}
stack->size++;
}
size_t minmax_stack_size(minmax_stack* stack)
{
return stack->size;
}
void* minmax_stack_pop(minmax_stack* stack)
{
minmax_stack_node* node;
void* return_value;
if (stack->size == 0) return NULL;
node = stack->top_node;
return_value = node->datum;
stack->max_node = node->max_node;
stack->min_node = node->min_node;
stack->top_node = node->next_node;
stack->size--;
return return_value;
}
void* minmax_stack_min(minmax_stack* stack)
{
return stack->size == 0 ? NULL : stack->min_node->datum;
}
void* minmax_stack_max(minmax_stack* stack)
{
return stack->size == 0 ? NULL : stack->max_node->datum;
}
void minmax_stack_print(minmax_stack* stack)
{
const char* separator = "";
printf("[");
for (minmax_stack_node* current_node = stack->top_node;
current_node;
current_node = current_node->next_node)
{
printf("%s", separator);
printf("%d", (int) current_node->datum);
separator = ", ";
}
printf("]");
}
main.c
#include "minmax_stack.h"
#include <stdio.h>
#include <string.h>
static int compare(void* a, void* b)
{
return (int)((int) a - (int) b);
}
int main() {
int num;
minmax_stack stack;
minmax_stack_init(&stack, compare);
char command[100];
while (1)
{
scanf("%s", command);
if (strcmp(command, "push") == 0)
{
scanf("%d", &num);
minmax_stack_push(&stack, (void*) num);
}
else if (strcmp(command, "pop") == 0)
{
printf("%d\n", (int) minmax_stack_pop(&stack));
}
else if (strcmp(command, "min") == 0)
{
printf("%d\n", (int) minmax_stack_min(&stack));
}
else if (strcmp(command, "max") == 0)
{
printf("%d\n", (int) minmax_stack_max(&stack));
}
else if (strcmp(command, "print") == 0)
{
minmax_stack_print(&stack);
puts("");
} else if (strcmp(command, "quit") == 0)
{
return 0;
}
}
}
Any critique is much appreciated.