共享栈的实现
import javax. crypto. Mac;
public class DoubleStatk {
private final static int MAXSIZE= ;
private int [ ] stackElem;
private int top ;
private int top ;
public DoubleStatk ( ) {
top = - ;
top = MAXSIZE;
stackElem= new int [ MAXSIZE] ;
}
public boolean isEmptyStack ( ) {
if ( top == - && top == MAXSIZE) {
return true ;
}
return false ;
}
public void clearStack ( ) {
top = - ;
top = MAXSIZE;
}
public int lengthStak ( ) {
return ( top + ) + ( MAXSIZE- top ) ;
}
public int getTop Elem ( ) {
if ( top == - ) {
return - ;
}
return stackElem[ top ] ;
}
public int getTop Elem ( ) {
if ( top == MAXSIZE) {
return - ;
}
return stackElem[ top ] ;
}
public void stackPush ( int stackNumber, int e) {
if ( top + == top ) {
System. out. println ( "栈已满" ) ;
return ;
}
if ( stackNumber== ) {
top += ;
stackElem[ top ] = e;
return ;
}
if ( stackNumber== ) {
top -= ;
stackElem[ top ] = e;
return ;
}
}
public int stackPop ( int stackNumber) {
int rs;
if ( isEmptyStack ( ) ) {
System. out. println ( "栈为空" ) ;
return - ;
}
if ( stackNumber== ) {
rs= stackElem[ top ] ;
top -- ;
} else if ( stackNumber== ) {
rs= stackElem[ top ] ;
top ++ ;
} else {
System. out. println ( "输入数据有误" ) ;
return - ;
}
return rs;
}
public void stackTraverse ( ) {
System. out. println ( "此时,栈中的元素为:" ) ;
int i= ;
while ( i<= top ) {
System. out. println ( stackElem[ i++ ] + " " ) ;
}
i= top ;
while ( i< MAXSIZE) {
System. out. println ( stackElem[ i++ ] + " " ) ;
}
System. out. println ( ) ;
}
public static void main ( String[ ] args) {
DoubleStatk seqStack= new DoubleStatk ( ) ;
for ( int j= ; j<= ; j++ ) {
seqStack. stackPush ( , j) ;
}
for ( int i= MAXSIZE; i>= MAXSIZE- ; i-- ) {
seqStack. stackPush ( , i) ;
}
seqStack. stackTraverse ( ) ;
System. out. println ( "栈的长度为:" + seqStack. lengthStak ( ) ) ;
seqStack. stackPop ( ) ;
seqStack. stackTraverse ( ) ;
System. out. println ( "栈 的栈顶元素为: " + seqStack. getTop Elem ( ) ) ;
System. out. println ( "栈 的栈顶元素为: " + seqStack. getTop Elem ( ) ) ;
System. out. println ( "栈的长度为: " + seqStack. lengthStak ( ) ) ;
for ( int i = ; i <= MAXSIZE- ; i++ ) {
seqStack. stackPush ( , i) ;
}
seqStack. stackTraverse ( ) ;
System. out. println ( "栈 的栈顶元素为: " + seqStack. getTop Elem ( ) ) ;
System. out. println ( "栈 的栈顶元素为: " + seqStack. getTop Elem ( ) ) ;
System. out. println ( "栈顶元素为: " + seqStack. getTop Elem ( ) ) ;
System. out. println ( "栈的长度为: " + seqStack. lengthStak ( ) ) ;
System. out. println ( "栈是否为空: " + seqStack. isEmptyStack ( ) ) ;
seqStack. clearStack ( ) ;
System. out. println ( "栈是否为空: " + seqStack. isEmptyStack ( ) ) ;
}
}
链表栈
import org. junit. jupiter. api. Test;
public class NodeStack < T> {
private Node< T> top= null;
public NodeStack ( ) {
this . top= null;
}
public boolean isEmpty ( ) {
if ( top!= null) {
return false ;
}
return true ;
}
public boolean push ( T value) {
Node< T> node= new Node < T> ( value) ;
node. setNext ( top) ;
top= node;
return true ;
}
public T pop ( ) {
if ( top== null) {
return null;
}
T tmp= top. data;
top= top. getNext ( ) ;
return tmp;
}
public T peek ( ) {
if ( isEmpty ( ) ) {
return null;
}
return top. data;
}
class Node < T> {
private T data;
private Node< T> next;
public Node ( T data) {
this . data= data;
}
public Node< T> getNext ( ) {
return this . next;
}
public void setNext ( Node< T> n) {
this . next= n;
}
public T getData ( ) {
return this . data;
}
public void setData ( T d) {
this . data= d;
}
}
public static void main ( String[ ] args) {
NodeStack< String> ns= new NodeStack < String> ( ) ;
System. out. println ( "=======是否为空======" ) ;
System. out. println ( ns. isEmpty ( ) ) ;
System. out. println ( "=============" ) ;
System. out. println ( "=======压栈======" ) ;
ns. push ( "北京" ) ;
ns. push ( "上海" ) ;
ns. push ( "深证" ) ;
ns. push ( "广州" ) ;
System. out. println ( "=======是否为空======" ) ;
System. out. println ( ns. isEmpty ( ) ) ;
System. out. println ( "=============" ) ;
System. out. println ( "=======出栈=======" ) ;
System. out. println ( ns. pop ( ) ) ;
System. out. println ( ns. pop ( ) ) ;
System. out. println ( ns. pop ( ) ) ;
System. out. println ( ns. pop ( ) ) ;
System. out. println ( ns. pop ( ) ) ;
System. out. println ( "=======是否为空======" ) ;
System. out. println ( ns. isEmpty ( ) ) ;
System. out. println ( "=============" ) ;
}
}