About

An implementation of a stack for integers.

Code

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
//                                                                       //
//   intStack                                                            //
//                                                                       //
//   An implementation of an integer stack.                              //
//   Implemented functions:                                              //
//       - push                                                          //
//       - pop                                                           //
//       - is empty                                                      //
//       - count                                                         //
//       - size                                                          //
//       - print                                                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
// Stack structure.
typedef struct {
   int size;
   int *store;
   int top;
} intStack;
 
// Zero or one arguments for intStackCreate_Internal.
#define intStackCreate_0() intStackCreate_Internal(1)
#define intStackCreate_1(x) intStackCreate_Internal(x)
#define _FUNC_OVERRIDE(_1, FUNC) FUNC
#define intStackCreate(...) _FUNC_OVERRIDE(intStackCreate_1(__VA_ARGS__), intStackCreate_0())
#define intStackIsEmpty(s) (s->top) == 0
#define intStackSize(s) s->size
#define intStackCount(s) s->top
 
// Stack constructor.
intStack* intStackCreate_Internal(int size) {
   intStack *s = (intStack*)calloc(1, sizeof(intStack));
   if ((s->store = (int*)calloc(size, sizeof(int))) == NULL)
       return NULL;
   s->size = size;
   s->top = 0;
   return s;
}
 
// Clear the stack of items.
intStack* intStackClear(intStack *s) {
    if (s != NULL)
        free(s);
    return intStackCreate(1);
}
 
// Pushes an item onto the stack.
void intStackPush(intStack *s, int i) {
   if (s->top > s->size - 1)
       s->store = (int *)realloc(s->store, ++s->size * sizeof(int));
   s->store[s->top] = i;
   ++s->top;
}
 
// Pops an element off the stack and returns it.
int intStackPop(intStack *s) {
   int *e;
   if (intStackIsEmpty(s))
       return (int) NULL;
   return s->store[--s->top];
}
 
// Prints out the stack.
void intStackPrint(intStack *s) {
    int i;
    if (intStackIsEmpty(s)) {
        printf("Stack is empty.\n");
        return;
    }
    printf("Items in the stack: ");
    i = s->top - 1;
    do {
        printf("%d ", s->store[i]);
    } while (--i > -1);
    printf("\n");
}
 
int main(int argc, char **argv) {
    intStack *s = intStackCreate();
    int i;
    intStackPush(s, 1000);
    intStackPush(s, 0);
    printf("%d\n", intStackPop(s));
    if((i = intStackPop(s)) != (int) NULL)
        printf("%d\n", i);
    printf("%d\n", intStackPop(s));
    if((i = intStackPop(s)) == (int)NULL)
        printf("Stack is empty!\n");
 
    intStackPush(s, -1);
    intStackPrint(s);
 
    return 0;
}

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