About

A handy implementation of character-based stack.

Code

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
//                                                                       //
//   charStack                                                           //
//                                                                       //
//   An implementation of a stack of ASCII characters [0-255].           //
//   Implemented functions:                                              //
//       - push                                                          //
//       - pop                                                           //
//       - is empty                                                      //
//       - count                                                         //
//       - size                                                          //
//       - print                                                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
// Zero or one arguments for charStackCreate_Internal.
#define charStackCreate_0() charStackCreate_Internal(1)
#define charStackCreate_1(x) charStackCreate_Internal(x)
#define _FUNC_OVERRIDE(_1, FUNC) FUNC
#define charStackCreate(...) _FUNC_OVERRIDE(charStackCreate_1(__VA_ARGS__), charStackCreate_0())
#define charStackIsEmpty(s) s->top == 0
#define charStackSize(s) s->size
#define charStackCount(s) s->top
 
// Stack structure.
typedef struct {
   int size;
   int **store;
   int top;
} charStack;
 
// Stack constructor.
charStack* charStackCreate_Internal(int size) {
   charStack *s = (charStack*)calloc(1, sizeof(charStack));
   if ((s->store = (int**)calloc(size, sizeof(int *))) == NULL)
       return NULL;
   s->size = size;
   s->top = 0;
   return s;
}
 
charStack* charStackCreate_Empty() {
   charStack *s = (charStack*)calloc(1, sizeof(charStack));
   if ((s->store = (int**)calloc(1, sizeof(int *))) == NULL)
       return NULL;
   s->size = 1;
   s->top = 0;
   return s;
}
 
// Clear the stack of items.
charStack* charStackClear(charStack *s) {
    if (s != NULL)
        free(s);
    return charStackCreate(1);
}
 
// Pushes an item onto the stack.
void charStackPush(charStack *s, int *e) {
   if (s->top > s->size - 1)
       s->store = (int**)realloc(s->store, ++s->size * sizeof(int *));
   s->store[s->top] = (int*)calloc(1, sizeof(int));
   memcpy(s->store[s->top], e, 1);
   ++s->top;
}
 
// Pops an element off the stack and returns it.
int *charStackPop(charStack *s) {
   int *e;
   if (charStackIsEmpty(s))
       return NULL;
   --s->top;
   e = (int *)calloc(1, sizeof(int *));
   memcpy(e, s->store[s->top], 1);
   return e;
}
 
int main(int argc, char **argv) {
    charStack *s = charStackCreate();
    int *c = (int *) calloc(1, sizeof(int));
    *c = 43;
 
    charStackPush(s, c);
    *c = 65;
    charStackPush(s, c);
    *c = 66;
    charStackPush(s, c);
    printf("%c\n", *charStackPop(s));
    printf("%c\n", *charStackPop(s));
    printf("%c\n", *charStackPop(s));
 
    return 0;
}

fuss/c/data_structures/stacks/characters.txt ยท Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.