堆栈的抽象数据类型描述:
堆栈(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;
}