// Linked List Implementation
# Include <stdlib. h>
# Include <stdio. h>
# Define false 0
# Define true 1
# Define OK 1
# Define error-1
Typedef char datatype;
Struct stacknode {
Datatype data;
Struct stacknode * link;
};
Struct stacks {
Struct stacknode * top;
};
Typedef struct stacks linkstack;
Int initstack (linkstack * s)
{
S-> Top = (struct stacknode *) malloc (sizeof (struct stacknode ));
S-> top-> link = NULL;
Return OK;
}
Int stackempty (linkstack * s)
{
Return (S-> top-> link = NULL );
}
Int push (linkstack * s, datatype node)
{
Struct stacknode * newnode = NULL;
If (! (Newnode = (struct stacknode *) malloc (sizeof (struct stacknode ))))
Return Error;
Newnode-> DATA = node;
Newnode-> link = s-> top;
S-> Top = newnode;
Return OK;
}
Datatype POP (linkstack * s)
{
If (stackempty (s ))
Return Error;
Struct stacknode * topnode = NULL;
Topnode = s-> top;
S-> Top = s-> top-> link;
Char CH = topnode-> data;
Free (topnode );
Return ch;
}
Datatype getstacktop (linkstack * s)
{
If (stackempty (s ))
Return Error;
Return S-> top-> data;
}
Int main ()
{
Linkstack * s = (linkstack *) malloc (sizeof (linkstack ));
Initstack (s );
Push (S, 'M ');
Push (S, 'E ');
Push (S, 'J ');
Push (S, 'H ');
Push (S, 'A ');
Push (S, 'n ');
Printf ("% C/N", getstacktop (s ));
While (! Stackempty (s ))
{
Printf ("% C/N", pop (s ));
}
Return OK;
}
// Array Implementation # include <stdio. h> # include <stdlib. h> # define maxsize 100 # define true 1 # define false 0 # define OK 1 # define error 0 typedef int datatype; typedef struct stacknode {datatype data; int top; int maxsize; struct stacknode * Next;} stacks; void initstack (stacks * s) {S-> Top =-1; s-> maxsize = maxsize;} int stackempty (stacks * s) {return (S-> Top =-1);} int stackfull (stacks * s) {return (S-> Top = s-> maxsiz E);} int push (stacks * s, datatype data) {If (stackfull (s) Return Error; s-> top ++; s [S-> top]. data = data; Return OK;} datatype POP (stacks * s) {If (stackempty (s) Return Error; datatype temp; temp = s [S-> top]. data; s-> top --; return temp;} datatype gettop (stacks * s) {return s [S-> top]. data;} void main () {stacks * s = (stacks *) malloc (sizeof (stacks); initstack (s); int I = 0; while (I ++ <5) Push (S, I ); While (! Stackempty (s) {printf ("% d", pop (s);} printf ("/N") ;}// the above code can be directly run through compilation.