Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
62
Task/Stack/C/stack-1.c
Normal file
62
Task/Stack/C/stack-1.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* to read expanded code, run through cpp | indent -st */
|
||||
#define DECL_STACK_TYPE(type, name) \
|
||||
typedef struct stk_##name##_t{type *buf; size_t alloc,len;}*stk_##name; \
|
||||
stk_##name stk_##name##_create(size_t init_size) { \
|
||||
stk_##name s; if (!init_size) init_size = 4; \
|
||||
s = malloc(sizeof(struct stk_##name##_t)); \
|
||||
if (!s) return 0; \
|
||||
s->buf = malloc(sizeof(type) * init_size); \
|
||||
if (!s->buf) { free(s); return 0; } \
|
||||
s->len = 0, s->alloc = init_size; \
|
||||
return s; } \
|
||||
int stk_##name##_push(stk_##name s, type item) { \
|
||||
type *tmp; \
|
||||
if (s->len >= s->alloc) { \
|
||||
tmp = realloc(s->buf, s->alloc*2*sizeof(type)); \
|
||||
if (!tmp) return -1; s->buf = tmp; \
|
||||
s->alloc *= 2; } \
|
||||
s->buf[s->len++] = item; \
|
||||
return s->len; } \
|
||||
type stk_##name##_pop(stk_##name s) { \
|
||||
type tmp; \
|
||||
if (!s->len) abort(); \
|
||||
tmp = s->buf[--s->len]; \
|
||||
if (s->len * 2 <= s->alloc && s->alloc >= 8) { \
|
||||
s->alloc /= 2; \
|
||||
s->buf = realloc(s->buf, s->alloc * sizeof(type));} \
|
||||
return tmp; } \
|
||||
void stk_##name##_delete(stk_##name s) { \
|
||||
free(s->buf); free(s); }
|
||||
|
||||
#define stk_empty(s) (!(s)->len)
|
||||
#define stk_size(s) ((s)->len)
|
||||
|
||||
DECL_STACK_TYPE(int, int)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
stk_int stk = stk_int_create(0);
|
||||
|
||||
printf("pushing: ");
|
||||
for (i = 'a'; i <= 'z'; i++) {
|
||||
printf(" %c", i);
|
||||
stk_int_push(stk, i);
|
||||
}
|
||||
|
||||
printf("\nsize now: %d", stk_size(stk));
|
||||
printf("\nstack is%s empty\n", stk_empty(stk) ? "" : " not");
|
||||
|
||||
printf("\npoppoing:");
|
||||
while (stk_size(stk))
|
||||
printf(" %c", stk_int_pop(stk));
|
||||
printf("\nsize now: %d", stk_size(stk));
|
||||
printf("\nstack is%s empty\n", stk_empty(stk) ? "" : " not");
|
||||
|
||||
/* stk_int_pop(stk); <-- will abort() */
|
||||
stk_int_delete(stk);
|
||||
return 0;
|
||||
}
|
||||
71
Task/Stack/C/stack-2.c
Normal file
71
Task/Stack/C/stack-2.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define check_pointer(p) if (!p) {puts("Out of memory."); exit(EXIT_FAILURE);}
|
||||
|
||||
#define MINIMUM_SIZE 1
|
||||
/* Minimal stack size (expressed in number of elements) for which
|
||||
space is allocated. It should be at least 1. */
|
||||
#define GROWTH_FACTOR 2
|
||||
/* How much more memory is allocated each time a stack grows
|
||||
out of its allocated segment. */
|
||||
typedef int T;
|
||||
// The type of the stack elements.
|
||||
|
||||
typedef struct
|
||||
{T *bottom;
|
||||
T *top;
|
||||
T *allocated_top;} stack;
|
||||
|
||||
stack * new(void)
|
||||
/* Creates a new stack. */
|
||||
{stack *s = malloc(sizeof(stack));
|
||||
check_pointer(s);
|
||||
s->bottom = malloc(MINIMUM_SIZE * sizeof(T));
|
||||
check_pointer(s->bottom);
|
||||
s->top = s->bottom - 1;
|
||||
s->allocated_top = s->bottom + MINIMUM_SIZE - 1;
|
||||
return s;}
|
||||
|
||||
void destroy(stack *s)
|
||||
/* Frees all the memory used for a stack. */
|
||||
{free(s->bottom);
|
||||
free(s);}
|
||||
|
||||
bool empty(stack *s)
|
||||
/* Returns true iff there are no elements on the stack. This
|
||||
is different from the stack not having enough memory reserved
|
||||
for even one element, which case is never allowed to arise. */
|
||||
{return s->top < s->bottom ? true : false;}
|
||||
|
||||
void push(stack *s, T x)
|
||||
/* Puts a new element on the stack, enlarging the latter's
|
||||
memory allowance if necessary. */
|
||||
{if (s->top == s->allocated_top)
|
||||
{ptrdiff_t qtty = s->top - s->bottom + 1;
|
||||
ptrdiff_t new_qtty = GROWTH_FACTOR * qtty;
|
||||
s->bottom = realloc(s->bottom, new_qtty * sizeof(T));
|
||||
check_pointer(s->bottom);
|
||||
s->top = s->bottom + qtty - 1;
|
||||
s->allocated_top = s->bottom + new_qtty - 1;}
|
||||
*(++s->top) = x;}
|
||||
|
||||
T pop(stack *s)
|
||||
/* Removes and returns the topmost element. The result of popping
|
||||
an empty stack is undefined. */
|
||||
{return *(s->top--);}
|
||||
|
||||
void compress(stack *s)
|
||||
/* Frees any memory the stack isn't actually using. The
|
||||
allocated portion still isn't allowed to shrink smaller than
|
||||
MINIMUM_SIZE. If all the stack's memory is in use, nothing
|
||||
happens. */
|
||||
{if (s->top == s->allocated_top) return;
|
||||
ptrdiff_t qtty = s->top - s->bottom + 1;
|
||||
if (qtty < MINIMUM_SIZE) qtty = MINIMUM_SIZE;
|
||||
size_t new_size = qtty * sizeof(T);
|
||||
s->bottom = realloc(s->bottom, new_size);
|
||||
check_pointer(s->bottom);
|
||||
s->allocated_top = s->bottom + qtty - 1;}
|
||||
Loading…
Add table
Add a link
Reference in a new issue