《数据结构与算法分析——C语言描述》第三章 栈的基本概念和栈的顺序存储实现(C语言)

堆栈的抽象数据类型描述:
堆栈(Stack):具有一定操作约束的线性表,即只允许在一端(栈顶)进行插入和删除的线性表。
类型名称:堆栈(Stack)。
数据对象集:一个有0个或多个数据元素的有穷线性表。
数据操作集:

void make_empty( S ptrs );             //初始化栈 
int IsEmpty( S ptrs );                 //判断栈是否为空,若为空,返回1,否则返回0 
void push( ElementType x, S ptrs );    //入栈
ElementType pop( S ptrs );             //出栈并返回栈顶元素
ElementType GetTop( S ptrs );          //读取栈顶元素 

栈顶(Top) 栈底(Bottom)(不允许进行插入和删除的那一端)。
堆栈的特点是后入先出(Last In First Out)(LIFO)。
栈的顺序实现:

//栈的顺序存储
#include <stdio.h>
#include <stdlib.h>

#define ElementType int
#define Maxsize 10

typedef struct{
	ElementType data[Maxsize];
	int top;
}Stack;

typedef Stack* S;

void make_empty( S ptrs );             //初始化栈 
int IsEmpty( S ptrs );                 //判断栈是否为空,若为空,返回1,否则返回0 
void push( ElementType x, S ptrs );    //入栈
ElementType pop( S ptrs );             //出栈并返回栈顶元素
ElementType GetTop( S ptrs );          //读取栈顶元素 
 
int main ( void )
{
	Stack L;
	S ptrs = &L;
	
	make_empty( ptrs );
	printf( "ptrs -> top = %d\n", ptrs -> top );
	
	int i = 0;
	for( i = 0; i < Maxsize; i++ )
		push( i, ptrs );
	
	if( IsEmpty( ptrs ) )
		printf( "栈空\n" );
	else
		printf( "栈不空,有元素%d个\n", ptrs -> top + 1 );
	
	for( i = 0; i < Maxsize; i++ )
		printf( "%d ", ptrs -> data[i] );
	printf( "\n" );
	
	ElementType x = pop( ptrs );
	printf( "%d\n", x );
	
	x = GetTop( ptrs );
	printf( "%d", x );
	
	return 0;
} 

void make_empty( S ptrs )
{
	ptrs -> top = -1; 
}

int IsEmpty( S ptrs )
{
	return ptrs -> top == -1;
}

void push( ElementType x, S ptrs )
{
	if( ptrs -> top == Maxsize - 1 ){
		printf( "栈满\n" );
		exit(0);
	}
	ptrs -> data[ ++ptrs -> top ] = x;
}

ElementType pop( S ptrs )
{
	if( IsEmpty( ptrs ) ){
		printf( "栈空\n" );
		exit(0);
	}
	ElementType temp = ptrs -> data[ ptrs -> top-- ];
	return temp; 
} 

ElementType GetTop( S ptrs )
{
	if( IsEmpty( ptrs ) ){
		printf( "栈空\n" );
		exit(0);
	}
	return ptrs -> data[ ptrs -> top ];
}

共享栈:
利用栈底位置不变的特性,让两个顺序栈共享同一个一维数据空间,将两个栈底分别设置在共享空间的两端,两个栈顶向共享空间的中间延伸。
实现如下:

//栈的顺序存储——共享栈 
#include <stdio.h>
#include <stdlib.h>

#define ElementType int
#define Maxsize 10

typedef struct{
	ElementType data[Maxsize];
	int top1;
	int top2; 
}Stack;

typedef Stack* S;

void make_empty( S ptrs );                      //初始化栈 
int IsEmpty( S ptrs );                          //判断栈是否为空,若为空,返回1,否则返回0 
void push( ElementType x, S ptrs , int tag );    //入栈
ElementType pop( S ptrs, int tag );             //出栈并返回栈顶元素
ElementType GetTop( S ptrs, int tag );          //读取栈顶元素 
 
int main ( void )
{
	Stack L;
	S ptrs = &L;
	
	make_empty( ptrs );
	printf( "ptrs -> top1 = %d, ptrs -> top2 = %d\n", ptrs -> top1, ptrs -> top2 );
	
	int i;
	for( i = 0; i < 5; i++ )
		push( i, ptrs, 1 );
	for( i = 5; i < Maxsize; i++ )
		push( i, ptrs, 2 );
	
	if( IsEmpty( ptrs ) )
		printf( "栈空\n" );
	else
		printf( "栈不空,ptrs -> top1 = %d, ptrs -> top2 = %d\n", ptrs -> top1, ptrs -> top2 );
	
	for( i = 0; i < Maxsize; i++ )
		printf( "%d ", ptrs -> data[i] );
	printf( "\n" );
	
	ElementType x = pop( ptrs, 1 );
		printf( "%d\n", x );
	
	x = pop( ptrs, 2 );
		printf( "%d\n", x );
	
	x = GetTop( ptrs, 1 );
		printf( "%d\n", x );
	
	x = GetTop( ptrs, 2 );
		printf( "%d", x );
	
	return 0;
} 

void make_empty( S ptrs )
{
	ptrs -> top1 = -1; 
	ptrs -> top2 = Maxsize;
}

int IsEmpty( S ptrs )
{
	return (ptrs -> top1 == -1) && (ptrs -> top2 == Maxsize);
}

void push( ElementType x, S ptrs, int tag )
{
	if( ptrs -> top2 - ptrs -> top1 == 1 ){
		printf( "栈满\n" );
		exit(0);
	}
	if( tag == 1 )
		ptrs -> data[ ++ptrs -> top1 ] = x;
	else if( tag == 2 )
		ptrs -> data[ --ptrs -> top2 ] = x;
	else{
		printf( "输入有误\n" );
		exit(0); 
	} 
}

ElementType pop( S ptrs, int tag )
{
	if( IsEmpty( ptrs ) ){
		printf( "栈空\n" );
		exit(0);
	}
	ElementType temp;
	if( tag == 1 )
		temp = ptrs -> data[ ptrs -> top1-- ];
	else if( tag == 2 )
		temp = ptrs -> data[ ptrs -> top2++ ];
	else{
		printf( "输入有误\n" );
		exit(0);
	}
	return temp; 
} 

ElementType GetTop( S ptrs, int tag )
{
	if( IsEmpty( ptrs ) ){
		printf( "栈空\n" );
		exit(0);
	}
	ElementType temp;
	if( tag == 1 )
		temp = ptrs -> data[ ptrs -> top1 ];
	else if( tag == 2 )
		temp = ptrs -> data[ ptrs -> top2 ];
	else{
		printf( "输入有误\n" );
		exit(0);
	}
	return temp;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

房东的小黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值