This is a creation in
Article, where the information may have evolved or changed.
Golang
package stackimport ( "errors") Type stack []interface{}func (Stack *stack) push (v interface{}) { *stack = append (*Stack,  V)}func (stack *stack) pop () (interface{}, error) { if len (*stack) == 0 { return nil, Errors. New ("Stack empty") } v := (* Stack) [Len (*stack) - 1] *stack = (*stack) [: Len (*stack) - 1] return v, nil }func (Stack *stack) Top () (interface{}, error) { if len (*stack) == 0 { return nil, errors. New ("Stack empty") } return (*stack) [Len (*stack) - 1], nil }func (stack *Stack) len () int { return len (*stack)}
The same idea is implemented in C:
Typedef struct my_stack_t my_stack_t;struct my_stack_t { void Array of **s; /* elements for void * */ int use; /* the number of elements that have been deposited */ int size; /* the number of allocations in memory */ void (*do_free) (void *);}; #define  DEFAULT_N 100INT STACK_INIT (my_stack_t *s, void (*do_free) ()) { assert (s != null); s->s = (void **) malloc (sizeof (void *) * default_n); s->use = 0; s->size = DEFAULT_N; s->do_free = do_free; return 0;} Int stack_push (my_stack_t *s, void *v) { assert (s != null); char *p; if (s->use == s->size) { p = realloc (s->s, s->size * 2); if (p == null) return -1; s->size * = 2; } s->s[s->use++] = v ; return 0;} Void *stack_pop (my_stack_t *s) { assert (S != NULL); if (s->use == 0) { return null; } return s->s[--s- >use];} Void *stack_top (my_stack_t *s) { assert (s !=  NULL); if (s->use == 0) { return null; } return s- >s[s->use - 1]; }int stack_len (my_stack_t *s) { Return s->use;} Int stack_free (my_stack_t *s) { void *v; if (S->do_free == null) { return 0; } while ((V = stack_pop (s)) != null) { s->do_free (s); } return 0;}
*interface{} in Golang is equivalent to void in C *