链式堆栈的实现

//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 );
}


 

结果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值