//stack.h
#include< stdlib.h >
#include< stdio.h >
#include< string.h >
#define TRUE 1
#define FALSE 0
typedef struct{
int row;
int col;
}LElemType;
typedef struct temp{
struct temp *link;
LElemType data;
}Lstack;
Lstack * InitStack( );
void DestroyStack( Lstack *S );
void ClearStack( Lstack *S );
void GetTop( Lstack *S, LElemType *e );
void Push( Lstack *S, LElemType e );
void Pop( Lstack *S, LElemType *e );
int Length( Lstack *S );
int StackEmpty( Lstack *S );
void Print( Lstack *S );
//创建一个空栈,返回指向该空栈的地址。S指向栈,S->link指向栈顶。创建时将S->link指向空,作为栈为空的标志。
Lstack * InitStack( )
{
Lstack *S;
S = ( Lstack * )malloc( sizeof( Lstack ) );
if( !S ){
printf( "OVERFLOW!\n");
exit( EXIT_FAILURE );
}
S->link = NULL;
return S;
}
//先将栈清空,再将为指向栈的指针分配的存贮空间释放
void DestroyStack( Lstack *S )
{
ClearStack( S );
free( S );
}
//将栈清为空栈
void ClearStack( Lstack *S )
{
Lstack *top;
top = S->link;
while( top != NULL ){
S->link = S->link->link;
free( top );
top = S->link;
}
S->link = NULL;
}
//获得栈顶元素
void GetTop( Lstack *S, LElemType *e )
{
if( S->link == NULL ){
printf( "the stack is empty!\n");
exit( EXIT_FAILURE );
}
else{
*e = S->link->data;
}
}
//压栈,由栈的结构决定每次压栈时都将S->link指向为新的元素分配的空间,放在链表的第一项,且将第一项的link指向原来的第一项
void Push( Lstack *S, LElemType e )
{
Lstack *first;
first = ( Lstack * )malloc( sizeof( Lstack ) );
first->data = e;
first->link = S->link;
S->link = first;
}
//弹栈,<span style="font-family: Arial, Helvetica, sans-serif;">由栈的结构决定每次弹栈时都将链表的第一项弹出,并将S->link指向弹栈前的第二项</span>
void Pop( Lstack *S, LElemType *e )
{
if( S->link == NULL ){
printf( "the stack is empty!\n");
exit( EXIT_FAILURE );
}
*e = S->link->data;
S->link =S->link->link;
}
//栈中元素个数,栈底的标志是栈底的link指向NULL
int Length( Lstack *S )
{
int count = 0;
Lstack *current = S->link;
while( current != NULL ){
count++;
current = current->link;
}
return count;
}
//判断栈是否为空,若为空,返回TRUE,非空返回FALSE
int StackEmpty( Lstack *S )
{
if( Length( S ) )
return FALSE;
return TRUE;
}
//打印栈中的所有元素
void Print( Lstack *S )
{
Lstack *current;
current = S->link;
while( current != NULL ){
if( current == S->link )
printf( "top of the stack\n" );
printf("position:x=%d,y=%d\n", current->data.row, current->data.col );
current = current->link;
}
printf( "base of the stack\n" );
}
</pre><pre code_snippet_id="378257" snippet_file_name="blog_20140605_7_1380167" name="code" class="cpp"><pre name="code" class="cpp">#include< stdio.h >
#include"stack.h"
void main()
{
Lstack *stack;
LElemType e;
int length;
stack = InitStack( );
Push( stack, 5 );
Push( stack, 56 );
Push( stack, 36 );
Push( stack, 14 );
Push( stack, 84 );
Push( stack, 9 );
Push( stack, 10 );
Push( stack, 25 );
Push( stack, 16 );
//打印
printf( "****************打印栈中元素****************\n" );
Print( stack );
//栈中元素个数
printf( "************栈中元素个数********************\n" );
length = Length( stack );
printf( "the length of the stack is %d\n", length );
//获得栈顶元素但不弹栈
printf( "************获得栈顶元素但不弹栈************\n" );
GetTop( stack, &e );
printf( "the top of the stack is %d\n", e );
//弹栈并打印出栈的元素和弹栈后栈中的元素
Pop( stack, &e );
printf( "*************获得并弹出栈顶元素*************\n" );
printf( "the top element of the stack is %d\n", e );
printf( "*************弹栈后打印栈中元素*************\n" );
Print( stack );
printf( "******************销毁栈********************\n" );
DestroyStack( stack );
}
结果