Tag: Stack
#include <iostream>using namespace std; #define Stack_init_size 10#define stackincrement 10#define elemtype Inttypedef struct{elemtype *base;int top;size_t capacity;} Sqstack;bool isfull (Sqstack *st) {return st->top >= st->capacity;} BOOL IsEmpty (Sqstack *st) {return st->top = = 0;} BOOL Initstack (Sqstack *st) {st->base = (elemtype*) malloc (stack_init_size * sizeof (Elemtype)); st->top = 0;st-> capacity = Stack_init_size;return true;} Elemtype GetTop (Sqstack *st) {if (! IsEmpty (ST)) {return st->top;} cout<< "Stack empty!" <<endl;return-1;} void Push (Sqstack *st, elemtype Item) {if (! Isfull (ST)) {st->base[st->top++] = Item;} else{cout<< "Stack full!" <<endl;}} void Show (Sqstack *st) {for (int i=st->top-1; i>=0;-I.) {cout<<st->base[i]<< "";} Cout<<endl;} Elemtype Pop (Sqstack *st) {if (! IsEmpty (ST)) {St->top--;return st->base[st->top];} return-1;} int main () {Sqstack st;initstack (&st); int i = 0;for (; i<5; ++i) Push (&st,i); Pop (&ST); Show (&st); return 1;}
C Language implementation of stacks