For the basic concept of stacks and the relationship to Catalan numbers, see my other articles
Reference data structure and algorithm analysis--c language description
#include <stdio.h> #include <stdlib.h>/* Stack's linked list realizes */typedef struct stacknode{struct stacknode *next; int data;} Stacknode,*stack; Stack createstack (void);//Create an empty stack void Makeempty (stack s);//empty stack int IsEmpty (stack s);//test is empty stack void Push (int data,stack S) ;//Into Stack void POPs (stack s);//stack int top (stack s);//return stack top element int main () {stack sp=null; Sp=createstack (); Push (5,SP); Push (4,SP); Push (8,SP); printf ("%d\n", Top (SP)); Pop (SP); printf ("%d\n", Top (SP)); return 0;} Stack createstack (void) {stack S; S=malloc (sizeof (Stacknode)); if (s==null) printf ("Out of space!\n"); s->next=null; Makeempty (S); return S;} void Makeempty (Stack S) {if (s==null) printf ("Please Create Stack first!\n"); else while (! IsEmpty (s)) Pop (s);} int IsEmpty (Stack S) {return s->next==null;} void Push (int data,stack S) {Stack tmp; Tmp=malloc (sizeof (Stacknode)); if (tmp==null) printf ("Out of space!\n"); else {tmp->dAta=data; tmp->next=s->next; s->next=tmp; }}void Pop (Stack S) {stack tmp; if (IsEmpty (S)) printf ("Empty stack!\n"); else {tmp=s->next; s->next=s->next->next; Free (TMP); }}int Top (Stack S) {if (! IsEmpty (S)) return s->next->data; else printf ("Empty stack!\n"); return 0;}
Data structure Learning--The chain list implementation of stacks (programmatic)