1. 栈的定义及用法
1.1 定义
栈 (stack)是限定仅在表尾 进行插入或删除操作的线性表。对栈 来说,表尾被称为:栈顶 (top),表头被称为:栈底(bottom) 不含元素的空表被称为空栈 。
1.2 特点
后进先出 简称:LIFO 结构
1.3 图例
2. C语言下的栈
2.1 栈的定义及初始
#define STACK_SIZE = 10;
typedef struct {
SElemType * base;
SElemType * top;
int stacksize;
} SqStack;
Status InitStack ( SqStack & S) {
S. base = ( SElemType* ) malloc ( STACK_SIZE* sizeof ( SElemType) ) ;
if ( ! S. base) return - 2 ;
S. top = S. base;
S. stacksize = STACK_SIZE;
return OK;
}
2.2 取栈顶元素
Status GetTop ( SqStack S, SElemType & e) {
if ( S. top== S. base) return ERROR;
e = * ( S. top- 1 ) ;
return OK;
}
2.3 入栈
Status Push ( SqStack & S, SElemType e) {
if ( ( S. top- S. base) >= S. stacksize) {
S. base = ( SElemType* ) realloc ( S. base,
( STACK_SIZE+ 10 ) * sizeof ( SElemType) ) ;
if ( ! S. base) return - 2 ;
S. top = S. base + S. stacksize;
S. stacksize + = 10 ;
}
* S. top++ = e;
return OK;
}
2.4 出栈
Status Pop ( SqStack & S, SElemType & e) {
if ( S. top== S. base) return ERROR;
e = * -- S. top;
return OK;
}
2.5 判断空栈
Status StackEmpty ( SqStack & S) {
if ( S. top== S. base) {
printf ( "栈空\n" ) ;
return OK;
}
else return ERROR;
}
3. Java语言下的栈
3.1 构造一个空栈
SqStack ( ) {
this . stacksize = 0 ;
this . data = new int [ STACK_SIZE] ;
}
3.2 便历,输出,重写toString()
public String toString ( ) {
String result = "" ;
for ( int i = 0 ; i < this . stackSize; i++ ) {
result += data[ i] + " " ;
}
return result;
}
3.3 入栈push
public boolean push ( int elem) {
if ( this . stackSize == STACK_SIZE) {
System. out. println ( "Stack full." ) ;
return false ;
}
this . data[ this . stackSize] = elem;
this . stackSize++ ;
return true ;
}
2.4 出栈pop
public int pop ( ) {
if ( this . stackSize == 0 ) {
System. out. println ( "StackEmpty." ) ;
return '\0' ;
}
int elem = data[ this . stackSize - 1 ] ;
this . stackSize-- ;
return elem;
}
3.5 判断栈是否为空
public boolean isEmpty ( ) {
if ( this . stackSize == 0 ) {
return true ;
}
else {
return false ;
}
}
4. Java源码
4.1 SqStack类
package datastructure. stack;
public class SqStack {
public static final int STACK_SIZE = 15 ;
public int stackSize;
public int [ ] data;
SqStack ( ) {
this . stackSize = 0 ;
this . data = new int [ STACK_SIZE] ;
}
public String toString ( ) {
String result = "" ;
for ( int i = 0 ; i < this . stackSize; i++ ) {
result += data[ i] + " " ;
}
return result;
}
public boolean push ( int elem) {
if ( this . stackSize == STACK_SIZE) {
System. out. println ( "Stack full." ) ;
return false ;
}
this . data[ this . stackSize] = elem;
this . stackSize++ ;
return true ;
}
public int pop ( ) {
if ( this . stackSize == 0 ) {
System. out. println ( "StackEmpty." ) ;
return '\0' ;
}
int elem = data[ this . stackSize - 1 ] ;
this . stackSize-- ;
return elem;
}
public boolean isEmpty ( ) {
if ( this . stackSize == 0 ) {
return true ;
}
else {
return false ;
}
}
public static void main ( String[ ] args) {
SqStack S = new SqStack ( ) ;
S. push ( 1 ) ;
S. push ( 2 ) ;
S. push ( 3 ) ;
S. push ( 4 ) ;
System. out. println ( "入栈,并打印栈:" + S. toString ( ) ) ;
int temp = S. pop ( ) ;
System. out. println ( "出栈,并打印栈顶元素:" + temp) ;
S. isEmpty ( ) ;
}
}
入栈, 并打印栈:1 2 3 4
出栈, 并打印栈顶元素:4
4.2 输出样例