顺序栈实现
fatal.h
#ifndef fatal
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType * base;
ElemType * top;
int stackSize;
} SqStack;
#endif
main.c
#include "fatal.h"
Status InitStack ( SqStack * s)
{
s-> base = ( ElemType * ) malloc ( sizeof ( ElemType) * MAXSIZE) ;
if ( ! s-> base) exit ( OVERFLOW) ;
s-> top = s-> base;
s-> stackSize = MAXSIZE;
return OK;
}
Status DestroyStack ( SqStack * s)
{
if ( s-> base)
{
free ( s-> base) ;
s-> stackSize = 0 ;
s-> base = s-> top = NULL ;
}
return OK;
}
Status StackEmpty ( SqStack s)
{
if ( s. base == s. top)
return TRUE;
else
return FALSE;
}
int StackLength ( SqStack s)
{
return s. top - s. base;
}
int GetTopStack ( SqStack s)
{
if ( StackEmpty ( s) ) return ERROR;
return * ( -- s. top) ;
}
Status ClearStack ( SqStack s)
{
if ( s. base) s. top = s. base;
return OK;
}
Status Push ( SqStack * s, ElemType e)
{
if ( s-> top - s-> base == s-> stackSize)
return ERROR;
* s-> top++ = e;
return OK;
}
Status Pop ( SqStack * s, ElemType * e)
{
if ( s-> top == s-> base)
return ERROR;
* e = * -- s-> top;
return OK;
}
Status TraverseStack ( SqStack s)
{
if ( StackEmpty ( s) )
return ERROR;
ElemType* first = s. base;
printf ( "遍历栈:" ) ;
while ( first < s. top)
{
printf ( "%d " , * first) ;
first++ ;
}
printf ( "\n" ) ;
return OK;
}
int main ( )
{
SqStack s;
int len, e;
if ( InitStack ( & s) )
printf ( "初始化成功\n" ) ;
if ( StackEmpty ( s) )
printf ( "栈为空\n" ) ;
else
printf ( "栈不为空\n" ) ;
len = StackLength ( s) ;
printf ( "栈的长度为:%d\n" , len) ;
if ( Push ( & s, 3 ) )
printf ( "入栈成功\n" ) ;
if ( Push ( & s, 6 ) )
printf ( "入栈成功\n" ) ;
TraverseStack ( s) ;
len = StackLength ( s) ;
printf ( "栈的长度为:%d\n" , len) ;
if ( Pop ( & s, & e) )
printf ( "出栈成功:%d\n" , e) ;
len = StackLength ( s) ;
printf ( "栈的长度为:%d\n" , len) ;
if ( Pop ( & s, & e) )
printf ( "出栈成功:%d\n" , e) ;
printf ( "栈顶元素值:%d\n" , GetTopStack ( s) ) ;
return 0 ;
}
链表实现
fatal.h
#ifndef fatal
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;
typedef struct StackNode
{
ElemType data;
struct StackNode* next;
} StackNode, * LinkStack;
LinkStack s;
#endif
main.c
#include "fatal.h"
Status InitStack ( LinkStack * s) {
* s = NULL ;
return OK;
}
Status StackEmpty ( LinkStack s) {
if ( s == NULL ) return TRUE;
else return FALSE;
}
Status Push ( LinkStack * s, ElemType e) {
LinkStack p = ( LinkStack) malloc ( sizeof ( StackNode) ) ;
p-> data = e;
p-> next = * s;
* s = p;
return OK;
}
Status Pop ( LinkStack * s, ElemType * e) {
LinkStack p = * s;
if ( StackEmpty ( * s) ) return ERROR;
* e = ( * s) -> data;
* s = ( * s) -> next;
free ( p) ;
return OK;
}
ElemType GetTop ( LinkStack s) {
if ( ! StackEmpty ( s) )
return s-> data;
}
int main ( ) {
ElemType e;
if ( InitStack ( & s) )
printf ( "初始化成功\n" ) ;
if ( StackEmpty ( s) )
printf ( "链栈为空\n" ) ;
else
printf ( "链栈不为空\n" ) ;
if ( Push ( & s, 2 ) )
printf ( "入栈成功\n" ) ;
if ( Pop ( & s, & e) )
printf ( "出栈成功:%d\n" , e) ;
else
printf ( "出栈失败\n" ) ;
if ( Push ( & s, 3 ) )
printf ( "入栈成功\n" ) ;
if ( Push ( & s, 5 ) )
printf ( "入栈成功\n" ) ;
printf ( "%d\n" , GetTop ( s) ) ;
return 0 ;
}
参考:数据结构与算法基础(青岛大学-王卓)