About

A short implementation of a stack for strings with several benefits:

  • the stack expands automatically as elements are pushed and shrinks as elements are popped.
  • in case the total size of the stack is known before usage, then the stringStackCreate function will pre-allocate all the queue elements which will make ulterior operations faster since they will not suffer from the slowdowns of realloc.

Code

stringStack.c
///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
//                                                                       //
//   stringStack                                                         //
//                                                                       //
//   An implementation of a stack of strings.                            //
//   Implemented functions:                                              //
//       - push                                                          //
//       - pop                                                           //
//       - is empty                                                      //
//       - count                                                         //
//       - size                                                          //
//       - print                                                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
/* The stringStack structure with top being the index of the next element 
 * to be inserted in stack (the top-most element to be found at top - 1).
 */
typedef struct {
   int size;
   char **store;
   int top;
} stringStack;
 
// Zero or one arguments for stringStackCreate_Internal.
#define stringStackCreate_0() stringStackCreate_Internal(1)
#define stringStackCreate_1(x) stringStackCreate_Internal(x)
#define _FUNC_OVERRIDE(_1, FUNC) FUNC
#define stringStackCreate(...) _FUNC_OVERRIDE(stringStackCreate_1(__VA_ARGS__), stringStackCreate_0())
#define stringStackIsEmpty(s) (s->top == 0)
#define stringStackSize(s) s->size
#define stringStackCount(s) s->top
 
/*
 * Creates a new stringStack with a given size.
 */
stringStack* stringStackCreate_Internal(int size) {
   stringStack *s = (stringStack*)calloc(1, sizeof(stringStack));
   if ((s->store = (char**)calloc(size, sizeof(char *))) == NULL)
       return NULL;
   s->size = size;
   s->top = 0;
   return s;
}
 
/*
 * Clears a stringStack and returns a pointer to a new empty stack.
 */
stringStack* stringStackClear(stringStack *s) {
    if (s != NULL)
        free(s);
    return stringStackCreate(1);
}
 
/*
 * Pushes an element onto the stringStack.
 */
void stringStackPush(stringStack *s, char *e) {
   if (s->top > s->size - 1)
       s->store = (char**)realloc(s->store, ++s->size * sizeof(char *));
   s->store[s->top] = (char*)calloc(strlen(e) + 1, sizeof(char));
   strncpy(s->store[s->top], e, strlen(e) + 1);
   ++s->top;
}
 
/*
 * Pops an element off the stringStack or returns NULL in case the
 * stack is empty.
 */
char *stringStackPop(stringStack *s) {
   char *e;
   if (stringStackIsEmpty(s))
       return NULL;
   --s->top;
   e = (char *)calloc(strlen(s->store[s->top]) + 1, sizeof(char *));
   strncpy(e, s->store[s->top], strlen(s->store[s->top]) + 1);
   return e;
}
 
/*
 * Prints out the elements of the stringStack.
 */
void stringStackPrint(stringStack *s) {
    int i;
    if (stringStackIsEmpty(s)) {
        printf("Stack is empty.\n");
        return;
    }
    printf("Elements in the stack: ");
    i = s->top - 1;
    do {
        printf("%s ", s->store[i]);
    } while (--i > -1);
    printf("\n");
}
 
int main(void) {
    stringStack *q = stringStackCreate(2);
    printf("stack size: %d\n", stringStackSize(q));
    stringStackPush(q, "Good");
    stringStackPush(q, "Day");
    stringStackPush(q, "!");
    stringStackPrint(q);
    printf("stack size: %d\n", stringStackSize(q));
    printf("number of elements in the stack: %d\n", stringStackCount(q));
    printf("Pop: %s\n", stringStackPop(q));
    printf("Pop: %s\n", stringStackPop(q));
    stringStackPrint(q);
    printf("Pop: %s\n", stringStackPop(q));
    stringStackPrint(q);
}

fuss/c/data_structures/stacks/strings.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.