The chain stack is more flexible than the limited space of the sequential stack
#include <stdio.h>#include<malloc.h>typedefintSelemtype;//element TypetypedefintStatus;//return value type#defineOK 1//Operation succeeded#defineERROR 0//operation failedtypedefstructStacknode//Link Stack node structure{Selemtype date; //node Data structStacknode *next;//Node Pointers}*linkstackptr;//Knot nametypedefstructLinkstack//Link Stack structure{linkstackptr top; //stack Top pointer intCount//number of stack nodes}linkstack;//Stack name/*the stack operation of the link stack*/Status Push (linkstack*S, Selemtype e) {linkstackptr s= (linkstackptr)malloc(sizeof(Stacknode));//apply for a new node.S->date = e;//the data for the new node equals E.S->next = s->top;//The next node of the new node equals the current stack top node .S->top = s;//The top node of the stack equals the new node.s->count++;//data in the stack plus 1 returnOK;}/*Stack-out operation of the chain stack*/Status Pop (linkstack*s, Selemtype *e) { if(S->count = =0)//determine if the stack is empty returnERROR; Linkstackptr s= (linkstackptr)malloc(sizeof(Stacknode));//apply for a new node.s = s->top;//The new node equals the top node of the current stack .*e = s->date;//e equals the data of the top node of the stack.S->top = s->next;//The top of the stack equals the next node of the new node Free(s);//releasing new nodes.s->count--;//data in stack minus 1 returnOK;}voidMain () {Linkstack S; //Create Stack LS.count =0;//stack Top pointer is-1, stack is empty intE//elements into stacks and stacks while(true) {printf ("Please select the operation for the link stack: \ n"); printf ("1. into the stack \ n"); printf ("2. Out of stack \ n"); printf ("3. Exit \ n"); intA; scanf ("%d", &a); Switch(a) { Case 1: printf ("Please enter the elements into the stack:"); scanf ("%d", &e); if(Push (&S, E)) printf ("into the stack successfully \ n"); Elseprintf ("failed to enter the Stack \ n"); Break; Case 2: if(Pop (&s, &e)) printf ("The elements of the stack are:%d\n", E); Elseprintf ("stack empty \ n"); Break; Case 3: return; default: printf ("Select Error \ n"); Break; } }}
Chained stacks-C language implementation