//链表实现
#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;
}