Stack (C)

From LiteratePrograms

Jump to: navigation, search
Other implementations: C | Java | Python

This program is a code dump.
Code dumps are articles with little or no documentation or rearrangement of code. Please help to turn it into a literate program. Also make sure that the source of this code does consent to release it under the MIT or public domain license.


This is an implementation of a Stack in C. WARNING! This article is inherently unstable.

#define STACKSIZE 100
struct stack {
    int top;
    int items[STACKSIZE];
};
int
pop(struct stack *pstack)
{
    if(!empty(pstack)) {
        return ((pstack->items)[pstack->top--]);
    }
    else {
        printf("STACK UNDERFLOW DONCE\n");
        exit(1);
    }
}
void
push(int val, struct stack *pstack)
{
    if(pstack->top < (STACKSIZE - 1)) {
        (pstack->items)[pstack->top++] = val;
    }
    else {
        printf("STACK OVERFLOW DONCE");
        exit(1);
    }
}
int
stacktop(struct stack *pstack)
{
    return ((pstack->items)[pstack->top]);
}
int
empty(struct stack* pstack) {
    if(pstack->top == 0) { return 1; }
    else { return 0; }
}
Download code
Views